summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2022-04-15 18:57:47 (GMT)
committerGitHub <noreply@github.com>2022-04-15 18:57:47 (GMT)
commit5d421d7342fc0d278c129c05bea7028430e94a4e (patch)
treec5bb38c41d86c6289dfcb1f7937f2df07305f4d8 /Doc
parentc06a4ffe818feddef3b5083d9746a1c0b82c84ab (diff)
downloadcpython-5d421d7342fc0d278c129c05bea7028430e94a4e.zip
cpython-5d421d7342fc0d278c129c05bea7028430e94a4e.tar.gz
cpython-5d421d7342fc0d278c129c05bea7028430e94a4e.tar.bz2
gh-90501: Add PyErr_GetHandledException and PyErr_SetHandledException (GH-30531)
Diffstat (limited to 'Doc')
-rw-r--r--Doc/c-api/exceptions.rst44
-rw-r--r--Doc/data/stable_abi.dat2
-rw-r--r--Doc/library/sys.rst17
-rw-r--r--Doc/whatsnew/3.11.rst8
4 files changed, 55 insertions, 16 deletions
diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst
index a5a93d0..7bfeca5 100644
--- a/Doc/c-api/exceptions.rst
+++ b/Doc/c-api/exceptions.rst
@@ -460,12 +460,46 @@ Querying the error indicator
}
-.. c:function:: void PyErr_GetExcInfo(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)
+.. 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
- Retrieve the exception info, as known from ``sys.exc_info()``. This refers
+.. 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. Returns new references for the three objects, any of which
- may be ``NULL``. Does not modify the exception info state.
+ 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 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::
diff --git a/Doc/data/stable_abi.dat b/Doc/data/stable_abi.dat
index 849a2cf..5387d0b 100644
--- a/Doc/data/stable_abi.dat
+++ b/Doc/data/stable_abi.dat
@@ -137,6 +137,7 @@ function,PyErr_Fetch,3.2,,
function,PyErr_Format,3.2,,
function,PyErr_FormatV,3.5,,
function,PyErr_GetExcInfo,3.7,,
+function,PyErr_GetHandledException,3.11,,
function,PyErr_GivenExceptionMatches,3.2,,
function,PyErr_NewException,3.2,,
function,PyErr_NewExceptionWithDoc,3.2,,
@@ -159,6 +160,7 @@ function,PyErr_SetFromErrnoWithFilenameObject,3.2,,
function,PyErr_SetFromErrnoWithFilenameObjects,3.7,,
function,PyErr_SetFromWindowsErr,3.7,on Windows,
function,PyErr_SetFromWindowsErrWithFilename,3.7,on Windows,
+function,PyErr_SetHandledException,3.11,,
function,PyErr_SetImportError,3.7,,
function,PyErr_SetImportErrorSubclass,3.6,,
function,PyErr_SetInterrupt,3.2,,
diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst
index 126da31..2a8b532 100644
--- a/Doc/library/sys.rst
+++ b/Doc/library/sys.rst
@@ -381,19 +381,12 @@ always available.
.. function:: exception()
- This function returns the exception instance that is currently being
- handled. This exception is specific both to the current thread and
- to the current stack frame. If the current stack frame is not handling
- an exception, the exception is taken from the calling stack frame, or its
- caller, and so on until a stack frame is found that is handling an
- exception. Here, "handling an exception" is defined as "executing an
- except clause." For any stack frame, only the exception being currently
- handled is accessible.
+ This function, when called while an exception handler is executing (such as
+ an ``except`` or ``except*`` clause), returns the exception instance that
+ was caught by this handler. When exception handlers are nested within one
+ another, only the exception handled by the innermost handler is accessible.
- .. index:: object: traceback
-
- If no exception is being handled anywhere on the stack, ``None`` is
- returned.
+ If no exception handler is executing, this function returns ``None``.
.. versionadded:: 3.11
diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst
index a5a5268..b6f47f5 100644
--- a/Doc/whatsnew/3.11.rst
+++ b/Doc/whatsnew/3.11.rst
@@ -1161,6 +1161,14 @@ New Features
:c:func:`PyFrame_GetBuiltins`, :c:func:`PyFrame_GetGenerator`,
:c:func:`PyFrame_GetGlobals`, :c:func:`PyFrame_GetLasti`.
+* Added two new functions to get and set the active exception instance:
+ :c:func:`PyErr_GetHandledException` and :c:func:`PyErr_SetHandledException`.
+ These are alternatives to :c:func:`PyErr_SetExcInfo()` and
+ :c:func:`PyErr_GetExcInfo()` which work with the legacy 3-tuple
+ representation of exceptions.
+ (Contributed by Irit Katriel in :issue:`46343`.)
+
+
Porting to Python 3.11
----------------------