diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2022-04-15 18:57:47 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-15 18:57:47 (GMT) |
commit | 5d421d7342fc0d278c129c05bea7028430e94a4e (patch) | |
tree | c5bb38c41d86c6289dfcb1f7937f2df07305f4d8 /Python | |
parent | c06a4ffe818feddef3b5083d9746a1c0b82c84ab (diff) | |
download | cpython-5d421d7342fc0d278c129c05bea7028430e94a4e.zip cpython-5d421d7342fc0d278c129c05bea7028430e94a4e.tar.gz cpython-5d421d7342fc0d278c129c05bea7028430e94a4e.tar.bz2 |
gh-90501: Add PyErr_GetHandledException and PyErr_SetHandledException (GH-30531)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/errors.c | 41 |
1 files changed, 33 insertions, 8 deletions
diff --git a/Python/errors.c b/Python/errors.c index e170c9d..ce77858 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -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); } |