Skip to content
Merged
Show file tree
Hide file tree
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
Return a excinfo object from _PyXI_ApplyError().
  • Loading branch information
ericsnowcurrently committed Nov 22, 2023
commit 362412de1bb8479efd01f5076b9f698d58edd715
8 changes: 2 additions & 6 deletions Include/internal/pycore_crossinterp.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ typedef struct _sharedexception {
_PyXI_excinfo uncaught;
} _PyXI_error;

PyAPI_FUNC(void) _PyXI_ApplyError(
_PyXI_error *err,
PyObject *exctype);
PyAPI_FUNC(PyObject *) _PyXI_ApplyError(_PyXI_error *err);


typedef struct xi_session _PyXI_session;
Expand Down Expand Up @@ -273,9 +271,7 @@ PyAPI_FUNC(int) _PyXI_Enter(
PyObject *nsupdates);
PyAPI_FUNC(void) _PyXI_Exit(_PyXI_session *session);

PyAPI_FUNC(void) _PyXI_ApplyCapturedException(
_PyXI_session *session,
PyObject *excwrapper);
PyAPI_FUNC(PyObject *) _PyXI_ApplyCapturedException(_PyXI_session *session);
PyAPI_FUNC(int) _PyXI_HasCapturedException(_PyXI_session *session);


Expand Down
22 changes: 20 additions & 2 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ _run_script(PyObject *ns, const char *codestr, Py_ssize_t codestrlen, int flags)
return 0;
}

static void
_raise_formatted(PyObject *exctype, PyObject *excinfo)
{
PyObject *formatted = PyObject_GetAttrString(excinfo, "formatted");
if (formatted == NULL) {
assert(PyErr_Occurred());
return;
}
PyErr_SetObject(exctype, formatted);
Py_DECREF(formatted);
}

