Skip to content

bpo-32604: [_xxsubinterpreters] Propagate exceptions. #19768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy