]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Make int("...") return a long if an int would overflow.
authorWalter Dörwald <walter@livinglogic.de>
Wed, 6 Nov 2002 16:15:14 +0000 (16:15 +0000)
committerWalter Dörwald <walter@livinglogic.de>
Wed, 6 Nov 2002 16:15:14 +0000 (16:15 +0000)
Also remove the 512 character limitation for int(u"...") and long(u"...").

This closes SF bug #629989.

Lib/test/test_b1.py
Objects/intobject.c
Objects/longobject.c

index 65285ee7377f26df7251ea3a7caafdeacdde3a9a..9e6c8d5f82fe5163ee4c0eafd29c68de9e84cac7 100644 (file)
@@ -435,10 +435,8 @@ if int(s)+1 != -sys.maxint:
     raise TestFailed, "int(%s)" % `s`
 try:
     int(s[1:])
-except ValueError:
-    pass
-else:
-    raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError"
+except:
+    raise TestFailed, "int(%s)" % `s[1:]` + " should return long"
 try:
     int(1e100)
 except OverflowError:
@@ -468,9 +466,12 @@ try: int('53', 40)
 except ValueError: pass
 else: raise TestFailed("int('53', 40) didn't raise ValueError")
 
-try: int('1' * 512)
-except ValueError: pass
-else: raise TestFailed("int('1' * 512) didn't raise ValueError")
+try: int('1' * 600)
+except: raise TestFailed("int('1' * 600) didn't return long")
+
+if have_unicode:
+       try: int(unichr(0x661) * 600)
+       except: raise TestFailed("int('\\u0661' * 600) didn't return long")
 
 try: int(1, 12)
 except TypeError: pass
index 7404e980836b493bb648d6691e986d68f98a0a01..e33908591cf945322a6b6d798d4d1aeffe98ce77 100644 (file)
@@ -208,10 +208,9 @@ PyInt_FromString(char *s, char **pend, int base)
                return NULL;
        }
        else if (errno != 0) {
-               PyOS_snprintf(buffer, sizeof(buffer),
-                             "int() literal too large: %.200s", s);
-               PyErr_SetString(PyExc_ValueError, buffer);
-               return NULL;
+               if (err_ovf("string/unicode conversion"))
+                       return NULL;
+               return PyLong_FromString(s, pend, base);
        }
        if (pend)
                *pend = end;
@@ -222,16 +221,19 @@ PyInt_FromString(char *s, char **pend, int base)
 PyObject *
 PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
 {
-       char buffer[256];
+       PyObject *result;
+       char *buffer = PyMem_MALLOC(length+1);
 
-       if (length >= sizeof(buffer)) {
-               PyErr_SetString(PyExc_ValueError,
-                               "int() literal too large to convert");
+       if (buffer == NULL)
                return NULL;
-       }
-       if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
+
+       if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
+               PyMem_FREE(buffer);
                return NULL;
-       return PyInt_FromString(buffer, NULL, base);
+       }
+       result = PyInt_FromString(buffer, NULL, base);
+       PyMem_FREE(buffer);
+       return result;
 }
 #endif
 
index 35d121120d0e90bf978cb15c8660332fa6ede22f..7374fce9dd9242b27d2ed32d11d816ac2db373ff 100644 (file)
@@ -1123,17 +1123,19 @@ PyLong_FromString(char *str, char **pend, int base)
 PyObject *
 PyLong_FromUnicode(Py_UNICODE *u, int length, int base)
 {
-       char buffer[256];
+       PyObject *result;
+       char *buffer = PyMem_MALLOC(length+1);
 
-       if (length >= sizeof(buffer)) {
-               PyErr_SetString(PyExc_ValueError,
-                               "long() literal too large to convert");
-               return NULL;
-       }
-       if (PyUnicode_EncodeDecimal(u, length, buffer, NULL))
+       if (buffer == NULL)
                return NULL;
 
-       return PyLong_FromString(buffer, NULL, base);
+       if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
+               PyMem_FREE(buffer);
+               return NULL;
+       }
+       result = PyLong_FromString(buffer, NULL, base);
+       PyMem_FREE(buffer);
+       return result;
 }
 #endif