From e03b437018fa24cdc38fcfcb55c21807b4cfd1ea Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 16 Aug 2023 17:13:40 +0200 Subject: [PATCH 1/5] gh-108014: Add Py_IsFinalizing() function --- Doc/c-api/init.rst | 14 +++++++++++--- Doc/whatsnew/3.13.rst | 4 ++++ Include/cpython/pylifecycle.h | 2 ++ Include/internal/pycore_pylifecycle.h | 1 - .../2023-08-16-17-16-19.gh-issue-108014.wXN3CF.rst | 2 ++ Modules/_asynciomodule.c | 2 +- Python/pylifecycle.c | 2 +- Python/sysmodule.c | 2 +- 8 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/C API/2023-08-16-17-16-19.gh-issue-108014.wXN3CF.rst diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst index dd53fe2bc95696..8a1e13d8f6eb35 100644 --- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -373,6 +373,14 @@ Initializing and finalizing the interpreter :c:func:`Py_Initialize` is called again. +.. c:function:: int Py_IsFinalizing() + + Return non-zero if the Python interpreter is :term:`shutting down + `, return 0 otherwise. + + .. versionadded:: 3.13 + + .. c:function:: int Py_FinalizeEx() Undo all initializations made by :c:func:`Py_Initialize` and subsequent use of @@ -852,7 +860,7 @@ code, or when embedding the Python interpreter: .. note:: Calling this function from a thread when the runtime is finalizing will terminate the thread, even if the thread was not created by Python. - You can use :c:func:`!_Py_IsFinalizing` or :func:`sys.is_finalizing` to + You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to check if the interpreter is in process of being finalized before calling this function to avoid unwanted termination. @@ -898,7 +906,7 @@ with sub-interpreters: .. note:: Calling this function from a thread when the runtime is finalizing will terminate the thread, even if the thread was not created by Python. - You can use :c:func:`!_Py_IsFinalizing` or :func:`sys.is_finalizing` to + You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to check if the interpreter is in process of being finalized before calling this function to avoid unwanted termination. @@ -1180,7 +1188,7 @@ All of the following functions must be called after :c:func:`Py_Initialize`. .. note:: Calling this function from a thread when the runtime is finalizing will terminate the thread, even if the thread was not created by Python. - You can use :c:func:`!_Py_IsFinalizing` or :func:`sys.is_finalizing` to + You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to check if the interpreter is in process of being finalized before calling this function to avoid unwanted termination. diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 84ffd84b9f0bb6..decc4d7e4c785d 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -826,6 +826,10 @@ New Features not needed. (Contributed by Victor Stinner in :gh:`106004`.) +* Add :c:func:`Py_IsFinalizing` function: check if the Python interpreter is + :term:`shutting down `. + (Contributed by Victor Stinner in :gh:`108014`.) + Porting to Python 3.13 ---------------------- diff --git a/Include/cpython/pylifecycle.h b/Include/cpython/pylifecycle.h index d425a233f71000..11b280afa8435b 100644 --- a/Include/cpython/pylifecycle.h +++ b/Include/cpython/pylifecycle.h @@ -81,3 +81,5 @@ PyAPI_FUNC(PyStatus) Py_NewInterpreterFromConfig( typedef void (*atexit_datacallbackfunc)(void *); PyAPI_FUNC(int) PyUnstable_AtExit( PyInterpreterState *, atexit_datacallbackfunc, void *); + +PyAPI_FUNC(int) Py_IsFinalizing(void); diff --git a/Include/internal/pycore_pylifecycle.h b/Include/internal/pycore_pylifecycle.h index b4d5b1f1239e1d..56abd57d2bd5cf 100644 --- a/Include/internal/pycore_pylifecycle.h +++ b/Include/internal/pycore_pylifecycle.h @@ -98,7 +98,6 @@ extern int _Py_FdIsInteractive(FILE *fp, PyObject *filename); extern const char* _Py_gitidentifier(void); extern const char* _Py_gitversion(void); -extern int _Py_IsFinalizing(void); PyAPI_FUNC(int) _Py_IsInterpreterFinalizing(PyInterpreterState *interp); /* Random */ diff --git a/Misc/NEWS.d/next/C API/2023-08-16-17-16-19.gh-issue-108014.wXN3CF.rst b/Misc/NEWS.d/next/C API/2023-08-16-17-16-19.gh-issue-108014.wXN3CF.rst new file mode 100644 index 00000000000000..640f56ceea8bcb --- /dev/null +++ b/Misc/NEWS.d/next/C API/2023-08-16-17-16-19.gh-issue-108014.wXN3CF.rst @@ -0,0 +1,2 @@ +Add :c:func:`Py_IsFinalizing` function: check if the Python interpreter is +:term:`shutting down `. Patch by Victor Stinner. diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 39c803355ba95b..6266dc8e3555f8 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -529,7 +529,7 @@ future_init(FutureObj *fut, PyObject *loop) } if (is_true && !_Py_IsInterpreterFinalizing(_PyInterpreterState_GET())) { /* Only try to capture the traceback if the interpreter is not being - finalized. The original motivation to add a `_Py_IsFinalizing()` + finalized. The original motivation to add a `Py_IsFinalizing()` call was to prevent SIGSEGV when a Future is created in a __del__ method, which is called during the interpreter shutdown and the traceback module is already unloaded. diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 0de3abf9407899..8d68e4f36e19b6 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -129,7 +129,7 @@ _PyRuntime_Finalize(void) } int -_Py_IsFinalizing(void) +Py_IsFinalizing(void) { return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL; } diff --git a/Python/sysmodule.c b/Python/sysmodule.c index be026d95ba7e77..2def629df6ac29 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -2095,7 +2095,7 @@ static PyObject * sys_is_finalizing_impl(PyObject *module) /*[clinic end generated code: output=735b5ff7962ab281 input=f0df747a039948a5]*/ { - return PyBool_FromLong(_Py_IsFinalizing()); + return PyBool_FromLong(Py_IsFinalizing()); } #ifdef Py_STATS From f769755bfacb614508ad2262379c8edd8a4e45ee Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 17 Aug 2023 17:48:04 +0200 Subject: [PATCH 2/5] true/false --- Doc/c-api/init.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst index 8a1e13d8f6eb35..cdc835e827185c 100644 --- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -375,8 +375,8 @@ Initializing and finalizing the interpreter .. c:function:: int Py_IsFinalizing() - Return non-zero if the Python interpreter is :term:`shutting down - `, return 0 otherwise. + Return true (non-zero) if the Python interpreter is :term:`shutting down + `, return false (zero) otherwise. .. versionadded:: 3.13 From 9bd258ff7e7a37441583c398c358ba8d15a8d022 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 17 Aug 2023 17:49:25 +0200 Subject: [PATCH 3/5] Specify *main* interpreter --- Doc/c-api/init.rst | 4 ++-- Doc/library/sys.rst | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst index cdc835e827185c..60f5c81cff572c 100644 --- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -375,8 +375,8 @@ Initializing and finalizing the interpreter .. c:function:: int Py_IsFinalizing() - Return true (non-zero) if the Python interpreter is :term:`shutting down - `, return false (zero) otherwise. + Return true (non-zero) if the main Python interpreter is + :term:`shutting down `. Return false (zero) otherwise. .. versionadded:: 3.13 diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index 33391d11ab392d..92f4ed96990e90 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -1121,8 +1121,8 @@ always available. .. function:: is_finalizing() - Return :const:`True` if the Python interpreter is - :term:`shutting down `, :const:`False` otherwise. + Return :const:`True` if the main Python interpreter is + :term:`shutting down `. Return :const:`False` otherwise. .. versionadded:: 3.5 From fe6216c2d6b6acdf80480c2304cb68e5298c1d10 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 17 Aug 2023 22:01:02 +0200 Subject: [PATCH 4/5] Update Misc/NEWS.d/next/C API/2023-08-16-17-16-19.gh-issue-108014.wXN3CF.rst Co-authored-by: Serhiy Storchaka --- .../next/C API/2023-08-16-17-16-19.gh-issue-108014.wXN3CF.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/C API/2023-08-16-17-16-19.gh-issue-108014.wXN3CF.rst b/Misc/NEWS.d/next/C API/2023-08-16-17-16-19.gh-issue-108014.wXN3CF.rst index 640f56ceea8bcb..fee3d5b941dc8d 100644 --- a/Misc/NEWS.d/next/C API/2023-08-16-17-16-19.gh-issue-108014.wXN3CF.rst +++ b/Misc/NEWS.d/next/C API/2023-08-16-17-16-19.gh-issue-108014.wXN3CF.rst @@ -1,2 +1,2 @@ -Add :c:func:`Py_IsFinalizing` function: check if the Python interpreter is +Add :c:func:`Py_IsFinalizing` function: check if the main Python interpreter is :term:`shutting down `. Patch by Victor Stinner. From fa76703682449ce9cc8cb5db96f56b394201483b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 17 Aug 2023 22:01:12 +0200 Subject: [PATCH 5/5] Update Doc/whatsnew/3.13.rst Co-authored-by: Serhiy Storchaka --- Doc/whatsnew/3.13.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index decc4d7e4c785d..2ec82e5d29c612 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -826,7 +826,7 @@ New Features not needed. (Contributed by Victor Stinner in :gh:`106004`.) -* Add :c:func:`Py_IsFinalizing` function: check if the Python interpreter is +* Add :c:func:`Py_IsFinalizing` function: check if the main Python interpreter is :term:`shutting down `. (Contributed by Victor Stinner in :gh:`108014`.)