Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f2c0830

Browse files
committed
SF bug #963956: Bad error mesage when subclassing a module
Add a more informative message for the common user mistake of subclassing from a module name rather than another class (i.e. random instead of random.random).
1 parent 0096e26 commit f2c0830

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

Python/ceval.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3922,6 +3922,15 @@ build_class(PyObject *methods, PyObject *bases, PyObject *name)
39223922
}
39233923
result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
39243924
Py_DECREF(metaclass);
3925+
if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
3926+
/* A type error here likely means that the user passed
3927+
in a base that was not a class (such the random module
3928+
instead of the random.random type). Help them out with
3929+
a more informative error message */
3930+
PyErr_SetString(PyExc_TypeError,
3931+
"Error when calling the metaclass.\n" \
3932+
"Make sure the base arguments are valid.");
3933+
}
39253934
return result;
39263935
}
39273936

0 commit comments

Comments
 (0)