Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 077f271

Browse files
committed
SF bug 543840: complex(string) accepts strings with \0
complex_subtype_from_string(): this stopped parsing at the first 0 byte, as if that were the end of the input string. Bugfix candidate.
1 parent 518d261 commit 077f271

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

Lib/test/test_b1.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,29 @@ def __call__(self): pass
124124
if complex(0.0, 3.14) != 3.14j: raise TestFailed, 'complex(0.0, 3.14)'
125125
if complex("1") != 1+0j: raise TestFailed, 'complex("1")'
126126
if complex("1j") != 1j: raise TestFailed, 'complex("1j")'
127+
127128
try: complex("1", "1")
128129
except TypeError: pass
129130
else: raise TestFailed, 'complex("1", "1")'
131+
130132
try: complex(1, "1")
131133
except TypeError: pass
132134
else: raise TestFailed, 'complex(1, "1")'
135+
133136
if complex(" 3.14+J ") != 3.14+1j: raise TestFailed, 'complex(" 3.14+J )"'
134137
if have_unicode:
135138
if complex(unicode(" 3.14+J ")) != 3.14+1j:
136139
raise TestFailed, 'complex(u" 3.14+J )"'
140+
141+
# SF bug 543840: complex(string) accepts strings with \0
142+
# Fixed in 2.3.
143+
try:
144+
complex('1+1j\0j')
145+
except ValueError:
146+
pass
147+
else:
148+
raise TestFailed("complex('1+1j\0j') should have raised ValueError")
149+
137150
class Z:
138151
def __complex__(self): return 3.14j
139152
z = Z()

Objects/complexobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
787787

788788
} /* end of switch */
789789

790-
} while (*s!='\0' && !sw_error);
790+
} while (s - start < len && !sw_error);
791791

792792
if (sw_error) {
793793
PyErr_SetString(PyExc_ValueError,

0 commit comments

Comments
 (0)