Skip to content

Commit a513ee8

Browse files
committed
Add type information to wrong type error messages
1 parent 6c48ed7 commit a513ee8

File tree

5 files changed

+26
-27
lines changed

5 files changed

+26
-27
lines changed

Objects/abstract.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,17 +1418,17 @@ _PyNumber_Index(PyObject *item)
14181418

14191419
if (!PyLong_Check(result)) {
14201420
PyErr_Format(PyExc_TypeError,
1421-
"__index__ returned non-int (type %.200s)",
1422-
Py_TYPE(result)->tp_name);
1421+
"%.200s.__index__ returned non-int (type %.200s)",
1422+
Py_TYPE(item)->tp_name, Py_TYPE(result)->tp_name);
14231423
Py_DECREF(result);
14241424
return NULL;
14251425
}
14261426
/* Issue #17576: warn if 'result' not of exact type int. */
14271427
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1428-
"__index__ returned non-int (type %.200s). "
1428+
"%.200s.__index__ returned non-int (type %.200s). "
14291429
"The ability to return an instance of a strict subclass of int "
14301430
"is deprecated, and may be removed in a future version of Python.",
1431-
Py_TYPE(result)->tp_name)) {
1431+
Py_TYPE(item)->tp_name, Py_TYPE(result)->tp_name)) {
14321432
Py_DECREF(result);
14331433
return NULL;
14341434
}
@@ -1528,17 +1528,17 @@ PyNumber_Long(PyObject *o)
15281528

15291529
if (!PyLong_Check(result)) {
15301530
PyErr_Format(PyExc_TypeError,
1531-
"__int__ returned non-int (type %.200s)",
1532-
Py_TYPE(result)->tp_name);
1531+
"%.200s.__int__ returned non-int (type %.200s)",
1532+
Py_TYPE(o)->tp_name, Py_TYPE(result)->tp_name);
15331533
Py_DECREF(result);
15341534
return NULL;
15351535
}
15361536
/* Issue #17576: warn if 'result' not of exact type int. */
15371537
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1538-
"__int__ returned non-int (type %.200s). "
1538+
"%.200s.__int__ returned non-int (type %.200s). "
15391539
"The ability to return an instance of a strict subclass of int "
15401540
"is deprecated, and may be removed in a future version of Python.",
1541-
Py_TYPE(result)->tp_name)) {
1541+
Py_TYPE(o)->tp_name, Py_TYPE(result)->tp_name)) {
15421542
Py_DECREF(result);
15431543
return NULL;
15441544
}
@@ -2815,9 +2815,9 @@ PyObject_GetIter(PyObject *o)
28152815
PyObject *res = (*f)(o);
28162816
if (res != NULL && !PyIter_Check(res)) {
28172817
PyErr_Format(PyExc_TypeError,
2818-
"iter() returned non-iterator "
2818+
"%.100s.iter() returned non-iterator "
28192819
"of type '%.100s'",
2820-
Py_TYPE(res)->tp_name);
2820+
Py_TYPE(o)->tp_name, Py_TYPE(res)->tp_name);
28212821
Py_SETREF(res, NULL);
28222822
}
28232823
return res;

Objects/bytesobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,8 @@ format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen)
567567
return NULL;
568568
if (!PyBytes_Check(result)) {
569569
PyErr_Format(PyExc_TypeError,
570-
"__bytes__ returned non-bytes (type %.200s)",
571-
Py_TYPE(result)->tp_name);
570+
"%.200s.__bytes__ returned non-bytes (type %.200s)",
571+
Py_TYPE(v)->tp_name, Py_TYPE(result)->tp_name);
572572
Py_DECREF(result);
573573
return NULL;
574574
}
@@ -2762,8 +2762,8 @@ bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
27622762
return NULL;
27632763
if (!PyBytes_Check(bytes)) {
27642764
PyErr_Format(PyExc_TypeError,
2765-
"__bytes__ returned non-bytes (type %.200s)",
2766-
Py_TYPE(bytes)->tp_name);
2765+
"%.200s.__bytes__ returned non-bytes (type %.200s)",
2766+
Py_TYPE(x)->tp_name, Py_TYPE(bytes)->tp_name);
27672767
Py_DECREF(bytes);
27682768
return NULL;
27692769
}

Objects/complexobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,17 +499,17 @@ try_complex_special_method(PyObject *op)
499499
}
500500
if (!PyComplex_Check(res)) {
501501
PyErr_Format(PyExc_TypeError,
502-
"__complex__ returned non-complex (type %.200s)",
503-
Py_TYPE(res)->tp_name);
502+
"%.200s.__complex__ returned non-complex (type %.200s)",
503+
Py_TYPE(op)->tp_name, Py_TYPE(res)->tp_name);
504504
Py_DECREF(res);
505505
return NULL;
506506
}
507507
/* Issue #29894: warn if 'res' not of exact type complex. */
508508
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
509-
"__complex__ returned non-complex (type %.200s). "
509+
"%.200s.__complex__ returned non-complex (type %.200s). "
510510
"The ability to return an instance of a strict subclass of complex "
511511
"is deprecated, and may be removed in a future version of Python.",
512-
Py_TYPE(res)->tp_name)) {
512+
Py_TYPE(op)->tp_name, Py_TYPE(res)->tp_name)) {
513513
Py_DECREF(res);
514514
return NULL;
515515
}

Objects/object.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,8 @@ PyObject_Repr(PyObject *v)
779779
}
780780
if (!PyUnicode_Check(res)) {
781781
_PyErr_Format(tstate, PyExc_TypeError,
782-
"__repr__ returned non-string (type %.200s)",
783-
Py_TYPE(res)->tp_name);
782+
"%.200s.__repr__ returned non-string (type %.200s)",
783+
Py_TYPE(v)->tp_name, Py_TYPE(res)->tp_name);
784784
Py_DECREF(res);
785785
return NULL;
786786
}
@@ -822,8 +822,8 @@ PyObject_Str(PyObject *v)
822822
}
823823
if (!PyUnicode_Check(res)) {
824824
_PyErr_Format(tstate, PyExc_TypeError,
825-
"__str__ returned non-string (type %.200s)",
826-
Py_TYPE(res)->tp_name);
825+
"%.200s.__str__ returned non-string (type %.200s)",
826+
Py_TYPE(v)->tp_name, Py_TYPE(res)->tp_name);
827827
Py_DECREF(res);
828828
return NULL;
829829
}
@@ -878,8 +878,8 @@ PyObject_Bytes(PyObject *v)
878878
return NULL;
879879
if (!PyBytes_Check(result)) {
880880
PyErr_Format(PyExc_TypeError,
881-
"__bytes__ returned non-bytes (type %.200s)",
882-
Py_TYPE(result)->tp_name);
881+
"%.200s.__bytes__ returned non-bytes (type %.200s)",
882+
Py_TYPE(v)->tp_name, Py_TYPE(result)->tp_name);
883883
Py_DECREF(result);
884884
return NULL;
885885
}

Objects/typeobject.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9920,9 +9920,8 @@ slot_nb_bool(PyObject *self)
99209920
}
99219921
else {
99229922
PyErr_Format(PyExc_TypeError,
9923-
"__bool__ should return "
9924-
"bool, returned %s",
9925-
Py_TYPE(value)->tp_name);
9923+
"%s.__bool__ returned non-bool (type %s)",
9924+
Py_TYPE(self)->tp_name, Py_TYPE(value)->tp_name);
99269925
result = -1;
99279926
}
99289927

0 commit comments

Comments
 (0)