mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
This commit is contained in:
parent
1bcffef959
commit
e38051db87
3 changed files with 41 additions and 5 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
|
@ -717,6 +717,13 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
|
|||
"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;
|
||||
sign = 1;
|
||||
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue