]> granicus.if.org Git - python/commitdiff
Patch #1491866: change the complex() constructor to allow parthensized forms. This...
authorCollin Winter <collinw@gmail.com>
Fri, 9 Mar 2007 20:33:07 +0000 (20:33 +0000)
committerCollin Winter <collinw@gmail.com>
Fri, 9 Mar 2007 20:33:07 +0000 (20:33 +0000)
Lib/test/test_complex.py
Misc/NEWS
Objects/complexobject.c

index 0d42bd2b6868ffa4bf1c67aecd214e780a05dfc2..3035c2d52a616c81cc159b2541414f52740381a3 100644 (file)
@@ -215,6 +215,8 @@ class ComplexTest(unittest.TestCase):
         self.assertAlmostEqual(complex(),  0)
         self.assertAlmostEqual(complex("-1"), -1)
         self.assertAlmostEqual(complex("+1"), +1)
+        self.assertAlmostEqual(complex("(1+2j)"), 1+2j)
+        self.assertAlmostEqual(complex("(1.3+2.2j)"), 1.3+2.2j)
 
         class complex2(complex): pass
         self.assertAlmostEqual(complex(complex2(1+1j)), 1+1j)
@@ -244,12 +246,17 @@ class ComplexTest(unittest.TestCase):
         self.assertRaises(ValueError, complex, "")
         self.assertRaises(TypeError, complex, None)
         self.assertRaises(ValueError, complex, "\0")
+        self.assertRaises(ValueError, complex, "3\09")
         self.assertRaises(TypeError, complex, "1", "2")
         self.assertRaises(TypeError, complex, "1", 42)
         self.assertRaises(TypeError, complex, 1, "2")
         self.assertRaises(ValueError, complex, "1+")
         self.assertRaises(ValueError, complex, "1+1j+1j")
         self.assertRaises(ValueError, complex, "--")
+        self.assertRaises(ValueError, complex, "(1+2j")
+        self.assertRaises(ValueError, complex, "1+2j)")
+        self.assertRaises(ValueError, complex, "1+(2j)")
+        self.assertRaises(ValueError, complex, "(1+2j)123")
         if test_support.have_unicode:
             self.assertRaises(ValueError, complex, unicode("1"*500))
             self.assertRaises(ValueError, complex, unicode("x"))
@@ -312,6 +319,11 @@ class ComplexTest(unittest.TestCase):
 
         self.assertNotEqual(repr(-(1+0j)), '(-1+-0j)')
 
+        self.assertEqual(1-6j,complex(repr(1-6j)))
+        self.assertEqual(1+6j,complex(repr(1+6j)))
+        self.assertEqual(-6j,complex(repr(-6j)))
+        self.assertEqual(6j,complex(repr(6j)))
+
     def test_neg(self):
         self.assertEqual(-(1+6j), -1-6j)
 
index 89304b07697150e94ee7bffef3cf46b24149793d..a98accd3c231b63d7909572236d06ac487c38ff5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 2.6 alpha 1?
 Core and builtins
 -----------------
 
+- Patch #1491866: change the complex() constructor to allow parthensized
+  forms. This means complex(repr(x)) now works instead of raising a
+  ValueError.
+
 - Patch #703779: unset __file__ in __main__ after running a file. This
   makes the filenames the warning module prints much more sensible when
   a PYTHONSTARTUP file is used.
index 4de1fb65871ec899f803f7425e49d4d17026d2b2..1403de2e7a9694f523b42e99818484b0d1d519fb 100644 (file)
@@ -672,7 +672,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
        const char *s, *start;
        char *end;
        double x=0.0, y=0.0, z;
-       int got_re=0, got_im=0, done=0;
+       int got_re=0, got_im=0, got_bracket=0, done=0;
        int digit_or_dot;
        int sw_error=0;
        int sign;
@@ -712,10 +712,17 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
        start = s;
        while (*s && isspace(Py_CHARMASK(*s)))
                s++;
-       if (s[0] == '\0') {
+    if (s[0] == '\0') {
                PyErr_SetString(PyExc_ValueError,
                                "complex() arg is an empty string");
                return NULL;
+    }
+       if (s[0] == '(') {
+               /* Skip over possible bracket from repr(). */
+               got_bracket = 1;
+               s++;
+               while (*s && isspace(Py_CHARMASK(*s)))
+                       s++;
        }
 
        z = -1.0;
@@ -734,13 +741,26 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
                        if(!done) sw_error=1;
                        break;
 
+               case ')':
+                       if (!got_bracket || !(got_re || got_im)) {
+                               sw_error=1;
+                               break;
+                       }
+                       got_bracket=0;
+                       done=1;
+                       s++;
+                       while (*s && isspace(Py_CHARMASK(*s)))
+                               s++;
+                       if (*s) sw_error=1;
+                       break;
+
                case '-':
                        sign = -1;
                                /* Fallthrough */
                case '+':
                        if (done)  sw_error=1;
                        s++;
-                       if  (  *s=='\0'||*s=='+'||*s=='-'  ||
+                       if  (  *s=='\0'||*s=='+'||*s=='-'||*s==')'||
                               isspace(Py_CHARMASK(*s))  )  sw_error=1;
                        break;
 
@@ -766,7 +786,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
                        if (isspace(Py_CHARMASK(*s))) {
                                while (*s && isspace(Py_CHARMASK(*s)))
                                        s++;
-                               if (s[0] != '\0')
+                               if (*s && *s != ')')
                                        sw_error=1;
                                else
                                        done = 1;
@@ -812,7 +832,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
 
        } while (s - start < len && !sw_error);
 
-       if (sw_error) {
+       if (sw_error || got_bracket) {
                PyErr_SetString(PyExc_ValueError,
                                "complex() arg is a malformed string");
                return NULL;