From: Ezio Melotti Date: Thu, 7 Nov 2013 17:18:34 +0000 (+0200) Subject: #17080: improve error message of float/complex when the wrong type is passed. X-Git-Tag: v3.4.0b1~350 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a5b9599538083468cffee3c052e15dd541c4bbd5;p=thirdparty%2FPython%2Fcpython.git #17080: improve error message of float/complex when the wrong type is passed. --- diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index f80d7ac5226a..cd55375bdb98 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -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") diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 502292f61542..5c2dc3ff3bcd 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -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 diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 64e7b4457773..60a388fa2415 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -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); } diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 977106364434..abea975c59c3 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -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;