Skip to content

gh-85283: Add PyInterpreterState_IsMain() function #108577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Doc/c-api/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,15 @@ All of the following functions must be called after :c:func:`Py_Initialize`.

.. versionadded:: 3.8

.. c:function:: int PyInterpreterState_IsMain(PyInterpreterState *interp)

Return true (non-zero) if the interpreter is the main interpreter.
Return false (zero) otherwise.

See also :c:func:`PyInterpreterState_Main`.

.. versionadded:: 3.13

.. c:type:: PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)

Type of a frame evaluation function.
Expand Down
1 change: 1 addition & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,10 @@ New Features
(with an underscore prefix).
(Contributed by Victor Stinner in :gh:`108014`.)

* Add :c:func:`PyInterpreterState_IsMain` function to test if an interpreter
is the main interpreter.
(Contributed by Victor Stinner in :gh:`85283`.)

Porting to Python 3.13
----------------------

Expand Down
5 changes: 5 additions & 0 deletions Include/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *);
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
PyAPI_FUNC(int) PyInterpreterState_IsMain(PyInterpreterState *interp);
#endif


/* State unique per thread */

/* New in 3.3 */
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_stable_abi_ctypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :c:func:`PyInterpreterState_IsMain` function to test if an interpreter
is the main interpreter. Patch by Victor Stinner.
2 changes: 2 additions & 0 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2452,3 +2452,5 @@
added = '3.13'
[function.PyLong_AsInt]
added = '3.13'
[function.PyInterpreterState_IsMain]
added = '3.13'
3 changes: 2 additions & 1 deletion Modules/syslogmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ static char S_log_open = 0;
static inline int
is_main_interpreter(void)
{
return (PyInterpreterState_Get() == PyInterpreterState_Main());
PyInterpreterState *interp = PyInterpreterState_Get();
return PyInterpreterState_IsMain(interp);
}

static PyObject *
Expand Down
1 change: 1 addition & 0 deletions PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,12 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
// lifecycle
//----------

int
PyInterpreterState_IsMain(PyInterpreterState *interp)
{
return _Py_IsMainInterpreter(interp);
}

/* Calling this indicates that the runtime is ready to create interpreters. */

PyStatus
Expand Down