PyErr_Print(): When printing a class exception, try to dig out the

__module__ string and if found, print <module>.<class>, unless
<module> == "exceptions".
This commit is contained in:
Barry Warsaw 1997-09-16 21:42:03 +00:00
parent b81b5c72db
commit 2f5f6a2595

View file

@ -745,13 +745,28 @@ PyErr_Print()
/* Don't do anything else */
}
else if (PyClass_Check(exception)) {
PyObject* className =
((PyClassObject*)exception)->cl_name;
if (className == NULL)
PyClassObject* exc = (PyClassObject*)exception;
PyObject* className = exc->cl_name;
PyObject* moduleName =
PyDict_GetItemString(exc->cl_dict, "__module__");
if (moduleName == NULL)
err = PyFile_WriteString("<unknown>", f);
else
err = PyFile_WriteObject(className, f,
Py_PRINT_RAW);
else {
char* modstr = PyString_AsString(moduleName);
if (modstr && strcmp(modstr, "exceptions"))
{
err = PyFile_WriteString(modstr, f);
err += PyFile_WriteString(".", f);
}
}
if (err == 0) {
if (className == NULL)
err = PyFile_WriteString("<unknown>", f);
else
err = PyFile_WriteObject(className, f,
Py_PRINT_RAW);
}
}
else
err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);