Skip to content

Commit

Permalink
Add Py_IsFinalizing()
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Aug 16, 2023
1 parent 64becb3 commit b7bac03
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ Python 3.13
See `PyWeakref_GetRef() documentation <https://docs.python.org/dev/c-api/weakref.html#c.PyWeakref_GetRef>`__.
.. c:function:: int Py_IsFinalizing()
Return non-zero if the Python interpreter is shutting down, return 0
otherwise.
Availability: Python 3.3 and newer.
See `Py_IsFinalizing() documentation <https://docs.python.org/dev/c-api/init.html#c.Py_IsFinalizing>`__.
Python 3.12
-----------
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Changelog
=========

* 2023-08-16: Add ``Py_IsFinalizing()`` function.
* 2023-07-21: Add ``PyDict_GetItemRef()`` function.
* 2023-07-18: Add ``PyModule_Add()`` function.
* 2023-07-12: Add ``PyObject_GetOptionalAttr()``,
Expand Down
16 changes: 16 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,22 @@ PyModule_Add(PyObject *mod, const char *name, PyObject *value)
#endif


// gh-108014 added Py_IsFinalizing() to Python 3.13.0a1
// bpo-1856 added _Py_Finalizing to Python 3.2.1b1.
#if PY_VERSION_HEX < 0x030D00A1 && PY_VERSION_HEX >= 0x030201B1
PYCAPI_COMPAT_STATIC_INLINE(int)
Py_IsFinalizing(void)
{
#if PY_VERSION_HEX >= 0x030700A1
// _Py_IsFinalizing() was added to Python 3.7.0a1.
return _Py_IsFinalizing();
#else
return (_Py_Finalizing != NULL);
#endif
}
#endif


#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 5 additions & 0 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ test_interpreter(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
PyInterpreterState *interp2 = PyThreadState_GetInterpreter(tstate);
assert(interp == interp2);

#if 0x030300A1 <= PY_VERSION_HEX
// test Py_IsFinalizing()
assert(Py_IsFinalizing() == 0);
#endif

Py_RETURN_NONE;
}

Expand Down

0 comments on commit b7bac03

Please sign in to comment.