Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2b4d518
bpo-46343: Add PyErr_GetActiveException and PyErr_SetActiveException
iritkatriel Jan 11, 2022
87e6087
GetActiveException returns exception as its return value
iritkatriel Jan 11, 2022
ee4d98e
fix test
iritkatriel Jan 11, 2022
ebd9278
use sys.exception instead of sys.exc_info
iritkatriel Jan 13, 2022
5631d32
updated whatsnew
iritkatriel Jan 13, 2022
20fa421
regen-limited-abi
iritkatriel Jan 13, 2022
3a7ab17
[1] Do not steal reference. [2] () --> (void)
iritkatriel Jan 13, 2022
dbfd9d4
Use the new Py_(X)NewRef in a couple of places. Use self.fail() in test.
iritkatriel Jan 17, 2022
a412e4b
add :func: markup in doc
iritkatriel Jan 17, 2022
bcdf599
split out _PyErr_SetActiveException(tstate, exc)
iritkatriel Feb 1, 2022
c63eabf
Merge remote-tracking branch 'upstream/main' into bpo-46343-GetSetExc…
iritkatriel Mar 15, 2022
18eb35c
read --> get
iritkatriel Mar 23, 2022
87981ab
fix limited api version
iritkatriel Mar 23, 2022
9555597
PyErr_GetActiveException always returns NULL if no exception. Fix ref…
iritkatriel Mar 23, 2022
e0ee504
doc changes following review by Victor
iritkatriel Mar 23, 2022
e6a3a7e
renamed Get/SetActiveException --> Get/SetHandledException
iritkatriel Apr 13, 2022
261464c
Merge remote-tracking branch 'upstream/main' into bpo-46343-GetSetExc…
iritkatriel Apr 13, 2022
0355cc9
add new functions to Doc/data/stable_abi.dat
iritkatriel Apr 13, 2022
9382215
alphabetical
iritkatriel Apr 13, 2022
1cc3a4b
make regen-limited-abi
iritkatriel Apr 13, 2022
ffa870b
clearer doc
iritkatriel Apr 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
renamed Get/SetActiveException --> Get/SetHandledException
  • Loading branch information
iritkatriel committed Apr 13, 2022
commit e6a3a7e01b8a4c18bac0af652b826416981927bc
12 changes: 6 additions & 6 deletions Doc/c-api/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ Querying the error indicator
}


.. c:function:: PyObject* PyErr_GetActiveException(void)
.. 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
Expand All @@ -471,12 +471,12 @@ Querying the error indicator

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_SetActiveException` to restore or
state temporarily. Use :c:func:`PyErr_SetHandledException` to restore or
clear the exception state.

.. versionadded:: 3.11

.. c:function:: void PyErr_SetActiveException(PyObject *exc)
.. 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
Expand All @@ -487,7 +487,7 @@ Querying the error indicator

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_GetActiveException` to get the exception
state temporarily. Use :c:func:`PyErr_GetHandledException` to get the exception
state.

.. versionadded:: 3.11
Expand All @@ -499,7 +499,7 @@ Querying the error indicator
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_GetActiveException`.
:c:func:`PyErr_GetHandledException`.

.. note::

Expand All @@ -518,7 +518,7 @@ Querying the error indicator
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_SetActiveException`.
:c:func:`PyErr_SetHandledException`.

.. note::

Expand Down
4 changes: 2 additions & 2 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ New Features
(Contributed by Victor Stinner in :issue:`46906`.)

* Added two new functions to get and set the active exception instance:
:c:func:`PyErr_GetActiveException` and :c:func:`PyErr_SetActiveException`.
: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.
Expand Down
4 changes: 2 additions & 2 deletions Include/cpython/pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ typedef PyOSErrorObject PyWindowsErrorObject;

