]> granicus.if.org Git - python/commitdiff
Issue a warning when int('0...', 0) returns an int with the sign
authorGuido van Rossum <guido@python.org>
Wed, 12 Feb 2003 20:48:22 +0000 (20:48 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 12 Feb 2003 20:48:22 +0000 (20:48 +0000)
folded; this will change in Python 2.4.  On a 32-bit machine, this
happens for 0x80000000 through 0xffffffff, and for octal constants in
the same value range.  No warning is issued if an explicit base is
given, *or* if the string contains a sign (since in those cases no
sign folding ever happens).

Misc/NEWS
Objects/intobject.c

index 8a6e902f06e6e797cad56e925e646bc847389a73..f1b3a8b5bcd48ba46301fc43ed5bd31cd7d03fb2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,7 +10,7 @@ What's New in Python 2.3 alpha 2?
 *Release date: XX-XXX-2003*
 
 Core and builtins
------------------
+----------------
 
 - Through a bytecode optimizer bug (and I bet you didn't even know
   Python *had* a bytecode optimizer :-), "unsigned" hex/oct constants
@@ -24,6 +24,13 @@ Core and builtins
   value, but according to PEP 237 it really needs to be 1 now.  This
   will be backported to Python 2.2.3 a well.  (SF #660455)
 
+- int(s, base) sometimes sign-folds hex and oct constants; it only
+  does this when base is 0 and s.strip() starts with a '0'.  When the
+  sign is actually folded, as in int("0xffffffff", 0) on a 32-bit
+  machine, which returns -1, a FutureWarning is now issued; in Python
+  2.4, this will return 4294967295L, as do int("+0xffffffff", 0) and
+  int("0xffffffff", 16) right now.  (PEP 347)
+
 - super(X, x): x may now be a proxy for an X instance, i.e.
   issubclass(x.__class__, X) but not issubclass(type(x), X).
 
index 544e663dac4c37519ae75db1d20ed704cc167a54..fee7e4e38deac67f911f327f42aef2488354eac8 100644 (file)
@@ -187,17 +187,22 @@ PyInt_FromString(char *s, char **pend, int base)
        char *end;
        long x;
        char buffer[256]; /* For errors */
+       int warn = 0;
 
        if ((base != 0 && base < 2) || base > 36) {
-               PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36");
+               PyErr_SetString(PyExc_ValueError,
+                               "int() base must be >= 2 and <= 36");
                return NULL;
        }
 
        while (*s && isspace(Py_CHARMASK(*s)))
                s++;
        errno = 0;
-       if (base == 0 && s[0] == '0')
+       if (base == 0 && s[0] == '0') {
                x = (long) PyOS_strtoul(s, &end, base);
+               if (x < 0)
+                       warn = 1;
+       }
        else
                x = PyOS_strtol(s, &end, base);
        if (end == s || !isalnum(Py_CHARMASK(end[-1])))
@@ -216,6 +221,11 @@ PyInt_FromString(char *s, char **pend, int base)
                        return NULL;
                return PyLong_FromString(s, pend, base);
        }
+       if (warn) {
+               if (PyErr_Warn(PyExc_FutureWarning,
+                       "int('0...', 0): sign will change in Python 2.4") < 0)
+                       return NULL;
+       }
        if (pend)
                *pend = end;
        return PyInt_FromLong(x);