Skip to content

Commit 875e445

Browse files
Add a fallback check in _xxsubinterpreters for is-running-main.
1 parent 00301ee commit 875e445

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

Modules/_xxsubinterpretersmodule.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ _get_current_module(void)
5656
}
5757

5858

59+
static int
60+
is_running_main(PyInterpreterState *interp)
61+
{
62+
if (_PyInterpreterState_IsRunningMain(interp)) {
63+
return 1;
64+
}
65+
// Unlike with the general C-API, we can be confident that someone
66+
// using this module for the main interpreter is doing so through
67+
// the main program. Thus we can make this extra check. This benefits
68+
// applications that embed Python but haven't been updated yet
69+
// to call_PyInterpreterState_SetRunningMain().
70+
if (_Py_IsMainInterpreter(interp)) {
71+
return 1;
72+
}
73+
return 0;
74+
}
75+
76+
5977
/* Cross-interpreter Buffer Views *******************************************/
6078

6179
// XXX Release when the original interpreter is destroyed.
@@ -509,7 +527,7 @@ interp_destroy(PyObject *self, PyObject *args, PyObject *kwds)
509527
// Ensure the interpreter isn't running.
510528
/* XXX We *could* support destroying a running interpreter but
511529
aren't going to worry about it for now. */
512-
if (_PyInterpreterState_IsRunningMain(interp)) {
530+
if (is_running_main(interp)) {
513531
PyErr_Format(PyExc_RuntimeError, "interpreter running");
514532
return NULL;
515533
}
@@ -977,7 +995,7 @@ interp_is_running(PyObject *self, PyObject *args, PyObject *kwds)
977995
if (interp == NULL) {
978996
return NULL;
979997
}
980-
if (_PyInterpreterState_IsRunningMain(interp)) {
998+
if (is_running_main(interp)) {
981999
Py_RETURN_TRUE;
9821000
}
9831001
Py_RETURN_FALSE;

0 commit comments

Comments
 (0)