diff options
author | Victor Stinner <vstinner@python.org> | 2019-11-20 17:34:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-20 17:34:39 +0100 |
commit | fff7bbfdb6b7c143b73b6b4b6b40e828c101110c (patch) | |
tree | 36493fb3f956d9c5867b7312a0916c38956d0e13 | |
parent | db7925a1cac361ad8c94123a38b00b70f306c48d (diff) | |
download | cpython-git-fff7bbfdb6b7c143b73b6b4b6b40e828c101110c.tar.gz |
bpo-38858: Add _Py_IsMainInterpreter(tstate) (GH-17293)
-rw-r--r-- | Include/internal/pycore_pystate.h | 2 | ||||
-rw-r--r-- | Modules/_threadmodule.c | 6 | ||||
-rw-r--r-- | Python/pystate.c | 6 |
3 files changed, 11 insertions, 3 deletions
diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 0c3c1e3df3..936e9cbc65 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -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 */ diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index befd213bfe..4a651cea6c 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -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[] = { diff --git a/Python/pystate.c b/Python/pystate.c index 2fc563bf58..0a6d035836 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -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); |