PyAPI_FUNC(void) _PyErr_SetKeyError(PyObject *);
PyAPI_FUNC(_PyErr_StackItem*) _PyErr_GetTopmostException(PyThreadState *tstate);
PyAPI_FUNC(PyObject*) _PyErr_GetActiveException(PyThreadState *);
PyAPI_FUNC(void) _PyErr_SetActiveException(PyThreadState *, PyObject *);
PyAPI_FUNC(PyObject*) _PyErr_GetHandledException(PyThreadState *);
PyAPI_FUNC(void) _PyErr_SetHandledException(PyThreadState *, PyObject *);
PyAPI_FUNC(void) _PyErr_GetExcInfo(PyThreadState *, PyObject **, PyObject **, PyObject **);

/* Context manipulation (PEP 3134) */
Expand Down
4 changes: 2 additions & 2 deletions Include/pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ PyAPI_FUNC(void) PyErr_Clear(void);
PyAPI_FUNC(void) PyErr_Fetch(PyObject **, PyObject **, PyObject **);
PyAPI_FUNC(void) PyErr_Restore(PyObject *, PyObject *, PyObject *);
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030b0000
PyAPI_FUNC(PyObject*) PyErr_GetActiveException(void);
PyAPI_FUNC(void) PyErr_SetActiveException(PyObject *);
PyAPI_FUNC(PyObject*) PyErr_GetHandledException(void);
PyAPI_FUNC(void) PyErr_SetHandledException(PyObject *);
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(void) PyErr_GetExcInfo(PyObject **, PyObject **, PyObject **);
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_stable_abi_ctypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Added :c:func:`PyErr_GetActiveException` and
:c:func:`PyErr_SetActiveException` as simpler alternatives to
Added :c:func:`PyErr_GetHandledException` and
:c:func:`PyErr_SetHandledException` as simpler alternatives to
:c:func:`PyErr_GetExcInfo` and :c:func:`PyErr_SetExcInfo`.

They are included in the stable ABI.
4 changes: 2 additions & 2 deletions Misc/stable_abi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2224,8 +2224,8 @@ function PyMemoryView_FromBuffer

data Py_Version
added 3.11
function PyErr_GetActiveException
function PyErr_GetHandledException
added 3.11
function PyErr_SetActiveException
function PyErr_SetHandledException
added 3.11

4 changes: 2 additions & 2 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2565,10 +2565,10 @@ set_errno(PyObject *self, PyObject *args)
static PyObject *
test_set_exception(PyObject *self, PyObject *new_exc)
{
PyObject *exc = PyErr_GetActiveException();
PyObject *exc = PyErr_GetHandledException();
assert(PyExceptionInstance_Check(exc) || exc == NULL);

PyErr_SetActiveException(new_exc);
PyErr_SetHandledException(new_exc);
return exc;
}

Expand Down
4 changes: 2 additions & 2 deletions PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ _PyErr_GetExcInfo(PyThreadState *tstate,
}

PyObject*
_PyErr_GetActiveException(PyThreadState *tstate)
_PyErr_GetHandledException(PyThreadState *tstate)
{
_PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
PyObject *exc = exc_info->exc_value;
Expand All @@ -511,25 +511,25 @@ _PyErr_GetActiveException(PyThreadState *tstate)
}

PyObject*
PyErr_GetActiveException(void)
PyErr_GetHandledException(void)
{
PyThreadState *tstate = _PyThreadState_GET();
return _PyErr_GetActiveException(tstate);
return _PyErr_GetHandledException(tstate);
}

void
_PyErr_SetActiveException(PyThreadState *tstate, PyObject *exc)
_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_SetActiveException(PyObject *exc)
PyErr_SetHandledException(PyObject *exc)
{
PyThreadState *tstate = _PyThreadState_GET();
_PyErr_SetActiveException(tstate, exc);
_PyErr_SetHandledException(tstate, exc);
}

void
Expand All @@ -542,7 +542,7 @@ PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
void
PyErr_SetExcInfo(PyObject *type, PyObject *value, PyObject *traceback)
{
PyErr_SetActiveException(value);
PyErr_SetHandledException(value);
/* These args are no longer used, but we still need to steal a ref */
Py_XDECREF(type);
Py_XDECREF(traceback);
Expand Down