gh-90501: Add PyErr_GetHandledException and PyErr_SetHandledException (GH-30531)

This commit is contained in:
Irit Katriel 2022-04-15 19:57:47 +01:00 committed by GitHub
parent c06a4ffe81
commit 5d421d7342
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 141 additions and 24 deletions

View file

@ -499,6 +499,38 @@ _PyErr_GetExcInfo(PyThreadState *tstate,
Py_XINCREF(*p_traceback);
}
PyObject*
_PyErr_GetHandledException(PyThreadState *tstate)
{
_PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
PyObject *exc = exc_info->exc_value;
if (exc == NULL || exc == Py_None) {
return NULL;
}
return Py_NewRef(exc);
}
PyObject*
PyErr_GetHandledException(void)
{
PyThreadState *tstate = _PyThreadState_GET();
return _PyErr_GetHandledException(tstate);
}
void
_PyErr_SetHandledException(PyThreadState *tstate, PyObject *exc)
{
PyObject *oldexc = tstate->exc_info->exc_value;
tstate->exc_info->exc_value = Py_XNewRef(exc);
Py_XDECREF(oldexc);
}
void
PyErr_SetHandledException(PyObject *exc)
{
PyThreadState *tstate = _PyThreadState_GET();
_PyErr_SetHandledException(tstate, exc);
}
void
PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
@ -510,17 +542,10 @@ PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
void
PyErr_SetExcInfo(PyObject *type, PyObject *value, PyObject *traceback)
{
PyThreadState *tstate = _PyThreadState_GET();
PyObject *oldvalue = tstate->exc_info->exc_value;
tstate->exc_info->exc_value = value;
PyErr_SetHandledException(value);
/* These args are no longer used, but we still need to steal a ref */
Py_XDECREF(type);
Py_XDECREF(traceback);
Py_XDECREF(oldvalue);
}