Skip to content

Commit fff7bbf

Browse files
authored
bpo-38858: Add _Py_IsMainInterpreter(tstate) (GH-17293)
1 parent db7925a commit fff7bbf

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

Include/internal/pycore_pystate.h

+2
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ PyAPI_FUNC(void) _PyRuntime_Finalize(void);
269269
#define _Py_CURRENTLY_FINALIZING(runtime, tstate) \
270270
(runtime->finalizing == tstate)
271271

272+
PyAPI_FUNC(int) _Py_IsMainInterpreter(PyThreadState* tstate);
273+
272274

273275
/* Variable and macro for in-line access to current thread
274276
and interpreter state */

Modules/_threadmodule.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1466,9 +1466,9 @@ static PyObject *
14661466
_thread__is_main_interpreter_impl(PyObject *module)
14671467
/*[clinic end generated code: output=7dd82e1728339adc input=cc1eb00fd4598915]*/
14681468
{
1469-
_PyRuntimeState *runtime = &_PyRuntime;
1470-
PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
1471-
return PyBool_FromLong(interp == runtime->interpreters.main);
1469+
PyThreadState *tstate = _PyThreadState_GET();
1470+
int is_main = _Py_IsMainInterpreter(tstate);
1471+
return PyBool_FromLong(is_main);
14721472
}
14731473

14741474
static PyMethodDef thread_methods[] = {

Python/pystate.c

+6
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
159159
#define HEAD_UNLOCK(runtime) \
160160
PyThread_release_lock((runtime)->interpreters.mutex)
161161

162+
int
163+
_Py_IsMainInterpreter(PyThreadState* tstate)
164+
{
165+
return (tstate->interp == tstate->interp->runtime->interpreters.main);
166+
}
167+
162168
/* Forward declaration */
163169
static void _PyGILState_NoteThreadState(
164170
struct _gilstate_runtime_state *gilstate, PyThreadState* tstate);

0 commit comments

Comments
 (0)