[3.11] gh-113358: Fix rendering tracebacks with exceptions with a broken __getattr__ (GH-113359) (#114118)

This commit is contained in:
Jérome Perrin 2024-01-20 05:35:57 +09:00 committed by GitHub
parent a4ad7a0ac5
commit 20f7cf2c7f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 52 additions and 2 deletions

View file

@ -1199,6 +1199,36 @@ error:
return -1;
}
static int
get_exception_notes(struct exception_print_context *ctx, PyObject *value, PyObject **notes) {
PyObject *note = NULL;
if (_PyObject_LookupAttr(value, &_Py_ID(__notes__), notes) < 0) {
PyObject *type, *errvalue, *tback;
PyErr_Fetch(&type, &errvalue, &tback);
note = PyUnicode_FromFormat("Ignored error getting __notes__: %R", errvalue);
Py_XDECREF(type);
Py_XDECREF(errvalue);
Py_XDECREF(tback);
if (!note) {
goto error;
}
*notes = PyList_New(1);
if (!*notes) {
goto error;
}
if (PyList_SetItem(*notes, 0, note) < 0) {
Py_DECREF(*notes);
goto error;
}
}
return 0;
error:
Py_XDECREF(note);
return -1;
}
static int
print_exception(struct exception_print_context *ctx, PyObject *value)
{
@ -1218,7 +1248,7 @@ print_exception(struct exception_print_context *ctx, PyObject *value)
/* grab the type and notes now because value can change below */
PyObject *type = (PyObject *) Py_TYPE(value);
if (_PyObject_LookupAttr(value, &_Py_ID(__notes__), &notes) < 0) {
if (get_exception_notes(ctx, value, &notes) < 0) {
goto error;
}