Skip to content

Commit 478cd1e

Browse files
committed
gh-113212: Improve super() error message.
1 parent 4afa7be commit 478cd1e

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Objects/typeobject.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10389,9 +10389,22 @@ supercheck(PyTypeObject *type, PyObject *obj)
1038910389
Py_XDECREF(class_attr);
1039010390
}
1039110391

10392-
PyErr_SetString(PyExc_TypeError,
10393-
"super(type, obj): "
10394-
"obj must be an instance or subtype of type");
10392+
const char *type_or_instance, *obj_str;
10393+
10394+
if (PyType_Check(obj)) {
10395+
type_or_instance = "type";
10396+
obj_str = ((PyTypeObject*)obj)->tp_name;
10397+
}
10398+
else {
10399+
type_or_instance = "instance of";
10400+
obj_str = Py_TYPE(obj)->tp_name;
10401+
}
10402+
10403+
PyErr_Format(PyExc_TypeError,
10404+
"super(type, obj): obj (%s %.200s) is not "
10405+
"an instance or subtype of type (%.200s).",
10406+
type_or_instance, obj_str, type->tp_name);
10407+
1039510408
return NULL;
1039610409
}
1039710410

0 commit comments

Comments
 (0)