Skip to content

Commit 3ff5ef2

Browse files
gh-108014: Add Py_IsFinalizing() function (#108032)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent cc58ec9 commit 3ff5ef2

File tree

9 files changed

+24
-9
lines changed

9 files changed

+24
-9
lines changed

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ Initializing and finalizing the interpreter
373373
:c:func:`Py_Initialize` is called again.
374374
375375
376+
.. c:function:: int Py_IsFinalizing()
377+
378+
Return true (non-zero) if the main Python interpreter is
379+
:term:`shutting down <interpreter shutdown>`. Return false (zero) otherwise.
380+
381+
.. versionadded:: 3.13
382+
383+
376384
.. c:function:: int Py_FinalizeEx()
377385
378386
Undo all initializations made by :c:func:`Py_Initialize` and subsequent use of
@@ -852,7 +860,7 @@ code, or when embedding the Python interpreter:
852860
.. note::
853861
Calling this function from a thread when the runtime is finalizing
854862
will terminate the thread, even if the thread was not created by Python.
855-
You can use :c:func:`!_Py_IsFinalizing` or :func:`sys.is_finalizing` to
863+
You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
856864
check if the interpreter is in process of being finalized before calling
857865
this function to avoid unwanted termination.
858866
@@ -898,7 +906,7 @@ with sub-interpreters:
898906
.. note::
899907
Calling this function from a thread when the runtime is finalizing
900908
will terminate the thread, even if the thread was not created by Python.
901-
You can use :c:func:`!_Py_IsFinalizing` or :func:`sys.is_finalizing` to
909+
You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
902910
check if the interpreter is in process of being finalized before calling
903911
this function to avoid unwanted termination.
904912
@@ -1180,7 +1188,7 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
11801188
.. note::
11811189
Calling this function from a thread when the runtime is finalizing
11821190
will terminate the thread, even if the thread was not created by Python.
1183-
You can use :c:func:`!_Py_IsFinalizing` or :func:`sys.is_finalizing` to
1191+
You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
11841192
check if the interpreter is in process of being finalized before calling
11851193
this function to avoid unwanted termination.
11861194

Diff for: Doc/library/sys.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1121,8 +1121,8 @@ always available.
11211121

11221122
.. function:: is_finalizing()
11231123

1124-
Return :const:`True` if the Python interpreter is
1125-
:term:`shutting down <interpreter shutdown>`, :const:`False` otherwise.
1124+
Return :const:`True` if the main Python interpreter is
1125+
:term:`shutting down <interpreter shutdown>`. Return :const:`False` otherwise.
11261126

11271127
.. versionadded:: 3.5
11281128

Diff for: Doc/whatsnew/3.13.rst

+4
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,10 @@ New Features
832832
not needed.
833833
(Contributed by Victor Stinner in :gh:`106004`.)
834834

835+
* Add :c:func:`Py_IsFinalizing` function: check if the main Python interpreter is
836+
:term:`shutting down <interpreter shutdown>`.
837+
(Contributed by Victor Stinner in :gh:`108014`.)
838+
835839
Porting to Python 3.13
836840
----------------------
837841

Diff for: Include/cpython/pylifecycle.h

+2
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,5 @@ PyAPI_FUNC(PyStatus) Py_NewInterpreterFromConfig(
8181
typedef void (*atexit_datacallbackfunc)(void *);
8282
PyAPI_FUNC(int) PyUnstable_AtExit(
8383
PyInterpreterState *, atexit_datacallbackfunc, void *);
84+
85+
PyAPI_FUNC(int) Py_IsFinalizing(void);

Diff for: Include/internal/pycore_pylifecycle.h

-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ extern int _Py_FdIsInteractive(FILE *fp, PyObject *filename);
9898
extern const char* _Py_gitidentifier(void);
9999
extern const char* _Py_gitversion(void);
100100

101-
extern int _Py_IsFinalizing(void);
102101
PyAPI_FUNC(int) _Py_IsInterpreterFinalizing(PyInterpreterState *interp);
103102

104103
/* Random */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :c:func:`Py_IsFinalizing` function: check if the main Python interpreter is
2+
:term:`shutting down <interpreter shutdown>`. Patch by Victor Stinner.

Diff for: Modules/_asynciomodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ future_init(FutureObj *fut, PyObject *loop)
529529
}
530530
if (is_true && !_Py_IsInterpreterFinalizing(_PyInterpreterState_GET())) {
531531
/* Only try to capture the traceback if the interpreter is not being
532-
finalized. The original motivation to add a `_Py_IsFinalizing()`
532+
finalized. The original motivation to add a `Py_IsFinalizing()`
533533
call was to prevent SIGSEGV when a Future is created in a __del__
534534
method, which is called during the interpreter shutdown and the
535535
traceback module is already unloaded.

Diff for: Python/pylifecycle.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ _PyRuntime_Finalize(void)
129129
}
130130

131131
int
132-
_Py_IsFinalizing(void)
132+
Py_IsFinalizing(void)
133133
{
134134
return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL;
135135
}

Diff for: Python/sysmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ static PyObject *
20952095
sys_is_finalizing_impl(PyObject *module)
20962096
/*[clinic end generated code: output=735b5ff7962ab281 input=f0df747a039948a5]*/
20972097
{
2098-
return PyBool_FromLong(_Py_IsFinalizing());
2098+
return PyBool_FromLong(Py_IsFinalizing());
20992099
}
21002100

21012101
#ifdef Py_STATS

0 commit comments

Comments
 (0)