diff options
| author | Eric Snow <ericsnowcurrently@gmail.com> | 2024-03-22 00:20:20 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-22 00:20:20 (GMT) |
| commit | b3d25df8d38b79310587da54dbd88b06a16d4904 (patch) | |
| tree | 1daf220306410c2485f0bcfee557ddb0fce5022d /Modules/_xxsubinterpretersmodule.c | |
| parent | c4bf58a14f162557038a1535ca22c52b49d81d7b (diff) | |
| download | cpython-b3d25df8d38b79310587da54dbd88b06a16d4904.zip cpython-b3d25df8d38b79310587da54dbd88b06a16d4904.tar.gz cpython-b3d25df8d38b79310587da54dbd88b06a16d4904.tar.bz2 | |
gh-105716: Fix _PyInterpreterState_IsRunningMain() For Embedders (gh-117140)
When I added _PyInterpreterState_IsRunningMain() and friends last year, I tried to accommodate applications that embed Python but don't call _PyInterpreterState_SetRunningMain() (not that they're expected to). That mostly worked fine until my recent changes in gh-117049, where the subtleties with the fallback code led to failures; the change ended up breaking test_tools.test_freeze, which exercises a basic embedding situation.
The simplest fix is to drop the fallback code I originally added to _PyInterpreterState_IsRunningMain() (and later to _PyThreadState_IsRunningMain()). I've kept the fallback in the _xxsubinterpreters module though. I've also updated Py_FrozenMain() to call _PyInterpreterState_SetRunningMain().
Diffstat (limited to 'Modules/_xxsubinterpretersmodule.c')
| -rw-r--r-- | Modules/_xxsubinterpretersmodule.c | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/Modules/_xxsubinterpretersmodule.c b/Modules/_xxsubinterpretersmodule.c index befa225..5e5b3c1 100644 --- a/Modules/_xxsubinterpretersmodule.c +++ b/Modules/_xxsubinterpretersmodule.c @@ -56,6 +56,24 @@ _get_current_module(void) } +static int +is_running_main(PyInterpreterState *interp) +{ + if (_PyInterpreterState_IsRunningMain(interp)) { + return 1; + } + // Unlike with the general C-API, we can be confident that someone + // using this module for the main interpreter is doing so through + // the main program. Thus we can make this extra check. This benefits + // applications that embed Python but haven't been updated yet + // to call_PyInterpreterState_SetRunningMain(). + if (_Py_IsMainInterpreter(interp)) { + return 1; + } + return 0; +} + + /* Cross-interpreter Buffer Views *******************************************/ // XXX Release when the original interpreter is destroyed. @@ -509,7 +527,7 @@ interp_destroy(PyObject *self, PyObject *args, PyObject *kwds) // Ensure the interpreter isn't running. /* XXX We *could* support destroying a running interpreter but aren't going to worry about it for now. */ - if (_PyInterpreterState_IsRunningMain(interp)) { + if (is_running_main(interp)) { PyErr_Format(PyExc_RuntimeError, "interpreter running"); return NULL; } @@ -977,7 +995,7 @@ interp_is_running(PyObject *self, PyObject *args, PyObject *kwds) if (interp == NULL) { return NULL; } - if (_PyInterpreterState_IsRunningMain(interp)) { + if (is_running_main(interp)) { Py_RETURN_TRUE; } Py_RETURN_FALSE; |
