From e38051db87b92efeb09373bea3900cde073cda96 Mon Sep 17 00:00:00 2001 From: Collin Winter Date: Fri, 9 Mar 2007 20:33:07 +0000 Subject: Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError. --- Lib/test/test_complex.py | 12 ++++++++++++ Misc/NEWS | 4 ++++ Objects/complexobject.c | 30 +++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 0d42bd2b..3035c2d 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -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) diff --git a/Misc/NEWS b/Misc/NEWS index 89304b0..a98accd 100644 --- 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. diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 4de1fb6..1403de2 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -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; -- cgit v0.12