gh-102493: fix normalization in PyErr_SetObject (#102502)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Irit Katriel 2023-03-07 21:27:46 +00:00 committed by GitHub
parent 54060ae91d
commit a33ca2ad1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 4 deletions

View file

@ -149,9 +149,16 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
exception);
return;
}
Py_XINCREF(value);
/* Normalize the exception */
if (value == NULL || (PyObject *)Py_TYPE(value) != exception) {
int is_subclass = 0;
if (value != NULL && PyExceptionInstance_Check(value)) {
is_subclass = PyObject_IsSubclass((PyObject *)Py_TYPE(value), exception);
if (is_subclass < 0) {
return;
}
}
Py_XINCREF(value);
if (!is_subclass) {
/* We must normalize the value right now */
PyObject *fixed_value;
@ -206,9 +213,10 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
Py_DECREF(exc_value);
}
}
if (value != NULL && PyExceptionInstance_Check(value))
assert(value != NULL);
if (PyExceptionInstance_Check(value))
tb = PyException_GetTraceback(value);
_PyErr_Restore(tstate, Py_XNewRef(exception), value, tb);
_PyErr_Restore(tstate, Py_NewRef(Py_TYPE(value)), value, tb);
}
void