From: Eric Snow Date: Wed, 8 Dec 2021 01:56:06 +0000 (-0700) Subject: bpo-46008: Add _PyInterpreterState_Main(). (gh-29978) X-Git-Tag: v3.11.0a3~15 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=758b74e71eb22e1e83a9eb937d1c015e461745a1;p=thirdparty%2FPython%2Fcpython.git bpo-46008: Add _PyInterpreterState_Main(). (gh-29978) PyInterpreterState_Main() is a plain function exposed in the public C-API. For internal usage we can take the more efficient approach in this PR. https://bugs.python.org/issue46008 --- diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index f139fa96f5dd..06f58fb5100f 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -21,19 +21,23 @@ _Py_IsMainThread(void) } +static inline PyInterpreterState * +_PyInterpreterState_Main(void) +{ + return _PyRuntime.interpreters.main; +} + static inline int _Py_IsMainInterpreter(PyInterpreterState *interp) { - /* Use directly _PyRuntime rather than tstate->interp->runtime, since - this function is used in performance critical code path (ceval) */ - return (interp == _PyRuntime.interpreters.main); + return (interp == _PyInterpreterState_Main()); } static inline const PyConfig * _Py_GetMainConfig(void) { - PyInterpreterState *interp = _PyRuntime.interpreters.main; + PyInterpreterState *interp = _PyInterpreterState_Main(); if (interp == NULL) { return NULL; } @@ -45,7 +49,7 @@ _Py_GetMainConfig(void) static inline int _Py_ThreadCanHandleSignals(PyInterpreterState *interp) { - return (_Py_IsMainThread() && interp == _PyRuntime.interpreters.main); + return (_Py_IsMainThread() && _Py_IsMainInterpreter(interp)); } diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index b1c2914fb0f1..2d95efebb103 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -6696,7 +6696,7 @@ os_fork1_impl(PyObject *module) { pid_t pid; - if (_PyInterpreterState_GET() != PyInterpreterState_Main()) { + if (!_Py_IsMainInterpreter(_PyInterpreterState_GET())) { PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); return NULL; } @@ -7348,7 +7348,7 @@ os_forkpty_impl(PyObject *module) int master_fd = -1; pid_t pid; - if (_PyInterpreterState_GET() != PyInterpreterState_Main()) { + if (!_Py_IsMainInterpreter(_PyInterpreterState_GET())) { PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); return NULL; } diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index f648ddab0388..2b386a11f9f6 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1953,7 +1953,7 @@ new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter) #endif { /* No current thread state, copy from the main interpreter */ - PyInterpreterState *main_interp = PyInterpreterState_Main(); + PyInterpreterState *main_interp = _PyInterpreterState_Main(); config = _PyInterpreterState_GetConfig(main_interp); } diff --git a/Python/pystate.c b/Python/pystate.c index f94019d02944..f21673e9c3ec 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1204,7 +1204,7 @@ PyInterpreterState_Head(void) PyInterpreterState * PyInterpreterState_Main(void) { - return _PyRuntime.interpreters.main; + return _PyInterpreterState_Main(); } PyInterpreterState *