[Python-checkins] r54247 - in python/trunk: Lib/test/test_complex.py Misc/NEWS Objects/complexobject.c
collin.winter
python-checkins at python.org
Fri Mar 9 21:33:17 CET 2007
Author: collin.winter
Date: Fri Mar 9 21:33:07 2007
New Revision: 54247
Modified:
python/trunk/Lib/test/test_complex.py
python/trunk/Misc/NEWS
python/trunk/Objects/complexobject.c
Log:
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Modified: python/trunk/Lib/test/test_complex.py
==============================================================================
--- python/trunk/Lib/test/test_complex.py (original)
+++ python/trunk/Lib/test/test_complex.py Fri Mar 9 21:33:07 2007
@@ -215,6 +215,8 @@
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 @@
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 @@
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)
Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS (original)
+++ python/trunk/Misc/NEWS Fri Mar 9 21:33:07 2007
@@ -12,6 +12,10 @@
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.
Modified: python/trunk/Objects/complexobject.c
==============================================================================
--- python/trunk/Objects/complexobject.c (original)
+++ python/trunk/Objects/complexobject.c Fri Mar 9 21:33:07 2007
@@ -672,7 +672,7 @@
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 @@
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 @@
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 @@
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 @@
} 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;
More information about the Python-checkins
mailing list