Skip to content

Commit 658e247

Browse files
gh-130821: Improve error messages for magic methods returning wrong type
1 parent a096314 commit 658e247

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

‎Objects/typeobject.c‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7950,16 +7950,16 @@ _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs)
79507950
return -1;
79517951
}
79527952
if (!PyTuple_Check(newargs)) {
7953-
PyErr_Format(PyExc_TypeError,
7954-
"__getnewargs_ex__ should return a tuple, "
7955-
"not '%.200s'", Py_TYPE(newargs)->tp_name);
7953+
PyErr_Format(PyExc_TypeError,
7954+
"%T.__getnewargs_ex__() must return a tuple, "
7955+
"not %T", obj, newargs);
79567956
Py_DECREF(newargs);
79577957
return -1;
79587958
}
79597959
if (PyTuple_GET_SIZE(newargs) != 2) {
79607960
PyErr_Format(PyExc_ValueError,
7961-
"__getnewargs_ex__ should return a tuple of "
7962-
"length 2, not %zd", PyTuple_GET_SIZE(newargs));
7961+
"%T.__getnewargs_ex__() must return a tuple of "
7962+
"length 2, not %zd", obj, PyTuple_GET_SIZE(newargs));
79637963
Py_DECREF(newargs);
79647964
return -1;
79657965
}
@@ -8002,8 +8002,8 @@ _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs)
80028002
}
80038003
if (!PyTuple_Check(*args)) {
80048004
PyErr_Format(PyExc_TypeError,
8005-
"__getnewargs__ should return a tuple, "
8006-
"not '%.200s'", Py_TYPE(*args)->tp_name);
8005+
"%T.__getnewargs__() must return a tuple, "
8006+
"not %T", obj, *args);
80078007
Py_CLEAR(*args);
80088008
return -1;
80098009
}
@@ -10759,9 +10759,10 @@ slot_tp_hash(PyObject *self)
1075910759
return PyObject_HashNotImplemented(self);
1076010760
}
1076110761
if (!PyLong_Check(res)) {
10762+
PyErr_Format(PyExc_TypeError,
10763+
"%T.__hash__() must return an integer, not %T",
10764+
self, res);
1076210765
Py_DECREF(res);
10763-
PyErr_SetString(PyExc_TypeError,
10764-
"__hash__ method should return an integer");
1076510766
return -1;
1076610767
}
1076710768
/* Transform the PyLong `res` to a Py_hash_t `h`. For an existing
@@ -11030,8 +11031,8 @@ slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
1103011031
return -1;
1103111032
if (res != Py_None) {
1103211033
PyErr_Format(PyExc_TypeError,
11033-
"__init__() should return None, not '%.200s'",
11034-
Py_TYPE(res)->tp_name);
11034+
"%T.__init__() must return None, not %T",
11035+
self, res);
1103511036
Py_DECREF(res);
1103611037
return -1;
1103711038
}

‎Python/bytecodes.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4819,8 +4819,8 @@ dummy_func(
48194819
inst(EXIT_INIT_CHECK, (should_be_none -- )) {
48204820
if (!PyStackRef_IsNone(should_be_none)) {
48214821
PyErr_Format(PyExc_TypeError,
4822-
"__init__() should return None, not '%.200s'",
4823-
Py_TYPE(PyStackRef_AsPyObjectBorrow(should_be_none))->tp_name);
4822+
"__init__() must return None, not %T",
4823+
PyStackRef_AsPyObjectBorrow(should_be_none));
48244824
ERROR_NO_POP();
48254825
}
48264826
DEAD(should_be_none);

0 commit comments

Comments
 (0)