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

Skip to content

Commit cfc3192

Browse files
committed
SF bug #1014215: Unspecific errors with metaclass
High level error message was stomping useful detailed messages from lower level routines. The new approach is to augment string error messages returned by the low level routines. The provides both high and low level information. If the exception value is not a string, no changes are made. To see the improved messages in action, type: import random class R(random): pass class B(bool): pass
1 parent 6543b45 commit cfc3192

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

Python/ceval.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4086,10 +4086,22 @@ build_class(PyObject *methods, PyObject *bases, PyObject *name)
40864086
/* A type error here likely means that the user passed
40874087
in a base that was not a class (such the random module
40884088
instead of the random.random type). Help them out with
4089-
a more informative error message */
4090-
PyErr_SetString(PyExc_TypeError,
4091-
"Error when calling the metaclass.\n" \
4092-
"Make sure the base arguments are valid.");
4089+
by augmenting the error message with more information.*/
4090+
4091+
PyObject *ptype, *pvalue, *ptraceback;
4092+
4093+
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4094+
if (PyString_Check(pvalue)) {
4095+
PyObject *newmsg;
4096+
newmsg = PyString_FromFormat(
4097+
"Error when calling the metaclass bases\n %s",
4098+
PyString_AS_STRING(pvalue));
4099+
if (newmsg != NULL) {
4100+
Py_DECREF(pvalue);
4101+
pvalue = newmsg;
4102+
}
4103+
}
4104+
PyErr_Restore(ptype, pvalue, ptraceback);
40934105
}
40944106
return result;
40954107
}

0 commit comments

Comments
 (0)