Skip to content

Commit 1bb4e16

Browse files
authored
Add Py_IsFinalizing() (#66)
1 parent 91e323f commit 1bb4e16

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

docs/api.rst

+9
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, PyPy 7.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

+1
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

+18
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,24 @@ 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+
// Py_IsFinalizing() was added to PyPy 7.3.0.
852+
#if (0x030201B1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030D00A1) \
853+
&& (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x7030000)
854+
PYCAPI_COMPAT_STATIC_INLINE(int)
855+
Py_IsFinalizing(void)
856+
{
857+
#if PY_VERSION_HEX >= 0x030700A1
858+
// _Py_IsFinalizing() was added to Python 3.7.0a1.
859+
return _Py_IsFinalizing();
860+
#else
861+
return (_Py_Finalizing != NULL);
862+
#endif
863+
}
864+
#endif
865+
866+
849867
#ifdef __cplusplus
850868
}
851869
#endif

tests/test_pythoncapi_compat_cext.c

+5
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 && (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x7030000)
332+
// test Py_IsFinalizing()
333+
assert(Py_IsFinalizing() == 0);
334+
#endif
335+
331336
Py_RETURN_NONE;
332337
}
333338

0 commit comments

Comments
 (0)