]> granicus.if.org Git - python/commitdiff
#17080: improve error message of float/complex when the wrong type is passed.
authorEzio Melotti <ezio.melotti@gmail.com>
Thu, 7 Nov 2013 17:18:34 +0000 (19:18 +0200)
committerEzio Melotti <ezio.melotti@gmail.com>
Thu, 7 Nov 2013 17:18:34 +0000 (19:18 +0200)
Lib/test/test_complex.py
Lib/test/test_float.py
Objects/complexobject.c
Objects/floatobject.c

index f80d7ac5226a731a2357569478797a93a929f70e..cd55375bdb98b6dc9898cc7a802bf79a4b976549 100644 (file)
@@ -303,6 +303,7 @@ class ComplexTest(unittest.TestCase):
         self.assertRaises(TypeError, float, 5+3j)
         self.assertRaises(ValueError, complex, "")
         self.assertRaises(TypeError, complex, None)
+        self.assertRaisesRegex(TypeError, "not 'NoneType'", complex, None)
         self.assertRaises(ValueError, complex, "\0")
         self.assertRaises(ValueError, complex, "3\09")
         self.assertRaises(TypeError, complex, "1", "2")
index 502292f61542d5a291ff7e713af93b85f525c7f5..5c2dc3ff3bcd260ee40fb0afa7212481d1312d5f 100644 (file)
@@ -41,6 +41,7 @@ class GeneralFloatCases(unittest.TestCase):
         self.assertRaises(ValueError, float, "-.")
         self.assertRaises(ValueError, float, b"-")
         self.assertRaises(TypeError, float, {})
+        self.assertRaisesRegex(TypeError, "not 'dict'", float, {})
         # Lone surrogate
         self.assertRaises(UnicodeEncodeError, float, '\uD8F0')
         # check that we don't accept alternate exponent markers
index 64e7b4457773f68023028e55f2991c91edfa6439..60a388fa24155d6d8277c412ed6ea47c0d13c332 100644 (file)
@@ -773,8 +773,9 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
             goto error;
     }
     else if (PyObject_AsCharBuffer(v, &s, &len)) {
-        PyErr_SetString(PyExc_TypeError,
-                        "complex() argument must be a string or a number");
+        PyErr_Format(PyExc_TypeError,
+            "complex() argument must be a string or a number, not '%.200s'",
+            Py_TYPE(v)->tp_name);
         return NULL;
     }
 
@@ -953,8 +954,9 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         nbi = i->ob_type->tp_as_number;
     if (nbr == NULL || nbr->nb_float == NULL ||
         ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
-        PyErr_SetString(PyExc_TypeError,
-                   "complex() argument must be a string or a number");
+        PyErr_Format(PyExc_TypeError,
+            "complex() argument must be a string or a number, not '%.200s'",
+            Py_TYPE(r)->tp_name);
         if (own_r) {
             Py_DECREF(r);
         }
index 97710636443429ef89f5b14e719387f78069ce21..abea975c59c38c17deea50400d1e18d61e4f8209 100644 (file)
@@ -144,8 +144,9 @@ PyFloat_FromString(PyObject *v)
         }
     }
     else if (PyObject_AsCharBuffer(v, &s, &len)) {
-        PyErr_SetString(PyExc_TypeError,
-            "float() argument must be a string or a number");
+        PyErr_Format(PyExc_TypeError,
+            "float() argument must be a string or a number, not '%.200s'",
+            Py_TYPE(v)->tp_name);
         return NULL;
     }
     last = s + len;