diff options
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/intobject.c | 42 | ||||
-rw-r--r-- | Objects/stringobject.c | 36 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 36 |
3 files changed, 50 insertions, 64 deletions
diff --git a/Objects/intobject.c b/Objects/intobject.c index b97e2bc..47acbff 100644 --- a/Objects/intobject.c +++ b/Objects/intobject.c @@ -278,7 +278,6 @@ 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, @@ -292,7 +291,7 @@ PyInt_FromString(char *s, char **pend, int base) if (base == 0 && s[0] == '0') { x = (long) PyOS_strtoul(s, &end, base); if (x < 0) - warn = 1; + return PyLong_FromString(s, pend, base); } else x = PyOS_strtol(s, &end, base); @@ -312,11 +311,6 @@ 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); @@ -766,18 +760,13 @@ int_lshift(PyIntObject *v, PyIntObject *w) if (a == 0 || b == 0) return int_pos(v); if (b >= LONG_BIT) { - if (PyErr_Warn(PyExc_FutureWarning, - "x<<y losing bits or changing sign " - "will return a long in Python 2.4 and up") < 0) - return NULL; - return PyInt_FromLong(0L); + return PyNumber_Lshift(PyLong_FromLong(PyInt_AS_LONG(v)), + PyLong_FromLong(PyInt_AS_LONG(w))); } c = a << b; if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) { - if (PyErr_Warn(PyExc_FutureWarning, - "x<<y losing bits or changing sign " - "will return a long in Python 2.4 and up") < 0) - return NULL; + return PyNumber_Lshift(PyLong_FromLong(PyInt_AS_LONG(v)), + PyLong_FromLong(PyInt_AS_LONG(w))); } return PyInt_FromLong(c); } @@ -868,13 +857,9 @@ int_oct(PyIntObject *v) { char buf[100]; long x = v -> ob_ival; - if (x < 0) { - if (PyErr_Warn(PyExc_FutureWarning, - "hex()/oct() of negative int will return " - "a signed string in Python 2.4 and up") < 0) - return NULL; - } - if (x == 0) + if (x < 0) + PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x); + else if (x == 0) strcpy(buf, "0"); else PyOS_snprintf(buf, sizeof(buf), "0%lo", x); @@ -886,13 +871,10 @@ int_hex(PyIntObject *v) { char buf[100]; long x = v -> ob_ival; - if (x < 0) { - if (PyErr_Warn(PyExc_FutureWarning, - "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); + if (x < 0) + PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x); + else + PyOS_snprintf(buf, sizeof(buf), "0x%lx", x); return PyString_FromString(buf); } diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 739cc3e..d3351df 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -3565,6 +3565,7 @@ formatint(char *buf, size_t buflen, int flags, worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) + 1 + 1 = 24 */ char fmt[64]; /* plenty big enough! */ + char *sign; long x; x = PyInt_AsLong(v); @@ -3572,12 +3573,13 @@ formatint(char *buf, size_t buflen, int flags, PyErr_SetString(PyExc_TypeError, "int argument required"); return -1; } - if (x < 0 && type != 'd' && type != 'i') { - if (PyErr_Warn(PyExc_FutureWarning, - "%u/%o/%x/%X of negative int will return " - "a signed string in Python 2.4 and up") < 0) - return -1; + if (x < 0 && type == 'u') { + type = 'd'; } + if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) + sign = "-"; + else + sign = ""; if (prec < 0) prec = 1; @@ -3603,24 +3605,27 @@ formatint(char *buf, size_t buflen, int flags, * Note that this is the same approach as used in * formatint() in unicodeobject.c */ - PyOS_snprintf(fmt, sizeof(fmt), "0%c%%.%dl%c", - type, prec, type); + PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", + sign, type, prec, type); } else { - PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%dl%c", - (flags&F_ALT) ? "#" : "", + PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", + sign, (flags&F_ALT) ? "#" : "", prec, type); } - /* buf = '+'/'-'/'0'/'0x' + '[0-9]'*max(prec, len(x in octal)) - * worst case buf = '0x' + [0-9]*prec, where prec >= 11 + /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) + * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 */ - if (buflen <= 13 || buflen <= (size_t)2 + (size_t)prec) { + if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { PyErr_SetString(PyExc_OverflowError, "formatted integer is too long (precision too large?)"); return -1; } - PyOS_snprintf(buf, buflen, fmt, x); + if (sign[0]) + PyOS_snprintf(buf, buflen, fmt, -x); + else + PyOS_snprintf(buf, buflen, fmt, x); return strlen(buf); } @@ -3907,8 +3912,6 @@ PyString_Format(PyObject *format, PyObject *args) prec, c, &pbuf, &len); if (!temp) goto error; - /* unbounded ints can always produce - a sign character! */ sign = 1; } else { @@ -3918,8 +3921,7 @@ PyString_Format(PyObject *format, PyObject *args) flags, prec, c, v); if (len < 0) goto error; - /* only d conversion is signed */ - sign = c == 'd'; + sign = 1; } if (flags & F_ZERO) fill = '0'; diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index c950f8b..f87d749 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -6177,24 +6177,26 @@ formatint(Py_UNICODE *buf, * = 24 */ char fmt[64]; /* plenty big enough! */ + char *sign; long x; x = PyInt_AsLong(v); if (x == -1 && PyErr_Occurred()) return -1; - if (x < 0 && type != 'd' && type != 'i') { - if (PyErr_Warn(PyExc_FutureWarning, - "%u/%o/%x/%X of negative int will return " - "a signed string in Python 2.4 and up") < 0) - return -1; + if (x < 0 && type == 'u') { + type = 'd'; } + if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) + sign = "-"; + else + sign = ""; if (prec < 0) prec = 1; - /* buf = '+'/'-'/'0'/'0x' + '[0-9]'*max(prec,len(x in octal)) - * worst case buf = '0x' + [0-9]*prec, where prec >= 11 + /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) + * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 */ - if (buflen <= 13 || buflen <= (size_t)2 + (size_t)prec) { + if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { PyErr_SetString(PyExc_OverflowError, "formatted integer is too long (precision too large?)"); return -1; @@ -6222,15 +6224,18 @@ formatint(Py_UNICODE *buf, * Note that this is the same approach as used in * formatint() in stringobject.c */ - PyOS_snprintf(fmt, sizeof(fmt), "0%c%%.%dl%c", - type, prec, type); + PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", + sign, type, prec, type); } else { - PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%dl%c", - (flags&F_ALT) ? "#" : "", + PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", + sign, (flags&F_ALT) ? "#" : "", prec, type); } - return usprintf(buf, fmt, x); + if (sign[0]) + return usprintf(buf, fmt, -x); + else + return usprintf(buf, fmt, x); } static int @@ -6566,8 +6571,6 @@ PyObject *PyUnicode_Format(PyObject *format, goto onError; pbuf = PyUnicode_AS_UNICODE(temp); len = PyUnicode_GET_SIZE(temp); - /* unbounded ints can always produce - a sign character! */ sign = 1; } else { @@ -6576,8 +6579,7 @@ PyObject *PyUnicode_Format(PyObject *format, flags, prec, c, v); if (len < 0) goto onError; - /* only d conversion is signed */ - sign = c == 'd'; + sign = 1; } if (flags & F_ZERO) fill = '0'; |