Skip to content

gh-76785: Crossinterp utils additions #111530

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
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
67a88c2
Factor out _Py_excinfo.
ericsnowcurrently Oct 24, 2023
a9ea9ac
Extract _PyXI_errcode.
ericsnowcurrently Oct 25, 2023
33d91af
Extract _PyXI_exception_info.
ericsnowcurrently Oct 25, 2023
2424e33
Extract _PyXI_namespace.
ericsnowcurrently Oct 25, 2023
23d6959
Factor out _enter_interpreter(), _exit_interpreter(), etc.
ericsnowcurrently Oct 23, 2023
b08249f
Move enter/exit to crossinterp.c.
ericsnowcurrently Oct 27, 2023
773f5ab
Factor out _sharednsitem_set_value().
ericsnowcurrently Oct 23, 2023
cf7354e
Add _PyXI_NamespaceFromNames().
ericsnowcurrently Oct 31, 2023
6b43620
Add a default arg to _PyXI_ApplyNamespace().
ericsnowcurrently Oct 31, 2023
caef717
Allocate xid dynamically when in target interpreter.
ericsnowcurrently Oct 31, 2023
0bd42e0
Add _PyXI_FillNamespaceFromDict().
ericsnowcurrently Oct 31, 2023
a230f77
Add xid_state structs and lifecycle funcs.
ericsnowcurrently Oct 31, 2023
5675f86
Add PyExc_NotShareableError.
ericsnowcurrently Oct 31, 2023
45488f2
Propagate the ValueError when a value is not shareable.
ericsnowcurrently Oct 31, 2023
6f07364
Propagate errors in _PyXI_Enter() directly.
ericsnowcurrently Oct 31, 2023
0201b7f
Factor out _init_not_shareable_error_type() and _fini_not_shareable_e…
ericsnowcurrently Nov 1, 2023
d32a918
Drop some duplicate lines.
ericsnowcurrently Nov 1, 2023
1d4fc87
Fix a comment.
ericsnowcurrently Nov 1, 2023
88c9d54
Call _PyXI_Fini() *before* the interpreter is cleared.
ericsnowcurrently Nov 1, 2023
2edcb49
Fix init/fini.
ericsnowcurrently Nov 1, 2023
8e53752
Add _get_not_shareable_error_type().
ericsnowcurrently Nov 1, 2023
53764c1
Export fewer symbols.
ericsnowcurrently Nov 1, 2023
cacf969
Merge branch 'main' into crossinterp-utils-additions
ericsnowcurrently Nov 1, 2023
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
Extract _PyXI_exception_info.
  • Loading branch information
ericsnowcurrently committed Oct 30, 2023
commit 33d91af4256394822840c9330ca234659579593e
25 changes: 25 additions & 0 deletions Include/internal/pycore_crossinterp.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

#include "pycore_pyerrors.h"


/**************************/
/* cross-interpreter data */
/**************************/

/***************************/
/* cross-interpreter calls */
Expand Down Expand Up @@ -150,6 +156,25 @@ PyAPI_FUNC(int) _PyXI_ApplyErrorCode(
PyInterpreterState *interp);


typedef struct _sharedexception {
// The originating interpreter.
PyInterpreterState *interp;
// The kind of error to propagate.
_PyXI_errcode code;
// The exception information to propagate, if applicable.
// This is populated only for _PyXI_ERR_UNCAUGHT_EXCEPTION.
_Py_excinfo uncaught;
} _PyXI_exception_info;

PyAPI_FUNC(const char *) _PyXI_InitExceptionInfo(
_PyXI_exception_info *info,
PyObject *exc,
_PyXI_errcode code);
PyAPI_FUNC(void) _PyXI_ApplyExceptionInfo(
_PyXI_exception_info *info,
PyObject *exctype);


#ifdef __cplusplus
}
#endif
Expand Down
85 changes: 9 additions & 76 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,74 +114,6 @@ clear_module_state(module_state *state)
}


/* exception info ***********************************************************/

static const char *
_excinfo_bind(PyObject *exc, _Py_excinfo *info, _PyXI_errcode *p_code)
{
assert(exc != NULL);

const char *failure = _Py_excinfo_InitFromException(info, exc);
if (failure != NULL) {
// We failed to initialize info->uncaught.
// XXX Print the excobj/traceback? Emit a warning?
// XXX Print the current exception/traceback?
if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
*p_code = _PyXI_ERR_NO_MEMORY;
}
else {
*p_code = _PyXI_ERR_OTHER;
}
PyErr_Clear();
}
else {
assert(!PyErr_Occurred());
*p_code = _PyXI_ERR_UNCAUGHT_EXCEPTION;
}
return failure;
}

typedef struct _sharedexception {
PyInterpreterState *interp;
_PyXI_errcode code;
_Py_excinfo uncaught;
} _sharedexception;

static const char *
_sharedexception_bind(PyObject *exc, _PyXI_errcode code, _sharedexception *sharedexc)
{
if (sharedexc->interp == NULL) {
sharedexc->interp = PyInterpreterState_Get();
}

const char *failure = NULL;
if (code == _PyXI_ERR_UNCAUGHT_EXCEPTION) {
failure = _excinfo_bind(exc, &sharedexc->uncaught, &sharedexc->code);
assert(sharedexc->code != _PyXI_ERR_NO_ERROR);
}
else {
assert(exc == NULL);
assert(code != _PyXI_ERR_NO_ERROR);
sharedexc->code = code;
_Py_excinfo_Clear(&sharedexc->uncaught);
}
return failure;
}

