Skip to content
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
gh-85283: Add PyInterpreterState_IsMain() function
This function can be used to convert the syslog extension to the
limited C API. The PyInterpreterState_Main() is not available in the
limited C API.
  • Loading branch information
vstinner committed Aug 28, 2023
commit dac62e27cdc309d769faa7520c3e8c5f2fb94c9b
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