]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46008: Add _PyInterpreterState_Main(). (gh-29978)
authorEric Snow <ericsnowcurrently@gmail.com>
Wed, 8 Dec 2021 01:56:06 +0000 (18:56 -0700)
committerGitHub <noreply@github.com>
Wed, 8 Dec 2021 01:56:06 +0000 (18:56 -0700)
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

Include/internal/pycore_pystate.h
Modules/posixmodule.c
Python/pylifecycle.c
Python/pystate.c

index f139fa96f5dd968efabc7f1d7deef9d6ff8caacd..06f58fb5100f08c13483a2f35411470ef6706ae5 100644 (file)
@@ -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));
 }
 
 
index b1c2914fb0f1fb49eef9804e170a912a1b5d9627..2d95efebb1033f275468213d2f4b5b817e612fd3 100644 (file)
@@ -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;
     }
index f648ddab0388f0be23970bc349ec4762f8ba1ed2..2b386a11f9f6e84d1aab218cf8ba0f548a4d2d95 100644 (file)
@@ -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);
     }
 
index f94019d029441f53d0f6c62904771408ce899d7b..f21673e9c3ec6edd4c658b4efb81b8aeb108be04 100644 (file)
@@ -1204,7 +1204,7 @@ PyInterpreterState_Head(void)
 PyInterpreterState *
 PyInterpreterState_Main(void)
 {
-    return _PyRuntime.interpreters.main;
+    return _PyInterpreterState_Main();
 }
 
 PyInterpreterState *