Skip to content

Commit 0f7d134

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

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Objects/typeobject.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10389,9 +10389,20 @@ 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+
} else {
10398+
type_or_instance = "instance of";
10399+
obj_str = Py_TYPE(obj)->tp_name;
10400+
}
10401+
10402+
PyErr_Format(PyExc_TypeError,
10403+
"super(type, obj): obj (%s %.200s) is not an instance or subtype of type (%.200s).",
10404+
type_or_instance, obj_str, type->tp_name);
10405+
1039510406
return NULL;
1039610407
}
1039710408

0 commit comments

Comments
 (0)