mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
gh-90501: Add PyErr_GetHandledException and PyErr_SetHandledException (GH-30531)
This commit is contained in:
parent
c06a4ffe81
commit
5d421d7342
13 changed files with 141 additions and 24 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue