]> granicus.if.org Git - python/commitdiff
Remove length limitation on string arguments to complex()
authorMark Dickinson <dickinsm@gmail.com>
Mon, 26 Oct 2009 21:51:18 +0000 (21:51 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Mon, 26 Oct 2009 21:51:18 +0000 (21:51 +0000)
Lib/test/test_complex.py
Misc/NEWS
Objects/complexobject.c

index faa7e1f6be50452e6bdbb226bb298dce603bd239..9341016f2177e9ec96e1971cab3e8513afff7561 100644 (file)
@@ -284,7 +284,6 @@ class ComplexTest(unittest.TestCase):
         self.assertRaises(ValueError, complex, "1+2j)")
         self.assertRaises(ValueError, complex, "1+(2j)")
         self.assertRaises(ValueError, complex, "(1+2j)123")
-        self.assertRaises(ValueError, complex, "1"*500)
         self.assertRaises(ValueError, complex, "x")
         self.assertRaises(ValueError, complex, "1j+2")
         self.assertRaises(ValueError, complex, "1e1ej")
@@ -295,6 +294,9 @@ class ComplexTest(unittest.TestCase):
         self.assertRaises(ValueError, complex, "1.11.1j")
         self.assertRaises(ValueError, complex, "1e1.1j")
 
+        # check that complex accepts long unicode strings
+        self.assertEqual(type(complex("1"*500)), complex)
+
         class EvilExc(Exception):
             pass
 
index c54670002e995127681acd738f8b3224068ec909..bb5e4cc7e82562d5555a750eca044566d06deddd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 3.2 Alpha 1?
 Core and Builtins
 -----------------
 
+- Remove length limitation when constructing a complex number from a string.
+
 - Issue #1087418: Boost performance of bitwise operations for longs.
 
 - Support for AtheOS has been completely removed from the code base. It was
index ccee38229c2b19f4d9b78e05a1faa50df7d0af53..b541308ae3e70847f73f1390d7151a30a0bbcbdc 100644 (file)
@@ -740,20 +740,20 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
        char *end;
        double x=0.0, y=0.0, z;
        int got_bracket=0;
-       char s_buffer[256];
+       char *s_buffer = NULL;
        Py_ssize_t len;
 
        if (PyUnicode_Check(v)) {
-               if (PyUnicode_GET_SIZE(v) >= (Py_ssize_t)sizeof(s_buffer)) {
-                       PyErr_SetString(PyExc_ValueError,
-                                "complex() literal too large to convert");
-                       return NULL;
-               }
+               s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v) + 1);
+               if (s_buffer == NULL)
+                       return PyErr_NoMemory();
                if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
                                            PyUnicode_GET_SIZE(v),
                                            s_buffer,
-                                           NULL))
+                                           NULL)) {
+                       PyMem_FREE(s_buffer);
                        return NULL;
+               }
                s = s_buffer;
                len = strlen(s);
        }
@@ -870,9 +870,13 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
        if (s-start != len)
                goto parse_error;
 
+       if (s_buffer)
+               PyMem_FREE(s_buffer);
        return complex_subtype_from_doubles(type, x, y);
 
   parse_error:
+       if (s_buffer)
+               PyMem_FREE(s_buffer);
        PyErr_SetString(PyExc_ValueError,
                        "complex() arg is a malformed string");
        return NULL;