Skip to content

Commit

Permalink
Revert "pythongh-102192: remove unused _PyErr_ChainExceptions"
Browse files Browse the repository at this point in the history
This reverts commit de98c08.
  • Loading branch information
iritkatriel committed Mar 31, 2023
1 parent de98c08 commit c1261c7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions Include/cpython/pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ PyAPI_FUNC(void) _PyErr_GetExcInfo(PyThreadState *, PyObject **, PyObject **, Py

/* Context manipulation (PEP 3134) */

PyAPI_FUNC(void) _PyErr_ChainExceptions(PyObject *, PyObject *, PyObject *);
PyAPI_FUNC(void) _PyErr_ChainExceptions1(PyObject *);

/* Like PyErr_Format(), but saves current exception as __context__ and
Expand Down
37 changes: 37 additions & 0 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,43 @@ _PyErr_StackItemToExcInfoTuple(_PyErr_StackItem *err_info)
}


/* Like PyErr_Restore(), but if an exception is already set,
set the context associated with it.
The caller is responsible for ensuring that this call won't create
any cycles in the exception context chain. */
void
_PyErr_ChainExceptions(PyObject *typ, PyObject *val, PyObject *tb)
{
if (typ == NULL)
return;

PyThreadState *tstate = _PyThreadState_GET();

if (!PyExceptionClass_Check(typ)) {
_PyErr_Format(tstate, PyExc_SystemError,
"_PyErr_ChainExceptions: "
"exception %R is not a BaseException subclass",
typ);
return;
}

if (_PyErr_Occurred(tstate)) {
_PyErr_NormalizeException(tstate, &typ, &val, &tb);
if (tb != NULL) {
PyException_SetTraceback(val, tb);
Py_DECREF(tb);
}
Py_DECREF(typ);
PyObject *exc2 = _PyErr_GetRaisedException(tstate);
PyException_SetContext(exc2, val);
_PyErr_SetRaisedException(tstate, exc2);
}
else {
_PyErr_Restore(tstate, typ, val, tb);
}
}

/* Like PyErr_SetRaisedException(), but if an exception is already set,
set the context associated with it.
Expand Down

0 comments on commit c1261c7

Please sign in to comment.