diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2021-09-03 07:30:17 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-03 07:30:17 (GMT) |
commit | b4b6342848ec0459182a992151099252434cc619 (patch) | |
tree | 049ee796fb01dee4844bccc61fb7427eb169ccf4 /Python | |
parent | a1e15a7a604e6f44cdaf4e106339df62eac5dc9f (diff) | |
download | cpython-b4b6342848ec0459182a992151099252434cc619.zip cpython-b4b6342848ec0459182a992151099252434cc619.tar.gz cpython-b4b6342848ec0459182a992151099252434cc619.tar.bz2 |
bpo-45083: Include the exception class qualname when formatting an exception (GH-28119)
Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
Diffstat (limited to 'Python')
-rw-r--r-- | Python/errors.c | 33 | ||||
-rw-r--r-- | Python/pythonrun.c | 37 |
2 files changed, 35 insertions, 35 deletions
diff --git a/Python/errors.c b/Python/errors.c index ae1cde6..15ca21b 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -1287,46 +1287,45 @@ write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type, } assert(PyExceptionClass_Check(exc_type)); - const char *className = PyExceptionClass_Name(exc_type); - if (className != NULL) { - const char *dot = strrchr(className, '.'); - if (dot != NULL) { - className = dot+1; - } - } - PyObject *moduleName = _PyObject_GetAttrId(exc_type, &PyId___module__); - if (moduleName == NULL || !PyUnicode_Check(moduleName)) { - Py_XDECREF(moduleName); + PyObject *modulename = _PyObject_GetAttrId(exc_type, &PyId___module__); + if (modulename == NULL || !PyUnicode_Check(modulename)) { + Py_XDECREF(modulename); _PyErr_Clear(tstate); if (PyFile_WriteString("<unknown>", file) < 0) { return -1; } } else { - if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins)) { - if (PyFile_WriteObject(moduleName, file, Py_PRINT_RAW) < 0) { - Py_DECREF(moduleName); + if (!_PyUnicode_EqualToASCIIId(modulename, &PyId_builtins)) { + if (PyFile_WriteObject(modulename, file, Py_PRINT_RAW) < 0) { + Py_DECREF(modulename); return -1; } - Py_DECREF(moduleName); + Py_DECREF(modulename); if (PyFile_WriteString(".", file) < 0) { return -1; } } else { - Py_DECREF(moduleName); + Py_DECREF(modulename); } } - if (className == NULL) { + + PyObject *qualname = PyType_GetQualName((PyTypeObject *)exc_type); + if (qualname == NULL || !PyUnicode_Check(qualname)) { + Py_XDECREF(qualname); + _PyErr_Clear(tstate); if (PyFile_WriteString("<unknown>", file) < 0) { return -1; } } else { - if (PyFile_WriteString(className, file) < 0) { + if (PyFile_WriteObject(qualname, file, Py_PRINT_RAW) < 0) { + Py_DECREF(qualname); return -1; } + Py_DECREF(qualname); } if (exc_value && exc_value != Py_None) { diff --git a/Python/pythonrun.c b/Python/pythonrun.c index f00e3eb..3d07f43 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -961,36 +961,37 @@ print_exception(PyObject *f, PyObject *value) /* Don't do anything else */ } else { - PyObject* moduleName; - const char *className; + PyObject* modulename; + _Py_IDENTIFIER(__module__); assert(PyExceptionClass_Check(type)); - className = PyExceptionClass_Name(type); - if (className != NULL) { - const char *dot = strrchr(className, '.'); - if (dot != NULL) - className = dot+1; - } - moduleName = _PyObject_GetAttrId(type, &PyId___module__); - if (moduleName == NULL || !PyUnicode_Check(moduleName)) + modulename = _PyObject_GetAttrId(type, &PyId___module__); + if (modulename == NULL || !PyUnicode_Check(modulename)) { - Py_XDECREF(moduleName); + Py_XDECREF(modulename); + PyErr_Clear(); err = PyFile_WriteString("<unknown>", f); } else { - if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins)) + if (!_PyUnicode_EqualToASCIIId(modulename, &PyId_builtins)) { - err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW); + err = PyFile_WriteObject(modulename, f, Py_PRINT_RAW); err += PyFile_WriteString(".", f); } - Py_DECREF(moduleName); + Py_DECREF(modulename); } if (err == 0) { - if (className == NULL) - err = PyFile_WriteString("<unknown>", f); - else - err = PyFile_WriteString(className, f); + PyObject* qualname = PyType_GetQualName((PyTypeObject *)type); + if (qualname == NULL || !PyUnicode_Check(qualname)) { + Py_XDECREF(qualname); + PyErr_Clear(); + err = PyFile_WriteString("<unknown>", f); + } + else { + err = PyFile_WriteObject(qualname, f, Py_PRINT_RAW); + Py_DECREF(qualname); + } } } if (err == 0 && (value != Py_None)) { |