]> granicus.if.org Git - python/commitdiff
Patch [ 1586791 ] better error msgs for some TypeErrors
authorGeorg Brandl <georg@python.org>
Sun, 19 Nov 2006 08:48:30 +0000 (08:48 +0000)
committerGeorg Brandl <georg@python.org>
Sun, 19 Nov 2006 08:48:30 +0000 (08:48 +0000)
Lib/test/test_format.py
Misc/NEWS
Objects/listobject.c
Objects/stringobject.c
Objects/tupleobject.c

index a9b31707a24917c8e357fb136998a9588a413630..8bf5d6e4a96d09564b7ad72cc6ce734868752d41 100644 (file)
@@ -219,8 +219,8 @@ if have_unicode:
     test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
              "unsupported format character '?' (0x3000) at index 5")
 
-test_exc('%d', '1', TypeError, "int argument required")
-test_exc('%g', '1', TypeError, "float argument required")
+test_exc('%d', '1', TypeError, "int argument required, not str")
+test_exc('%g', '1', TypeError, "float argument required, not str")
 test_exc('no format', '1', TypeError,
          "not all arguments converted during string formatting")
 test_exc('no format', u'1', TypeError,
index 4f1df73fac950e53fa7ce180660ab80329b82139..6fd73f6426583bf363dd6770b65d8fffc42f3d15 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1?
 Core and builtins
 -----------------
 
+- Patch #1586791: Better exception messages for some operations on strings,
+  tuples and lists.
+
 - Bug #1067760: Deprecate passing floats to file.seek.
 
 - Bug #1591996: Correctly forward exception in instance_contains().
index a1ac2cba1017fd6e9785c38e646dfe6862f8661b..3083b5f3e016fc293c8dd00cd9297ba6fc5bbf69 100644 (file)
@@ -946,9 +946,10 @@ islt(PyObject *x, PyObject *y, PyObject *compare)
        if (res == NULL)
                return -1;
        if (!PyInt_Check(res)) {
+               PyErr_Format(PyExc_TypeError,
+                            "comparison function must return int, not %.200s",
+                            res->ob_type->tp_name);
                Py_DECREF(res);
-               PyErr_SetString(PyExc_TypeError,
-                               "comparison function must return int");
                return -1;
        }
        i = PyInt_AsLong(res);
@@ -2491,8 +2492,9 @@ list_subscript(PyListObject* self, PyObject* item)
                }
        }
        else {
-               PyErr_SetString(PyExc_TypeError,
-                               "list indices must be integers");
+               PyErr_Format(PyExc_TypeError,
+                            "list indices must be integers, not %.200s",
+                            item->ob_type->tp_name);
                return NULL;
        }
 }
@@ -2635,8 +2637,9 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
                }
        }
        else {
-               PyErr_SetString(PyExc_TypeError,
-                               "list indices must be integers");
+               PyErr_Format(PyExc_TypeError,
+                            "list indices must be integers, not %.200s",
+                            item->ob_type->tp_name);
                return -1;
        }
 }
index aa2fd872ddb4bb99d67eeec8781704f9b66a8216..5d31c388c6f5ab00dec13443fd207ebf1630614c 100644 (file)
@@ -1071,8 +1071,9 @@ string_contains(PyObject *str_obj, PyObject *sub_obj)
                        return PyUnicode_Contains(str_obj, sub_obj);
 #endif
                if (!PyString_Check(sub_obj)) {
-                       PyErr_SetString(PyExc_TypeError,
-                           "'in <string>' requires string as left operand");
+                       PyErr_Format(PyExc_TypeError,
+                           "'in <string>' requires string as left operand, "
+                           "not %.200s", sub_obj->ob_type->tp_name);
                        return -1;
                }
        }
@@ -1240,8 +1241,9 @@ string_subscript(PyStringObject* self, PyObject* item)
                }
        }
        else {
-               PyErr_SetString(PyExc_TypeError,
-                               "string indices must be integers");
+               PyErr_Format(PyExc_TypeError,
+                            "string indices must be integers, not %.200s",
+                            item->ob_type->tp_name);
                return NULL;
        }
 }
@@ -4148,7 +4150,8 @@ formatfloat(char *buf, size_t buflen, int flags,
        double x;
        x = PyFloat_AsDouble(v);
        if (x == -1.0 && PyErr_Occurred()) {
-               PyErr_SetString(PyExc_TypeError, "float argument required");
+               PyErr_Format(PyExc_TypeError, "float argument required, "
+                            "not %.200s", v->ob_type->tp_name);
                return -1;
        }
        if (prec < 0)
@@ -4343,7 +4346,8 @@ formatint(char *buf, size_t buflen, int flags,
 
        x = PyInt_AsLong(v);
        if (x == -1 && PyErr_Occurred()) {
-               PyErr_SetString(PyExc_TypeError, "int argument required");
+               PyErr_Format(PyExc_TypeError, "int argument required, not %.200s",
+                            v->ob_type->tp_name);
                return -1;
        }
        if (x < 0 && type == 'u') {
index 6f3711f1f457f290cc05d532a2e644c506b1b495..c85b35a6037396e95c558d2775903a97a3e847f2 100644 (file)
@@ -620,8 +620,9 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
                }
        }
        else {
-               PyErr_SetString(PyExc_TypeError, 
-                               "tuple indices must be integers");
+               PyErr_Format(PyExc_TypeError, 
+                            "tuple indices must be integers, not %.200s",
+                            item->ob_type->tp_name);
                return NULL;
        }
 }