Skip to content

Commit 9e81a3b

Browse files
committed
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.
1 parent 4116592 commit 9e81a3b

File tree

7 files changed

+30
-1
lines changed

7 files changed

+30
-1
lines changed

Diff for: Doc/c-api/init.rst

+9
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,15 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
11261126
11271127
.. versionadded:: 3.8
11281128
1129+
.. c:function:: int PyInterpreterState_IsMain(PyInterpreterState *interp)
1130+
1131+
Return true (non-zero) if the interpreter is the main interpreter.
1132+
Return false (zero) otherwise.
1133+
1134+
See also :c:func:`PyInterpreterState_Main`.
1135+
1136+
.. versionadded:: 3.13
1137+
11291138
.. c:type:: PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
11301139
11311140
Type of a frame evaluation function.

Diff for: Doc/whatsnew/3.13.rst

+4
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,10 @@ New Features
886886
(with an underscore prefix).
887887
(Contributed by Victor Stinner in :gh:`108014`.)
888888

889+
* Add :c:func:`PyInterpreterState_IsMain` function to test if an interpreter
890+
is the main interpreter.
891+
(Contributed by Victor Stinner in :gh:`85283`.)
892+
889893
Porting to Python 3.13
890894
----------------------
891895

Diff for: Include/pystate.h

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ PyAPI_FUNC(int64_t) PyInterpreterState_GetID(PyInterpreterState *);
3737
#endif
3838
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
3939

40+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
41+
PyAPI_FUNC(int) PyInterpreterState_IsMain(PyInterpreterState *interp);
42+
#endif
43+
44+
4045
/* State unique per thread */
4146

4247
/* New in 3.3 */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :c:func:`PyInterpreterState_IsMain` function to test if an interpreter
2+
is the main interpreter. Patch by Victor Stinner.

Diff for: Misc/stable_abi.toml

+2
Original file line numberDiff line numberDiff line change
@@ -2452,3 +2452,5 @@
24522452
added = '3.13'
24532453
[function.PyLong_AsInt]
24542454
added = '3.13'
2455+
[function.PyInterpreterState_IsMain]
2456+
added = '3.13'

Diff for: Modules/syslogmodule.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ static char S_log_open = 0;
6969
static inline int
7070
is_main_interpreter(void)
7171
{
72-
return (PyInterpreterState_Get() == PyInterpreterState_Main());
72+
PyInterpreterState *interp = PyInterpreterState_Get();
73+
return PyInterpreterState_IsMain(interp);
7374
}
7475

7576
static PyObject *

Diff for: Python/pystate.c

+6
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,12 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
583583
// lifecycle
584584
//----------
585585

586+
int
587+
PyInterpreterState_IsMain(PyInterpreterState *interp)
588+
{
589+
return _Py_IsMainInterpreter(interp);
590+
}
591+
586592
/* Calling this indicates that the runtime is ready to create interpreters. */
587593

588594
PyStatus

0 commit comments

Comments
 (0)