]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38858: Add _Py_IsMainInterpreter(tstate) (GH-17293)
authorVictor Stinner <vstinner@python.org>
Wed, 20 Nov 2019 16:34:39 +0000 (17:34 +0100)
committerGitHub <noreply@github.com>
Wed, 20 Nov 2019 16:34:39 +0000 (17:34 +0100)
Include/internal/pycore_pystate.h
Modules/_threadmodule.c
Python/pystate.c

index 0c3c1e3df34de17afccc18d47560d6380fc0feba..936e9cbc65f7ade22fe5428b151d5c2e0393f42b 100644 (file)
@@ -269,6 +269,8 @@ PyAPI_FUNC(void) _PyRuntime_Finalize(void);
 #define _Py_CURRENTLY_FINALIZING(runtime, tstate) \
     (runtime->finalizing == tstate)
 
+PyAPI_FUNC(int) _Py_IsMainInterpreter(PyThreadState* tstate);
+
 
 /* Variable and macro for in-line access to current thread
    and interpreter state */
index befd213bfebaa9ef4fb8429c0c1b5fd0443c9bc5..4a651cea6c7ff70428fb8945668469b2d3818cd1 100644 (file)
@@ -1466,9 +1466,9 @@ static PyObject *
 _thread__is_main_interpreter_impl(PyObject *module)
 /*[clinic end generated code: output=7dd82e1728339adc input=cc1eb00fd4598915]*/
 {
-    _PyRuntimeState *runtime = &_PyRuntime;
-    PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
-    return PyBool_FromLong(interp == runtime->interpreters.main);
+    PyThreadState *tstate = _PyThreadState_GET();
+    int is_main = _Py_IsMainInterpreter(tstate);
+    return PyBool_FromLong(is_main);
 }
 
 static PyMethodDef thread_methods[] = {
index 2fc563bf5836d93fa18a56a660efec38e3f550ad..0a6d035836e6e87c9157b2fe0ae2e6806cc96898 100644 (file)
@@ -159,6 +159,12 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
 #define HEAD_UNLOCK(runtime) \
     PyThread_release_lock((runtime)->interpreters.mutex)
 
+int
+_Py_IsMainInterpreter(PyThreadState* tstate)
+{
+    return (tstate->interp == tstate->interp->runtime->interpreters.main);
+}
+
 /* Forward declaration */
 static void _PyGILState_NoteThreadState(
     struct _gilstate_runtime_state *gilstate, PyThreadState* tstate);