mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
[3.10] bpo-45083: Include the exception class qualname when formatting an exception (GH-28119) (GH-28134)
Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
(cherry picked from commit b4b6342848
)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
* Use a private version of _PyType_GetQualName
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
parent
d41abe8970
commit
6b996d61c9
7 changed files with 78 additions and 36 deletions
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include "Python.h"
|
||||
#include "pycore_initconfig.h"
|
||||
#include "pycore_object.h" // _PyType_GetQualName
|
||||
#include "pycore_pyerrors.h"
|
||||
#include "pycore_pystate.h" // _PyThreadState_GET()
|
||||
#include "pycore_sysmodule.h"
|
||||
|
@ -1323,46 +1324,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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue