diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2023-03-07 21:27:46 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-07 21:27:46 (GMT) |
commit | a33ca2ad1fcf857817cba505a788e15cf9d6ed0c (patch) | |
tree | 8717635b992632d1f3f95f27631f0085423d5423 /Python/errors.c | |
parent | 54060ae91da2df44b3f6e6c698694d40284687e9 (diff) | |
download | cpython-a33ca2ad1fcf857817cba505a788e15cf9d6ed0c.zip cpython-a33ca2ad1fcf857817cba505a788e15cf9d6ed0c.tar.gz cpython-a33ca2ad1fcf857817cba505a788e15cf9d6ed0c.tar.bz2 |
gh-102493: fix normalization in PyErr_SetObject (#102502)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Python/errors.c')
-rw-r--r-- | Python/errors.c | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/Python/errors.c b/Python/errors.c index f573bed..bbf6d39 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -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 |