static void
_sharedexception_apply(_sharedexception *exc, PyObject *wrapperclass)
{
if (exc->code == _PyXI_ERR_UNCAUGHT_EXCEPTION) {
_Py_excinfo_Apply(&exc->uncaught, wrapperclass);
}
else {
assert(exc->code != _PyXI_ERR_NO_ERROR);
(void)_PyXI_ApplyErrorCode(exc->code, exc->interp);
assert(PyErr_Occurred());
}
}


/* data-sharing-specific code ***********************************************/

struct _sharednsitem {
Expand Down Expand Up @@ -431,7 +363,7 @@ exceptions_init(PyObject *mod)
static int
_run_script(PyInterpreterState *interp,
const char *codestr, Py_ssize_t codestrlen,
_sharedns *shared, _sharedexception *sharedexc, int flags)
_sharedns *shared, _PyXI_exception_info *sharedexc, int flags)
{
PyObject *excval = NULL;
_PyXI_errcode errcode = _PyXI_ERR_UNCAUGHT_EXCEPTION;
Expand Down Expand Up @@ -489,7 +421,7 @@ _run_script(PyInterpreterState *interp,
}
_PyInterpreterState_SetNotRunningMain(interp);

*sharedexc = (_sharedexception){0};
*sharedexc = (_PyXI_exception_info){ NULL };
return 0;

error:
Expand All @@ -501,20 +433,21 @@ _run_script(PyInterpreterState *interp,
else {
assert(!PyErr_Occurred());
}
const char *failure = _sharedexception_bind(excval, errcode, sharedexc);
const char *failure = _PyXI_InitExceptionInfo(sharedexc, excval, errcode);
if (failure != NULL) {
fprintf(stderr,
"RunFailedError: script raised an uncaught exception (%s)",
failure);
}
if (excval != NULL) {
if (sharedexc->code == _PyXI_ERR_UNCAUGHT_EXCEPTION) {
assert(excval != NULL);
// XXX Instead, store the rendered traceback on sharedexc,
// attach it to the exception when applied,
// and teach PyErr_Display() to print it.
PyErr_Display(NULL, excval, NULL);
Py_DECREF(excval);
}
if (errcode != _PyXI_ERR_ALREADY_RUNNING) {
Py_XDECREF(excval);
if (sharedexc->code != _PyXI_ERR_ALREADY_RUNNING) {
_PyInterpreterState_SetNotRunningMain(interp);
}
assert(!PyErr_Occurred());
Expand Down Expand Up @@ -545,7 +478,7 @@ _run_in_interpreter(PyObject *mod, PyInterpreterState *interp,
}

// Run the script.
_sharedexception exc = (_sharedexception){ .interp = interp };
_PyXI_exception_info exc = (_PyXI_exception_info){ .interp = interp };
int result = _run_script(interp, codestr, codestrlen, shared, &exc, flags);

// Switch back.
Expand All @@ -558,7 +491,7 @@ _run_in_interpreter(PyObject *mod, PyInterpreterState *interp,
// Propagate any exception out to the caller.
if (result < 0) {
assert(!PyErr_Occurred());
_sharedexception_apply(&exc, state->RunFailedError);
_PyXI_ApplyExceptionInfo(&exc, state->RunFailedError);
assert(PyErr_Occurred());
}

Expand Down
56 changes: 56 additions & 0 deletions Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,59 @@ _PyXI_ApplyErrorCode(_PyXI_errcode code, PyInterpreterState *interp)
assert(PyErr_Occurred());
return -1;
}

/* shared exceptions */

const char *
_PyXI_InitExceptionInfo(_PyXI_exception_info *info,
PyObject *excobj, _PyXI_errcode code)
{
if (info->interp == NULL) {
info->interp = PyInterpreterState_Get();
}

const char *failure = NULL;
if (code == _PyXI_ERR_UNCAUGHT_EXCEPTION) {
// There is an unhandled exception we need to propagate.
failure = _Py_excinfo_InitFromException(&info->uncaught, excobj);
if (failure != NULL) {
// We failed to initialize info->uncaught.
// XXX Print the excobj/traceback? Emit a warning?
// XXX Print the current exception/traceback?
if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
info->code = _PyXI_ERR_NO_MEMORY;
}
else {
info->code = _PyXI_ERR_OTHER;
}
PyErr_Clear();
}
else {
info->code = code;
}
assert(info->code != _PyXI_ERR_NO_ERROR);
}
else {
// There is an error code we need to propagate.
assert(excobj == NULL);
assert(code != _PyXI_ERR_NO_ERROR);
info->code = code;
_Py_excinfo_Clear(&info->uncaught);
}
return failure;
}

void
_PyXI_ApplyExceptionInfo(_PyXI_exception_info *info, PyObject *exctype)
{
if (info->code == _PyXI_ERR_UNCAUGHT_EXCEPTION) {
// Raise an exception that proxies the propagated exception.
_Py_excinfo_Apply(&info->uncaught, exctype);
}
else {
// Raise an exception corresponding to the code.
assert(info->code != _PyXI_ERR_NO_ERROR);
(void)_PyXI_ApplyErrorCode(info->code, info->interp);
}
assert(PyErr_Occurred());
}
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