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

@ -460,12 +460,46 @@ Querying the error indicator
}
.. c:function:: PyObject* PyErr_GetHandledException(void)
Retrieve the active exception instance, as would be returned by :func:`sys.exception`.
This refers to an exception that was *already caught*, not to an exception that was
freshly raised. Returns a new reference to the exception or ``NULL``.
Does not modify the interpreter's exception state.
.. note::
This function is not normally used by code that wants to handle exceptions.
Rather, it can be used when code needs to save and restore the exception
state temporarily. Use :c:func:`PyErr_SetHandledException` to restore or
clear the exception state.
.. versionadded:: 3.11
.. c:function:: void PyErr_SetHandledException(PyObject *exc)
Set the active exception, as known from ``sys.exception()``. This refers
to an exception that was *already caught*, not to an exception that was
freshly raised.
To clear the exception state, pass ``NULL``.
.. note::
This function is not normally used by code that wants to handle exceptions.
Rather, it can be used when code needs to save and restore the exception
state temporarily. Use :c:func:`PyErr_GetHandledException` to get the exception
state.
.. versionadded:: 3.11
.. c:function:: void PyErr_GetExcInfo(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)
Retrieve the exception info, as known from ``sys.exc_info()``. This refers
to an exception that was *already caught*, not to an exception that was
freshly raised. Returns new references for the three objects, any of which
may be ``NULL``. Does not modify the exception info state.
Retrieve the old-style representation of the exception info, as known from
:func:`sys.exc_info`. This refers to an exception that was *already caught*,
not to an exception that was freshly raised. Returns new references for the
three objects, any of which may be ``NULL``. Does not modify the exception
info state. This function is kept for backwards compatibility. Prefer using
:c:func:`PyErr_GetHandledException`.
.. note::
@ -483,6 +517,8 @@ Querying the error indicator
to an exception that was *already caught*, not to an exception that was
freshly raised. This function steals the references of the arguments.
To clear the exception state, pass ``NULL`` for all three arguments.
This function is kept for backwards compatibility. Prefer using
:c:func:`PyErr_SetHandledException`.
.. note::