]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-108014: Add Py_IsFinalizing() function (#108032)
authorVictor Stinner <vstinner@python.org>
Fri, 18 Aug 2023 10:34:41 +0000 (12:34 +0200)
committerGitHub <noreply@github.com>
Fri, 18 Aug 2023 10:34:41 +0000 (12:34 +0200)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Doc/c-api/init.rst
Doc/library/sys.rst
Doc/whatsnew/3.13.rst
Include/cpython/pylifecycle.h
Include/internal/pycore_pylifecycle.h
Misc/NEWS.d/next/C API/2023-08-16-17-16-19.gh-issue-108014.wXN3CF.rst [new file with mode: 0644]
Modules/_asynciomodule.c
Python/pylifecycle.c
Python/sysmodule.c

index dd53fe2bc95696bb6858ebaa6c41f3e4dfe4f742..60f5c81cff572c71f4ddb2a2b3b4c5a9ea946b7f 100644 (file)
@@ -373,6 +373,14 @@ Initializing and finalizing the interpreter
    :c:func:`Py_Initialize` is called again.
 
 
+.. c:function:: int Py_IsFinalizing()
+
+   Return true (non-zero) if the main Python interpreter is
+   :term:`shutting down <interpreter shutdown>`. Return false (zero) 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.
 
index b6346bafbf625e96c3a5c6b42d5ac100a1285396..da2435aa4bc23d09a2aac3597c4abe4406523004 100644 (file)
@@ -1121,8 +1121,8 @@ always available.
 
 .. function:: is_finalizing()
 
-   Return :const:`True` if the Python interpreter is
-   :term:`shutting down <interpreter shutdown>`, :const:`False` otherwise.
+   Return :const:`True` if the main Python interpreter is
+   :term:`shutting down <interpreter shutdown>`. Return :const:`False` otherwise.
 
    .. versionadded:: 3.5
 
index 13ae6e595dc9937b1c4ccb51eb7c093c41f8e607..85aa81b4a98d2cc828c89de859b0d5522b63245b 100644 (file)
@@ -832,6 +832,10 @@ New Features
   not needed.
   (Contributed by Victor Stinner in :gh:`106004`.)
 
+* Add :c:func:`Py_IsFinalizing` function: check if the main Python interpreter is
+  :term:`shutting down <interpreter shutdown>`.
+  (Contributed by Victor Stinner in :gh:`108014`.)
+
 Porting to Python 3.13
 ----------------------
 
index d425a233f71000044377b7061bbd5c5aca7d73e5..11b280afa8435b8384870ef36a6b45b9feb388fc 100644 (file)
@@ -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);
index b4d5b1f1239e1db2fcfa3bee2d2facea36e05c3b..56abd57d2bd5cf606ace40abb00f1170f635abc0 100644 (file)
@@ -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 (file)
index 0000000..fee3d5b
--- /dev/null
@@ -0,0 +1,2 @@
+Add :c:func:`Py_IsFinalizing` function: check if the main Python interpreter is
+:term:`shutting down <interpreter shutdown>`. Patch by Victor Stinner.
index 39c803355ba95b0c291c8e81c9bae20e5d73ea4c..6266dc8e3555f8ddb7ccc793e3c5bf94dd0902de 100644 (file)
@@ -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.
index 90633960cb00ee65cba66088aa101b4943354b60..263ead39fa472abafb144c98101f2a7f40701763 100644 (file)
@@ -129,7 +129,7 @@ _PyRuntime_Finalize(void)
 }
 
 int
-_Py_IsFinalizing(void)
+Py_IsFinalizing(void)
 {
     return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL;
 }
index f82901181f8866569a8f5bb231368652750a40b5..54533603f1a7f87a6eb09c140a079cb4497a193a 100644 (file)
@@ -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