static int
_run_in_interpreter(PyInterpreterState *interp,
const char *codestr, Py_ssize_t codestrlen,
Expand All @@ -237,7 +249,10 @@ _run_in_interpreter(PyInterpreterState *interp,
// Prep and switch interpreters.
if (_PyXI_Enter(&session, interp, shareables) < 0) {
assert(!PyErr_Occurred());
_PyXI_ApplyError(session.error, excwrapper);
PyObject *excinfo = _PyXI_ApplyError(session.error);
if (excinfo != NULL) {
_raise_formatted(excwrapper, excinfo);
}
assert(PyErr_Occurred());
return -1;
}
Expand All @@ -251,7 +266,10 @@ _run_in_interpreter(PyInterpreterState *interp,
// Propagate any exception out to the caller.
assert(!PyErr_Occurred());
if (res < 0) {
_PyXI_ApplyCapturedException(&session, excwrapper);
PyObject *excinfo = _PyXI_ApplyCapturedException(&session);
if (excinfo != NULL) {
_raise_formatted(excwrapper, excinfo);
}
}
else {
assert(!_PyXI_HasCapturedException(&session));
Expand Down
132 changes: 121 additions & 11 deletions Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "pycore_ceval.h" // _Py_simple_func
#include "pycore_crossinterp.h" // struct _xid
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_namespace.h" //_PyNamespace_New()
#include "pycore_pyerrors.h" // _PyErr_Clear()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_typeobject.h" // _PyType_GetModuleName()
Expand Down Expand Up @@ -1145,6 +1146,116 @@ _PyXI_excinfo_Apply(_PyXI_excinfo *info, PyObject *exctype)
Py_DECREF(formatted);
}

static PyObject *
_PyXI_excinfo_TypeAsObject(_PyXI_excinfo *info)
{
PyObject *ns = _PyNamespace_New(NULL);
if (ns == NULL) {
return NULL;
}
int empty = 1;

if (info->type.name != NULL) {
PyObject *name = PyUnicode_FromString(info->type.name);
if (name == NULL) {
goto error;
}
int res = PyObject_SetAttrString(ns, "__name__", name);
Py_DECREF(name);
if (res < 0) {
goto error;
}
empty = 0;
}

if (info->type.qualname != NULL) {
PyObject *qualname = PyUnicode_FromString(info->type.qualname);
if (qualname == NULL) {
goto error;
}
int res = PyObject_SetAttrString(ns, "__qualname__", qualname);
Py_DECREF(qualname);
if (res < 0) {
goto error;
}
empty = 0;
}

if (info->type.module != NULL) {
PyObject *module = PyUnicode_FromString(info->type.module);
if (module == NULL) {
goto error;
}
int res = PyObject_SetAttrString(ns, "__module__", module);
Py_DECREF(module);
if (res < 0) {
goto error;
}
empty = 0;
}

if (empty) {
Py_CLEAR(ns);
}

return ns;

error:
Py_DECREF(ns);
return NULL;
}

static PyObject *
_PyXI_excinfo_AsObject(_PyXI_excinfo *info)
{
PyObject *ns = _PyNamespace_New(NULL);
if (ns == NULL) {
return NULL;
}
int res;

PyObject *type = _PyXI_excinfo_TypeAsObject(info);
if (type == NULL) {
if (PyErr_Occurred()) {
goto error;
}
type = Py_NewRef(Py_None);
}
res = PyObject_SetAttrString(ns, "type", type);
Py_DECREF(type);
if (res < 0) {
goto error;
}

PyObject *msg = info->msg != NULL
? PyUnicode_FromString(info->msg)
: Py_NewRef(Py_None);
if (msg == NULL) {
goto error;
}
res = PyObject_SetAttrString(ns, "msg", msg);
Py_DECREF(msg);
if (res < 0) {
goto error;
}

PyObject *formatted = _PyXI_excinfo_format(info);
if (formatted == NULL) {
goto error;
}
res = PyObject_SetAttrString(ns, "formatted", formatted);
Py_DECREF(formatted);
if (res < 0) {
goto error;
}

return ns;

error:
Py_DECREF(ns);
return NULL;
}


/***************************/
/* short-term data sharing */
Expand Down Expand Up @@ -1238,15 +1349,12 @@ _PyXI_InitError(_PyXI_error *error, PyObject *excobj, _PyXI_errcode code)
return failure;
}

void
_PyXI_ApplyError(_PyXI_error *error, PyObject *exctype)
PyObject *
_PyXI_ApplyError(_PyXI_error *error)
{
if (exctype == NULL) {
exctype = PyExc_RuntimeError;
}
if (error->code == _PyXI_ERR_UNCAUGHT_EXCEPTION) {
// Raise an exception that proxies the propagated exception.
_PyXI_excinfo_Apply(&error->uncaught, exctype);
return _PyXI_excinfo_AsObject(&error->uncaught);
}
else if (error->code == _PyXI_ERR_NOT_SHAREABLE) {
// Propagate the exception directly.
Expand All @@ -1259,13 +1367,14 @@ _PyXI_ApplyError(_PyXI_error *error, PyObject *exctype)
if (error->uncaught.type.name != NULL || error->uncaught.msg != NULL) {
// __context__ will be set to a proxy of the propagated exception.
PyObject *exc = PyErr_GetRaisedException();
_PyXI_excinfo_Apply(&error->uncaught, exctype);
_PyXI_excinfo_Apply(&error->uncaught, PyExc_RuntimeError);
PyObject *exc2 = PyErr_GetRaisedException();
PyException_SetContext(exc, exc2);
PyErr_SetRaisedException(exc);
}
}
assert(PyErr_Occurred());
return NULL;
}

/* shared namespaces */
Expand Down Expand Up @@ -1877,14 +1986,15 @@ _capture_current_exception(_PyXI_session *session)
session->error = err;
}

void
_PyXI_ApplyCapturedException(_PyXI_session *session, PyObject *excwrapper)
PyObject *
_PyXI_ApplyCapturedException(_PyXI_session *session)
{
assert(!PyErr_Occurred());
assert(session->error != NULL);
_PyXI_ApplyError(session->error, excwrapper);
assert(PyErr_Occurred());
PyObject *res = _PyXI_ApplyError(session->error);
assert((res == NULL) != (PyErr_Occurred() == NULL));
session->error = NULL;
return res;
}

int
Expand Down