diff -r b92296e62450 Doc/whatsnew/whatsnew25.tex --- a/Doc/whatsnew/whatsnew25.tex Fri May 19 09:10:08 2006 +0200 +++ b/Doc/whatsnew/whatsnew25.tex Sat May 20 13:15:55 2006 +0200 @@ -1096,6 +1096,11 @@ class C(): \end{verbatim} (Implemented by Brett Cannon.) +\item The constructor for \code{complex} objects now allows for a +bracketed expression. This makes \code{complex(repr(x))} work for +all complex \var{x}, consistent with \code{int(repr(x))} and +\code{float(repr(x))}. (Implemented by Heiko Wundram.) + \end{itemize} diff -r b92296e62450 Lib/test/test_complex.py --- a/Lib/test/test_complex.py Fri May 19 09:10:08 2006 +0200 +++ b/Lib/test/test_complex.py Sat May 20 13:15:55 2006 +0200 @@ -215,6 +215,7 @@ 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) class complex2(complex): pass self.assertAlmostEqual(complex(complex2(1+1j)), 1+1j) @@ -250,6 +251,10 @@ class ComplexTest(unittest.TestCase): 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")) @@ -311,6 +316,11 @@ class ComplexTest(unittest.TestCase): self.assertEqual(repr(1-6j), '(1-6j)') 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) diff -r b92296e62450 Objects/complexobject.c --- a/Objects/complexobject.c Fri May 19 09:10:08 2006 +0200 +++ b/Objects/complexobject.c Sat May 20 13:15:55 2006 +0200 @@ -672,7 +672,7 @@ complex_subtype_from_string(PyTypeObject 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,12 @@ complex_subtype_from_string(PyTypeObject start = s; while (*s && isspace(Py_CHARMASK(*s))) s++; - 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 +736,26 @@ complex_subtype_from_string(PyTypeObject 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 +781,7 @@ complex_subtype_from_string(PyTypeObject 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 +827,7 @@ complex_subtype_from_string(PyTypeObject } 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;