Skip to content

Commit 768a3cd

Browse files
author
georg.brandl
committed
Patch [ 1586791 ] better error msgs for some TypeErrors
git-svn-id: http://svn.python.org/projects/python/trunk@52787 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 67db0e6 commit 768a3cd

5 files changed

Lines changed: 27 additions & 16 deletions

File tree

Lib/test/test_format.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ def test_exc(formatstr, args, exception, excmsg):
219219
test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
220220
"unsupported format character '?' (0x3000) at index 5")
221221

222-
test_exc('%d', '1', TypeError, "int argument required")
223-
test_exc('%g', '1', TypeError, "float argument required")
222+
test_exc('%d', '1', TypeError, "int argument required, not str")
223+
test_exc('%g', '1', TypeError, "float argument required, not str")
224224
test_exc('no format', '1', TypeError,
225225
"not all arguments converted during string formatting")
226226
test_exc('no format', u'1', TypeError,

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Patch #1586791: Better exception messages for some operations on strings,
16+
tuples and lists.
17+
1518
- Bug #1067760: Deprecate passing floats to file.seek.
1619

1720
- Bug #1591996: Correctly forward exception in instance_contains().

Objects/listobject.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -946,9 +946,10 @@ islt(PyObject *x, PyObject *y, PyObject *compare)
946946
if (res == NULL)
947947
return -1;
948948
if (!PyInt_Check(res)) {
949+
PyErr_Format(PyExc_TypeError,
950+
"comparison function must return int, not %.200s",
951+
res->ob_type->tp_name);
949952
Py_DECREF(res);
950-
PyErr_SetString(PyExc_TypeError,
951-
"comparison function must return int");
952953
return -1;
953954
}
954955
i = PyInt_AsLong(res);
@@ -2491,8 +2492,9 @@ list_subscript(PyListObject* self, PyObject* item)
24912492
}
24922493
}
24932494
else {
2494-
PyErr_SetString(PyExc_TypeError,
2495-
"list indices must be integers");
2495+
PyErr_Format(PyExc_TypeError,
2496+
"list indices must be integers, not %.200s",
2497+
item->ob_type->tp_name);
24962498
return NULL;
24972499
}
24982500
}
@@ -2635,8 +2637,9 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
26352637
}
26362638
}
26372639
else {
2638-
PyErr_SetString(PyExc_TypeError,
2639-
"list indices must be integers");
2640+
PyErr_Format(PyExc_TypeError,
2641+
"list indices must be integers, not %.200s",
2642+
item->ob_type->tp_name);
26402643
return -1;
26412644
}
26422645
}

Objects/stringobject.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,9 @@ string_contains(PyObject *str_obj, PyObject *sub_obj)
10711071
return PyUnicode_Contains(str_obj, sub_obj);
10721072
#endif
10731073
if (!PyString_Check(sub_obj)) {
1074-
PyErr_SetString(PyExc_TypeError,
1075-
"'in <string>' requires string as left operand");
1074+
PyErr_Format(PyExc_TypeError,
1075+
"'in <string>' requires string as left operand, "
1076+
"not %.200s", sub_obj->ob_type->tp_name);
10761077
return -1;
10771078
}
10781079
}
@@ -1240,8 +1241,9 @@ string_subscript(PyStringObject* self, PyObject* item)
12401241
}
12411242
}
12421243
else {
1243-
PyErr_SetString(PyExc_TypeError,
1244-
"string indices must be integers");
1244+
PyErr_Format(PyExc_TypeError,
1245+
"string indices must be integers, not %.200s",
1246+
item->ob_type->tp_name);
12451247
return NULL;
12461248
}
12471249
}
@@ -4148,7 +4150,8 @@ formatfloat(char *buf, size_t buflen, int flags,
41484150
double x;
41494151
x = PyFloat_AsDouble(v);
41504152
if (x == -1.0 && PyErr_Occurred()) {
4151-
PyErr_SetString(PyExc_TypeError, "float argument required");
4153+
PyErr_Format(PyExc_TypeError, "float argument required, "
4154+
"not %.200s", v->ob_type->tp_name);
41524155
return -1;
41534156
}
41544157
if (prec < 0)
@@ -4343,7 +4346,8 @@ formatint(char *buf, size_t buflen, int flags,
43434346

43444347
x = PyInt_AsLong(v);
43454348
if (x == -1 && PyErr_Occurred()) {
4346-
PyErr_SetString(PyExc_TypeError, "int argument required");
4349+
PyErr_Format(PyExc_TypeError, "int argument required, not %.200s",
4350+
v->ob_type->tp_name);
43474351
return -1;
43484352
}
43494353
if (x < 0 && type == 'u') {

Objects/tupleobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,9 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
620620
}
621621
}
622622
else {
623-
PyErr_SetString(PyExc_TypeError,
624-
"tuple indices must be integers");
623+
PyErr_Format(PyExc_TypeError,
624+
"tuple indices must be integers, not %.200s",
625+
item->ob_type->tp_name);
625626
return NULL;
626627
}
627628
}

0 commit comments

Comments
 (0)