Skip to content

Commit 613f8e5

Browse files
committed
Issue #28203: Fix incorrect type in error message from complex(1.0, {2:3}). Patch by Soumya Sharma.
1 parent 8609cda commit 613f8e5

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

Lib/test/test_complex.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ def split_zeros(x):
326326
self.assertRaises(ValueError, complex, "1e1ej")
327327
self.assertRaises(ValueError, complex, "1e++1ej")
328328
self.assertRaises(ValueError, complex, ")1+2j(")
329+
self.assertRaisesRegex(
330+
TypeError,
331+
"first argument must be a string or a number, not 'dict'",
332+
complex, {1:2}, 1)
333+
self.assertRaisesRegex(
334+
TypeError,
335+
"second argument must be a number, not 'dict'",
336+
complex, 1, {1:2})
329337
# the following three are accepted by Python 2.6
330338
self.assertRaises(ValueError, complex, "1..1j")
331339
self.assertRaises(ValueError, complex, "1.11.1j")

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,7 @@ Daniel Shahaf
13491349
Mark Shannon
13501350
Ha Shao
13511351
Richard Shapiro
1352+
Soumya Sharma
13521353
Varun Sharma
13531354
Daniel Shaulov
13541355
Vlad Shcherbina

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #28203: Fix incorrect type in error message from
14+
``complex(1.0, {2:3})``. Patch by Soumya Sharma.
15+
1316
- Issue #27955: Fallback on reading /dev/urandom device when the getrandom()
1417
syscall fails with EPERM, for example when blocked by SECCOMP.
1518

Objects/complexobject.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -954,18 +954,29 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
954954
}
955955

956956
nbr = r->ob_type->tp_as_number;
957-
if (i != NULL)
958-
nbi = i->ob_type->tp_as_number;
959-
if (nbr == NULL || nbr->nb_float == NULL ||
960-
((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
957+
if (nbr == NULL || nbr->nb_float == NULL) {
961958
PyErr_Format(PyExc_TypeError,
962-
"complex() argument must be a string or a number, not '%.200s'",
963-
Py_TYPE(r)->tp_name);
959+
"complex() first argument must be a string or a number, "
960+
"not '%.200s'",
961+
Py_TYPE(r)->tp_name);
964962
if (own_r) {
965963
Py_DECREF(r);
966964
}
967965
return NULL;
968966
}
967+
if (i != NULL) {
968+
nbi = i->ob_type->tp_as_number;
969+
if (nbi == NULL || nbi->nb_float == NULL) {
970+
PyErr_Format(PyExc_TypeError,
971+
"complex() second argument must be a number, "
972+
"not '%.200s'",
973+
Py_TYPE(i)->tp_name);
974+
if (own_r) {
975+
Py_DECREF(r);
976+
}
977+
return NULL;
978+
}
979+
}
969980

970981
/* If we get this far, then the "real" and "imag" parts should
971982
both be treated as numbers, and the constructor should return a

0 commit comments

Comments
 (0)