[Python-checkins] cpython (merge 3.6 -> default): Issue #28203: Merge from 3.6

mark.dickinson python-checkins at python.org
Sat Sep 24 10:29:21 EDT 2016


https://hg.python.org/cpython/rev/9790bc211107
changeset:   104053:9790bc211107
parent:      104050:3b608f150f5f
parent:      104052:a2d93e6bcbcf
user:        Mark Dickinson <dickinsm at gmail.com>
date:        Sat Sep 24 15:29:07 2016 +0100
summary:
  Issue #28203: Merge from 3.6

files:
  Lib/test/test_complex.py |   8 ++++++++
  Misc/ACKS                |   1 +
  Misc/NEWS                |   3 +++
  Objects/complexobject.c  |  23 +++++++++++++++++------
  4 files changed, 29 insertions(+), 6 deletions(-)


diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py
--- a/Lib/test/test_complex.py
+++ b/Lib/test/test_complex.py
@@ -328,6 +328,14 @@
         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")
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1369,6 +1369,7 @@
 Mark Shannon
 Ha Shao
 Richard Shapiro
+Soumya Sharma
 Varun Sharma
 Daniel Shaulov
 Vlad Shcherbina
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #28203: Fix incorrect type in complex(1.0, {2:3}) error message.
+  Patch by Soumya Sharma.
+
 - Issue #28086: Single var-positional argument of tuple subtype was passed
   unscathed to the C-defined function.  Now it is converted to exact tuple.
 
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -965,18 +965,29 @@
     }
 
     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

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list