]> granicus.if.org Git - python/commitdiff
Issue #16741: Fix an error reporting in int().
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 3 Aug 2013 18:14:05 +0000 (21:14 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 3 Aug 2013 18:14:05 +0000 (21:14 +0300)
1  2 
Include/longobject.h
Lib/test/test_int.py
Misc/NEWS
Objects/abstract.c
Objects/longobject.c

Simple merge
Simple merge
diff --cc Misc/NEWS
index 10c0470f39b505d94f07237ad41d8b92e5988cc9,10d908cd6df639963ee30e37780869e3e5fb5ed0..61857a947ede6531b25d49276f2bcd09405338c4
+++ b/Misc/NEWS
@@@ -10,16 -12,10 +10,18 @@@ What's New in Python 3.4.0 Alpha 1
  Core and Builtins
  -----------------
  
+ - Issue #16741: Fix an error reporting in int().
  - Issue #17899: Fix rare file descriptor leak in os.listdir().
  
 +- Issue #9035: ismount now recognises volumes mounted below a drive root
 +  on Windows. Original patch by Atsuo Ishimoto.
 +
 +- Issue #18214: Improve finalization of Python modules to avoid setting
 +  their globals to None, in most cases.
 +
 +- Issue #18112: PEP 442 implementation (safe object finalization).
 +
  - Issue #18552: Check return value of PyArena_AddPyObject() in
    obj2ast_object().
  
Simple merge
index b47be40627221f5a6c6da35c2bf98666fc100276,30ffc94d416dc32a3b3d315ee43c60819bc6e39d..e0d641a0168fa3bfb5324d137aeb1b2b83a83989
@@@ -2274,12 -2292,37 +2287,37 @@@ digit beyond the first
      if (strobj == NULL)
          return NULL;
      PyErr_Format(PyExc_ValueError,
--                 "invalid literal for int() with base %d: %R",
++                 "invalid literal for int() with base %d: %.200R",
                   base, strobj);
      Py_DECREF(strobj);
      return NULL;
  }
  
 -                     "invalid literal for int() with base %d: %R",
+ /* Since PyLong_FromString doesn't have a length parameter,
+  * check here for possible NULs in the string.
+  *
+  * Reports an invalid literal as a bytes object.
+  */
+ PyObject *
+ _PyLong_FromBytes(const char *s, Py_ssize_t len, int base)
+ {
+     PyObject *result, *strobj;
+     char *end = NULL;
+     result = PyLong_FromString((char*)s, &end, base);
+     if (end == NULL || (result != NULL && end == s + len))
+         return result;
+     Py_XDECREF(result);
+     strobj = PyBytes_FromStringAndSize(s, Py_MIN(len, 200));
+     if (strobj != NULL) {
+         PyErr_Format(PyExc_ValueError,
++                     "invalid literal for int() with base %d: %.200R",
+                      base, strobj);
+         Py_DECREF(strobj);
+     }
+     return NULL;
+ }
  PyObject *
  PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
  {
  PyObject *
  PyLong_FromUnicodeObject(PyObject *u, int base)
  {
-     PyObject *result;
-     PyObject *asciidig;
-     char *buffer, *end;
 -    PyObject *result, *asciidig, *strobj;
++    PyObject *result, *asciidig;
+     char *buffer, *end = NULL;
      Py_ssize_t buflen;
  
      asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u);
      buffer = PyUnicode_AsUTF8AndSize(asciidig, &buflen);
      if (buffer == NULL) {
          Py_DECREF(asciidig);
-         return NULL;
+         if (!PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
+             return NULL;
      }
-     result = PyLong_FromString(buffer, &end, base);
-     if (result != NULL && end != buffer + buflen) {
-         PyErr_SetString(PyExc_ValueError,
-                         "null byte in argument for int()");
-         Py_DECREF(result);
-         result = NULL;
+     else {
+         result = PyLong_FromString(buffer, &end, base);
+         if (end == NULL || (result != NULL && end == buffer + buflen)) {
+             Py_DECREF(asciidig);
+             return result;
+         }
+         Py_DECREF(asciidig);
+         Py_XDECREF(result);
      }
-     Py_DECREF(asciidig);
-     return result;
 -    strobj = PySequence_GetSlice(u, 0, 200);
 -    if (strobj != NULL) {
 -        PyErr_Format(PyExc_ValueError,
 -                     "invalid literal for int() with base %d: %R",
 -                     base, strobj);
 -        Py_DECREF(strobj);
 -    }
++    PyErr_Format(PyExc_ValueError,
++                 "invalid literal for int() with base %d: %.200R",
++                 base, u);
+     return NULL;
  }
  
  /* forward */