Skip to content
Merged
Changes from 1 commit
Commits
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
Factor out _pyexc_set_context().
  • Loading branch information
ericsnowcurrently committed Apr 28, 2020
commit 99a58f4fbe7ae4f6152b4a7cfac4b6db1ee66c57
43 changes: 26 additions & 17 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,30 @@ _sharedexception_extract(_sharedexception *she, PyObject *exc)
Py_XDECREF(msgobj);
}

static void
_pyexc_set_context(PyObject *exc, PyObject *context)
{
PyObject *contexttype = (PyObject *)Py_TYPE(context);

// Set the context as though it were just caught in an "except" block.
// Doing this first makes it chain automatically.
PyErr_SetObject(contexttype, context);
// PyErr_SetExcInfo() steals references.
Py_INCREF(contexttype);
PyObject *tb = PyException_GetTraceback(context);
// Behave as though the exception was caught in this thread.
// (This is like entering an "except" block.)
PyErr_SetExcInfo(contexttype, context, tb);

// Chain "exc" as the current exception.
// exc.__context__ will be set automatically.
PyErr_SetObject((PyObject *)Py_TYPE(exc), exc);
PyErr_Clear();

// This is like leaving "except" block.
PyErr_SetExcInfo(NULL, NULL, NULL);
}

static PyObject *
_sharedexception_resolve(_sharedexception *sharedexc, PyObject *wrapperclass)
{
Expand All @@ -1147,27 +1171,12 @@ _sharedexception_resolve(_sharedexception *sharedexc, PyObject *wrapperclass)
// Set __cause__, is possible.
PyObject *cause = _sharedexception_get_cause(sharedexc);
if (cause != NULL) {
// Set the cause as though it were just caught in an "except" block.
// Doing this first makes it chain automatically.
PyObject *causetype = (PyObject *)Py_TYPE(cause);
PyErr_SetObject(causetype, cause);
// PyErr_SetExcInfo() steals references.
Py_INCREF(causetype);
PyObject *tb = PyException_GetTraceback(cause);
// Behave as though the exception was caught in this thread.
// (This is like entering an "except" block.)
PyErr_SetExcInfo(causetype, cause, tb);

// Chain "exc" as the current exception.
// exc.__context__ will be set automatically.
PyErr_SetObject((PyObject *)Py_TYPE(exc), exc);
// Set __context__ automatically.
_pyexc_set_context(exc, cause);

// Set __cause__.
Py_INCREF(cause); // PyException_SetCause() steals a reference.
PyException_SetCause(exc, cause);

// This is like leaving "except" block.
PyErr_SetExcInfo(NULL, NULL, NULL);
}

return exc;
Expand Down