]> granicus.if.org Git - python/commitdiff
Issue #28203: Fix incorrect type in error message from complex(1.0, {2:3}). Patch...
authorMark Dickinson <dickinsm@gmail.com>
Sat, 24 Sep 2016 14:26:36 +0000 (15:26 +0100)
committerMark Dickinson <dickinsm@gmail.com>
Sat, 24 Sep 2016 14:26:36 +0000 (15:26 +0100)
Lib/test/test_complex.py
Misc/ACKS
Misc/NEWS
Objects/complexobject.c

index 0ef9a7a1098e70d07a185b3866590dc22bee6f24..403ee3bc580f0a4d122b47fe7f058ca5081b860c 100644 (file)
@@ -326,6 +326,14 @@ class ComplexTest(unittest.TestCase):
         self.assertRaises(ValueError, complex, "1e1ej")
         self.assertRaises(ValueError, complex, "1e++1ej")
         self.assertRaises(ValueError, complex, ")1+2j(")
+        self.assertRaisesRegex(
+            TypeError,
+            "first argument must be a string or a number, not 'dict'",
+            complex, {1:2}, 1)
+        self.assertRaisesRegex(
+            TypeError,
+            "second argument must be a number, not 'dict'",
+            complex, 1, {1:2})
         # the following three are accepted by Python 2.6
         self.assertRaises(ValueError, complex, "1..1j")
         self.assertRaises(ValueError, complex, "1.11.1j")
index f590f6c6a097b7eb757b93463c52b30f4962871c..ed93e998610ca109d0718953e41d6991df9ccace 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1349,6 +1349,7 @@ Daniel Shahaf
 Mark Shannon
 Ha Shao
 Richard Shapiro
+Soumya Sharma
 Varun Sharma
 Daniel Shaulov
 Vlad Shcherbina
index cf97628edf1f7766b6f68fa38aa90f959863205d..8c9dcc36969f74caab6027cc9c8cda264287f4a9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ Release date: TBA
 Core and Builtins
 -----------------
 
+- Issue #28203: Fix incorrect type in error message from
+  ``complex(1.0, {2:3})``. Patch by Soumya Sharma.
+
 - Issue #27955: Fallback on reading /dev/urandom device when the getrandom()
   syscall fails with EPERM, for example when blocked by SECCOMP.
 
index a5bfb667c46e77f4cb4bd068fa595f73827f8936..d82c5eb9f1ea44e01a79381ef279bae69d7d6cf2 100644 (file)
@@ -954,18 +954,29 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     }
 
     nbr = r->ob_type->tp_as_number;
-    if (i != NULL)
-        nbi = i->ob_type->tp_as_number;
-    if (nbr == NULL || nbr->nb_float == NULL ||
-        ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
+    if (nbr == NULL || nbr->nb_float == NULL) {
         PyErr_Format(PyExc_TypeError,
-            "complex() argument must be a string or a number, not '%.200s'",
-            Py_TYPE(r)->tp_name);
+                     "complex() first argument must be a string or a number, "
+                     "not '%.200s'",
+                     Py_TYPE(r)->tp_name);
         if (own_r) {
             Py_DECREF(r);
         }
         return NULL;
     }
+    if (i != NULL) {
+        nbi = i->ob_type->tp_as_number;
+        if (nbi == NULL || nbi->nb_float == NULL) {
+            PyErr_Format(PyExc_TypeError,
+                         "complex() second argument must be a number, "
+                         "not '%.200s'",
+                         Py_TYPE(i)->tp_name);
+            if (own_r) {
+                Py_DECREF(r);
+            }
+            return NULL;
+        }
+    }
 
     /* If we get this far, then the "real" and "imag" parts should
        both be treated as numbers, and the constructor should return a