[Python-checkins] python/dist/src/Objects intobject.c,2.86,2.87 stringobject.c,2.175,2.176 unicodeobject.c,2.160,2.161
gvanrossum@users.sourceforge.net
gvanrossum@users.sourceforge.net
2002年8月10日 21:24:14 -0700
Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv15811/Objects
Modified Files:
intobject.c stringobject.c unicodeobject.c
Log Message:
Implement stage B0 of PEP 237: add warnings for operations that
currently return inconsistent results for ints and longs; in
particular: hex/oct/%u/%o/%x/%X of negative short ints, and x<<n that
either loses bits or changes sign. (No warnings for repr() of a long,
though that will also change to lose the trailing 'L' eventually.)
This introduces some warnings in the test suite; I'll take care of
those later.
Index: intobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/intobject.c,v
retrieving revision 2.86
retrieving revision 2.87
diff -C2 -d -r2.86 -r2.87
*** intobject.c 9 Aug 2002 15:20:48 -0000 2.86
--- intobject.c 11 Aug 2002 04:24:11 -0000 2.87
***************
*** 660,664 ****
int_lshift(PyIntObject *v, PyIntObject *w)
{
! register long a, b;
CONVERT_TO_LONG(v, a);
CONVERT_TO_LONG(w, b);
--- 660,664 ----
int_lshift(PyIntObject *v, PyIntObject *w)
{
! long a, b, c;
CONVERT_TO_LONG(v, a);
CONVERT_TO_LONG(w, b);
***************
*** 670,677 ****
return int_pos(v);
if (b >= LONG_BIT) {
return PyInt_FromLong(0L);
}
! a = (long)((unsigned long)a << b);
! return PyInt_FromLong(a);
}
--- 670,687 ----
return int_pos(v);
if (b >= LONG_BIT) {
+ if (PyErr_Warn(PyExc_DeprecationWarning,
+ "x<<y losing bits or changing sign "
+ "will return a long in Python 2.4 and up") < 0)
+ return NULL;
return PyInt_FromLong(0L);
}
! c = (long)((unsigned long)a << b);
! if ((c >> b) != a || (c < 0 && a > 0)) {
! if (PyErr_Warn(PyExc_DeprecationWarning,
! "x<<y losing bits or changing sign "
! "will return a long in Python 2.4 and up") < 0)
! return NULL;
! }
! return PyInt_FromLong(c);
}
***************
*** 762,765 ****
--- 772,781 ----
char buf[100];
long x = v -> ob_ival;
+ if (x < 0) {
+ if (PyErr_Warn(PyExc_DeprecationWarning,
+ "hex()/oct() of negative int will return "
+ "a signed string in Python 2.4 and up") < 0)
+ return NULL;
+ }
if (x == 0)
strcpy(buf, "0");
***************
*** 774,777 ****
--- 790,799 ----
char buf[100];
long x = v -> ob_ival;
+ if (x < 0) {
+ if (PyErr_Warn(PyExc_DeprecationWarning,
+ "hex()/oct() of negative int will return "
+ "a signed string in Python 2.4 and up") < 0)
+ return NULL;
+ }
PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
return PyString_FromString(buf);
Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.175
retrieving revision 2.176
diff -C2 -d -r2.175 -r2.176
*** stringobject.c 6 Aug 2002 16:58:21 -0000 2.175
--- stringobject.c 11 Aug 2002 04:24:11 -0000 2.176
***************
*** 3312,3315 ****
--- 3312,3321 ----
return -1;
}
+ if (x < 0 && type != 'd' && type != 'i') {
+ if (PyErr_Warn(PyExc_DeprecationWarning,
+ "%u/%o/%x/%X of negative int will return "
+ "a signed string in Python 2.4 and up") < 0)
+ return -1;
+ }
if (prec < 0)
prec = 1;
Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.160
retrieving revision 2.161
diff -C2 -d -r2.160 -r2.161
*** unicodeobject.c 9 Aug 2002 15:36:48 -0000 2.160
--- unicodeobject.c 11 Aug 2002 04:24:11 -0000 2.161
***************
*** 5220,5223 ****
--- 5220,5227 ----
}
+ /* XXX To save some code duplication, formatfloat/long/int could have been
+ shared with stringobject.c, converting from 8-bit to Unicode after the
+ formatting is done. */
+
static int
formatfloat(Py_UNICODE *buf,
***************
*** 5295,5298 ****
--- 5299,5308 ----
if (x == -1 && PyErr_Occurred())
return -1;
+ if (x < 0 && type != 'd' && type != 'i') {
+ if (PyErr_Warn(PyExc_DeprecationWarning,
+ "%u/%o/%x/%X of negative int will return "
+ "a signed string in Python 2.4 and up") < 0)
+ return -1;
+ }
if (prec < 0)
prec = 1;