Skip to content

gh-76785: Handle Legacy Interpreters Properly #117490

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
19 commits
Select commit Hold shift + click to select a range
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
Require the interpreter to be ready for most operations.
  • Loading branch information
ericsnowcurrently committed Apr 11, 2024
commit cd7d5ee63a1b11214926eaf3a049bbdb248ff430
2 changes: 2 additions & 0 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
PyAPI_FUNC(int) _PyInterpreterState_IDIncref(PyInterpreterState *);
PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);

PyAPI_FUNC(int) _PyInterpreterState_IsReady(PyInterpreterState *interp);

PyAPI_FUNC(long) _PyInterpreterState_GetWhence(PyInterpreterState *interp);
extern void _PyInterpreterState_SetWhence(
PyInterpreterState *interp,
Expand Down
61 changes: 46 additions & 15 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ get_whence(PyInterpreterState *interp)


static PyInterpreterState *
resolve_interp(PyObject *idobj, int restricted, const char *op)
resolve_interp(PyObject *idobj, int restricted, int reqready, const char *op)
{
PyInterpreterState *interp;
if (idobj == NULL) {
Expand All @@ -512,6 +512,18 @@ resolve_interp(PyObject *idobj, int restricted, const char *op)
}
}

if (reqready && !_PyInterpreterState_IsReady(interp)) {
if (idobj == NULL) {
PyErr_Format(PyExc_InterpreterError,
"cannot %s current interpreter (not ready)", op);
}
else {
PyErr_Format(PyExc_InterpreterError,
"cannot %s interpreter %R (not ready)", op, idobj);
}
return NULL;
}

if (restricted && get_whence(interp) != _PyInterpreterState_WHENCE_STDLIB) {
if (idobj == NULL) {
PyErr_Format(PyExc_InterpreterError,
Expand Down Expand Up @@ -619,6 +631,7 @@ interp_create(PyObject *self, PyObject *args, PyObject *kwds)
_PyErr_ChainExceptions1(exc);
return NULL;
}
assert(_PyInterpreterState_IsReady(interp));

PyObject *idobj = _PyInterpreterState_GetIDObject(interp);
if (idobj == NULL) {
Expand Down Expand Up @@ -664,7 +677,9 @@ interp_destroy(PyObject *self, PyObject *args, PyObject *kwds)
}

// Look up the interpreter.
PyInterpreterState *interp = resolve_interp(id, restricted, "destroy");
int reqready = 0;
PyInterpreterState *interp = \
resolve_interp(id, restricted, reqready, "destroy");
if (interp == NULL) {
return NULL;
}
Expand Down Expand Up @@ -746,6 +761,7 @@ interp_get_current(PyObject *self, PyObject *Py_UNUSED(ignored))
if (interp == NULL) {
return NULL;
}
assert(_PyInterpreterState_IsReady(interp));
return get_summary(interp);
}

Expand All @@ -759,6 +775,7 @@ static PyObject *
interp_get_main(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyInterpreterState *interp = _PyInterpreterState_Main();
assert(_PyInterpreterState_IsReady(interp));
return get_summary(interp);
}

Expand All @@ -782,8 +799,9 @@ interp_set___main___attrs(PyObject *self, PyObject *args, PyObject *kwargs)
}

// Look up the interpreter.
PyInterpreterState *interp = resolve_interp(id, restricted,
"update __main__ for");
int reqready = 1;
PyInterpreterState *interp = \
resolve_interp(id, restricted, reqready, "update __main__ for");
if (interp == NULL) {
return NULL;
}
Expand Down Expand Up @@ -942,7 +960,9 @@ interp_exec(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}

PyInterpreterState *interp = resolve_interp(id, restricted, "exec code for");
int reqready = 1;
PyInterpreterState *interp = \
resolve_interp(id, restricted, reqready, "exec code for");
if (interp == NULL) {
return NULL;
}
Expand Down Expand Up @@ -1004,7 +1024,9 @@ interp_call(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}

PyInterpreterState *interp = resolve_interp(id, restricted, "make a call in");
int reqready = 1;
PyInterpreterState *interp = \
resolve_interp(id, restricted, reqready, "make a call in");
if (interp == NULL) {
return NULL;
}
Expand Down Expand Up @@ -1060,7 +1082,9 @@ interp_run_string(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}

PyInterpreterState *interp = resolve_interp(id, restricted, "run a string in");
int reqready = 1;
PyInterpreterState *interp = \
resolve_interp(id, restricted, reqready, "run a string in");
if (interp == NULL) {
return NULL;
}
Expand Down Expand Up @@ -1102,8 +1126,9 @@ interp_run_func(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}

PyInterpreterState *interp = resolve_interp(id, restricted,
"run a function in");
int reqready = 1;
PyInterpreterState *interp = \
resolve_interp(id, restricted, reqready, "run a function in");
if (interp == NULL) {
return NULL;
}
Expand Down Expand Up @@ -1172,8 +1197,9 @@ interp_is_running(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}

PyInterpreterState *interp = resolve_interp(id, restricted,
"check if running for");
int reqready = 1;
PyInterpreterState *interp = \
resolve_interp(id, restricted, reqready, "check if running for");
if (interp == NULL) {
return NULL;
}
Expand Down Expand Up @@ -1206,8 +1232,9 @@ interp_get_config(PyObject *self, PyObject *args, PyObject *kwds)
idobj = NULL;
}

PyInterpreterState *interp = resolve_interp(idobj, restricted,
"get the config of");
int reqready = 0;
PyInterpreterState *interp = \
resolve_interp(idobj, restricted, reqready, "get the config of");
if (interp == NULL) {
return NULL;
}
Expand Down Expand Up @@ -1272,7 +1299,9 @@ interp_incref(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}

PyInterpreterState *interp = resolve_interp(id, restricted, "incref");
int reqready = 1;
PyInterpreterState *interp = \
resolve_interp(id, restricted, reqready, "incref");
if (interp == NULL) {
return NULL;
}
Expand All @@ -1299,7 +1328,9 @@ interp_decref(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}

PyInterpreterState *interp = resolve_interp(id, restricted, "decref");
int reqready = 1;
PyInterpreterState *interp = \
resolve_interp(id, restricted, reqready, "decref");
if (interp == NULL) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1896,7 +1896,7 @@ _PyXI_EndInterpreter(PyInterpreterState *interp,
long whence = _PyInterpreterState_GetWhence(interp);
assert(whence != _PyInterpreterState_WHENCE_RUNTIME);
if (whence == _PyInterpreterState_WHENCE_UNKNOWN) {
assert(!interp->_ready);
assert(!_PyInterpreterState_IsReady(interp));
PyThreadState *tstate = PyThreadState_New(interp);
save_tstate = PyThreadState_Swap(tstate);
_PyInterpreterState_Clear(tstate);
Expand Down
7 changes: 7 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,13 @@ _PyInterpreterState_ReinitRunningMain(PyThreadState *tstate)
// accessors
//----------

int
_PyInterpreterState_IsReady(PyInterpreterState *interp)
{
return interp->_ready;
}


static inline int
check_interpreter_whence(long whence)
{
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