Skip to content

Commit b7bac03

Browse files
committed
Add Py_IsFinalizing()
1 parent 64becb3 commit b7bac03

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

docs/api.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ Python 3.13
6767
6868
See `PyWeakref_GetRef() documentation <https://docs.python.org/dev/c-api/weakref.html#c.PyWeakref_GetRef>`__.
6969
70+
.. c:function:: int Py_IsFinalizing()
71+
72+
Return non-zero if the Python interpreter is shutting down, return 0
73+
otherwise.
74+
75+
Availability: Python 3.3 and newer.
76+
77+
See `Py_IsFinalizing() documentation <https://docs.python.org/dev/c-api/init.html#c.Py_IsFinalizing>`__.
78+
7079
7180
Python 3.12
7281
-----------

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Changelog
22
=========
33

4+
* 2023-08-16: Add ``Py_IsFinalizing()`` function.
45
* 2023-07-21: Add ``PyDict_GetItemRef()`` function.
56
* 2023-07-18: Add ``PyModule_Add()`` function.
67
* 2023-07-12: Add ``PyObject_GetOptionalAttr()``,

pythoncapi_compat.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,22 @@ PyModule_Add(PyObject *mod, const char *name, PyObject *value)
846846
#endif
847847

848848

849+
// gh-108014 added Py_IsFinalizing() to Python 3.13.0a1
850+
// bpo-1856 added _Py_Finalizing to Python 3.2.1b1.
851+
#if PY_VERSION_HEX < 0x030D00A1 && PY_VERSION_HEX >= 0x030201B1
852+
PYCAPI_COMPAT_STATIC_INLINE(int)
853+
Py_IsFinalizing(void)
854+
{
855+
#if PY_VERSION_HEX >= 0x030700A1
856+
// _Py_IsFinalizing() was added to Python 3.7.0a1.
857+
return _Py_IsFinalizing();
858+
#else
859+
return (_Py_Finalizing != NULL);
860+
#endif
861+
}
862+
#endif
863+
864+
849865
#ifdef __cplusplus
850866
}
851867
#endif

tests/test_pythoncapi_compat_cext.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ test_interpreter(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
328328
PyInterpreterState *interp2 = PyThreadState_GetInterpreter(tstate);
329329
assert(interp == interp2);
330330

331+
#if 0x030300A1 <= PY_VERSION_HEX
332+
// test Py_IsFinalizing()
333+
assert(Py_IsFinalizing() == 0);
334+
#endif
335+
331336
Py_RETURN_NONE;
332337
}
333338

0 commit comments

Comments
 (0)