Skip to content

Commit 70069bd

Browse files
author
collin.winter
committed
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
git-svn-id: http://svn.python.org/projects/python/trunk@54247 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 24bdb06 commit 70069bd

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

Lib/test/test_complex.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ def __complex__(self): return self.value
215215
self.assertAlmostEqual(complex(), 0)
216216
self.assertAlmostEqual(complex("-1"), -1)
217217
self.assertAlmostEqual(complex("+1"), +1)
218+
self.assertAlmostEqual(complex("(1+2j)"), 1+2j)
219+
self.assertAlmostEqual(complex("(1.3+2.2j)"), 1.3+2.2j)
218220

219221
class complex2(complex): pass
220222
self.assertAlmostEqual(complex(complex2(1+1j)), 1+1j)
@@ -244,12 +246,17 @@ class complex2(complex): pass
244246
self.assertRaises(ValueError, complex, "")
245247
self.assertRaises(TypeError, complex, None)
246248
self.assertRaises(ValueError, complex, "\0")
249+
self.assertRaises(ValueError, complex, "3\09")
247250
self.assertRaises(TypeError, complex, "1", "2")
248251
self.assertRaises(TypeError, complex, "1", 42)
249252
self.assertRaises(TypeError, complex, 1, "2")
250253
self.assertRaises(ValueError, complex, "1+")
251254
self.assertRaises(ValueError, complex, "1+1j+1j")
252255
self.assertRaises(ValueError, complex, "--")
256+
self.assertRaises(ValueError, complex, "(1+2j")
257+
self.assertRaises(ValueError, complex, "1+2j)")
258+
self.assertRaises(ValueError, complex, "1+(2j)")
259+
self.assertRaises(ValueError, complex, "(1+2j)123")
253260
if test_support.have_unicode:
254261
self.assertRaises(ValueError, complex, unicode("1"*500))
255262
self.assertRaises(ValueError, complex, unicode("x"))
@@ -312,6 +319,11 @@ def test_repr(self):
312319

313320
self.assertNotEqual(repr(-(1+0j)), '(-1+-0j)')
314321

322+
self.assertEqual(1-6j,complex(repr(1-6j)))
323+
self.assertEqual(1+6j,complex(repr(1+6j)))
324+
self.assertEqual(-6j,complex(repr(-6j)))
325+
self.assertEqual(6j,complex(repr(6j)))
326+
315327
def test_neg(self):
316328
self.assertEqual(-(1+6j), -1-6j)
317329

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 2.6 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Patch #1491866: change the complex() constructor to allow parthensized
16+
forms. This means complex(repr(x)) now works instead of raising a
17+
ValueError.
18+
1519
- Patch #703779: unset __file__ in __main__ after running a file. This
1620
makes the filenames the warning module prints much more sensible when
1721
a PYTHONSTARTUP file is used.

Objects/complexobject.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
672672
const char *s, *start;
673673
char *end;
674674
double x=0.0, y=0.0, z;
675-
int got_re=0, got_im=0, done=0;
675+
int got_re=0, got_im=0, got_bracket=0, done=0;
676676
int digit_or_dot;
677677
int sw_error=0;
678678
int sign;
@@ -712,10 +712,17 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
712712
start = s;
713713
while (*s && isspace(Py_CHARMASK(*s)))
714714
s++;
715-
if (s[0] == '\0') {
715+
if (s[0] == '\0') {
716716
PyErr_SetString(PyExc_ValueError,
717717
"complex() arg is an empty string");
718718
return NULL;
719+
}
720+
if (s[0] == '(') {
721+
/* Skip over possible bracket from repr(). */
722+
got_bracket = 1;
723+
s++;
724+
while (*s && isspace(Py_CHARMASK(*s)))
725+
s++;
719726
}
720727

721728
z = -1.0;
@@ -734,13 +741,26 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
734741
if(!done) sw_error=1;
735742
break;
736743

744+
case ')':
745+
if (!got_bracket || !(got_re || got_im)) {
746+
sw_error=1;
747+
break;
748+
}
749+
got_bracket=0;
750+
done=1;
751+
s++;
752+
while (*s && isspace(Py_CHARMASK(*s)))
753+
s++;
754+
if (*s) sw_error=1;
755+
break;
756+
737757
case '-':
738758
sign = -1;
739759
/* Fallthrough */
740760
case '+':
741761
if (done) sw_error=1;
742762
s++;
743-
if ( *s=='\0'||*s=='+'||*s=='-' ||
763+
if ( *s=='\0'||*s=='+'||*s=='-'||*s==')'||
744764
isspace(Py_CHARMASK(*s)) ) sw_error=1;
745765
break;
746766

@@ -766,7 +786,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
766786
if (isspace(Py_CHARMASK(*s))) {
767787
while (*s && isspace(Py_CHARMASK(*s)))
768788
s++;
769-
if (s[0] != '\0')
789+
if (*s && *s != ')')
770790
sw_error=1;
771791
else
772792
done = 1;
@@ -812,7 +832,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
812832

813833
} while (s - start < len && !sw_error);
814834

815-
if (sw_error) {
835+
if (sw_error || got_bracket) {
816836
PyErr_SetString(PyExc_ValueError,
817837
"complex() arg is a malformed string");
818838
return NULL;

0 commit comments

Comments
 (0)