From dac62e27cdc309d769faa7520c3e8c5f2fb94c9b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 28 Aug 2023 19:03:03 +0200 Subject: [PATCH] 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. --- Doc/c-api/init.rst | 9 +++++++++ Doc/data/stable_abi.dat | 1 + Doc/whatsnew/3.13.rst | 4 ++++ Include/pystate.h | 5 +++++ Lib/test/test_stable_abi_ctypes.py | 1 + .../C API/2023-08-28-19-07-42.gh-issue-85283.BFzfPr.rst | 2 ++ Misc/stable_abi.toml | 2 ++ Modules/syslogmodule.c | 3 ++- PC/python3dll.c | 1 + Python/pystate.c | 6 ++++++ 10 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/C API/2023-08-28-19-07-42.gh-issue-85283.BFzfPr.rst diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst index 60f5c81cff572c..68b2f529a74b2c 100644 --- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -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. diff --git a/Doc/data/stable_abi.dat b/Doc/data/stable_abi.dat index cc6349a0330e8c..a3585024ccaa39 100644 --- a/Doc/data/stable_abi.dat +++ b/Doc/data/stable_abi.dat @@ -326,6 +326,7 @@ function,PyInterpreterState_Delete,3.2,, function,PyInterpreterState_Get,3.9,, function,PyInterpreterState_GetDict,3.8,, function,PyInterpreterState_GetID,3.7,, +function,PyInterpreterState_IsMain,3.13,, function,PyInterpreterState_New,3.2,, function,PyIter_Check,3.8,, function,PyIter_Next,3.2,, diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index a17b549aec5d8f..40d60a22bb0d62 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -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 ---------------------- diff --git a/Include/pystate.h b/Include/pystate.h index e6b4de979c87b8..46d4aea91ca61d 100644 --- a/Include/pystate.h +++ b/Include/pystate.h @@ -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 */ diff --git a/Lib/test/test_stable_abi_ctypes.py b/Lib/test/test_stable_abi_ctypes.py index 1f3cf612c18f6d..b422f8afdf684d 100644 --- a/Lib/test/test_stable_abi_ctypes.py +++ b/Lib/test/test_stable_abi_ctypes.py @@ -355,6 +355,7 @@ def test_windows_feature_macros(self): "PyInterpreterState_Get", "PyInterpreterState_GetDict", "PyInterpreterState_GetID", + "PyInterpreterState_IsMain", "PyInterpreterState_New", "PyIter_Check", "PyIter_Next", diff --git a/Misc/NEWS.d/next/C API/2023-08-28-19-07-42.gh-issue-85283.BFzfPr.rst b/Misc/NEWS.d/next/C API/2023-08-28-19-07-42.gh-issue-85283.BFzfPr.rst new file mode 100644 index 00000000000000..0f8d06216178f1 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2023-08-28-19-07-42.gh-issue-85283.BFzfPr.rst @@ -0,0 +1,2 @@ +Add :c:func:`PyInterpreterState_IsMain` function to test if an interpreter +is the main interpreter. Patch by Victor Stinner. diff --git a/Misc/stable_abi.toml b/Misc/stable_abi.toml index 2030a085abf27c..e6502d7af02e8f 100644 --- a/Misc/stable_abi.toml +++ b/Misc/stable_abi.toml @@ -2452,3 +2452,5 @@ added = '3.13' [function.PyLong_AsInt] added = '3.13' +[function.PyInterpreterState_IsMain] + added = '3.13' diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c index 6db8de9c491dd9..674ddde3ad05db 100644 --- a/Modules/syslogmodule.c +++ b/Modules/syslogmodule.c @@ -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 * diff --git a/PC/python3dll.c b/PC/python3dll.c index ee3a7d7b4e5be1..98d42a19bb52b2 100755 --- a/PC/python3dll.c +++ b/PC/python3dll.c @@ -315,6 +315,7 @@ EXPORT_FUNC(PyInterpreterState_Delete) EXPORT_FUNC(PyInterpreterState_Get) EXPORT_FUNC(PyInterpreterState_GetDict) EXPORT_FUNC(PyInterpreterState_GetID) +EXPORT_FUNC(PyInterpreterState_IsMain) EXPORT_FUNC(PyInterpreterState_New) EXPORT_FUNC(PyIter_Check) EXPORT_FUNC(PyIter_Next) diff --git a/Python/pystate.c b/Python/pystate.c index 01651d79f9acc0..0d8a4179c6c6a1 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -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