From 4e1d6d2efe0adbc5633a6546c29d5a7157656ed4 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 14 Nov 2026 20:33:01 +0100 Subject: [PATCH] PyUnstable_SetImmortal() is not available on the limited C API --- docs/api.rst | 2 ++ pythoncapi_compat.h | 4 +++- tests/test_pythoncapi_compat_cext.c | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/api.rst b/docs/api.rst index 5bc1541..9f327a0 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -349,6 +349,8 @@ Python 3.14 Not available on PyPy. ``Py_REFCNT()`` semantics is different on PyPy. + Not available on the limited C API. + .. c:function:: void PyUnstable_EnableTryIncRef(PyObject *op) See `PyUnstable_EnableTryIncRef() documentation `__. diff --git a/pythoncapi_compat.h b/pythoncapi_compat.h index a7a0b36..3fee8d2 100644 --- a/pythoncapi_compat.h +++ b/pythoncapi_compat.h @@ -2543,7 +2543,9 @@ static inline int PyUnstable_Object_IsUniquelyReferenced(PyObject *obj) // gh-128926 added PyUnstable_TryIncRef() and PyUnstable_EnableTryIncRef() to // Python 3.14. Adapted from _Py_TryIncref() and _PyObject_SetMaybeWeakref(). // Not available on PyPy: Py_REFCNT() semantics is different on PyPy. -#if PY_VERSION_HEX < _Py_PACK_VERSION(3, 14) && !defined(PYPY_VERSION) +// Not available on the limited C API. +#if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 14) \ + && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline int PyUnstable_TryIncRef(PyObject *op) { #ifndef Py_GIL_DISABLED diff --git a/tests/test_pythoncapi_compat_cext.c b/tests/test_pythoncapi_compat_cext.c index d63ce4c..0f8039b 100644 --- a/tests/test_pythoncapi_compat_cext.c +++ b/tests/test_pythoncapi_compat_cext.c @@ -2593,6 +2593,7 @@ test_try_incref(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) #endif // !defined(PYPY_VERSION) && !defined(Py_LIMITED_API) +// Test PyUnstable_SetImmortal() #if (_Py_PACK_VERSION(3, 13) <= PY_VERSION_HEX \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static PyObject *