From 996957004480fd9570511dc60a979bdb636ed52b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 16 Nov 2026 17:16:34 +0100 Subject: [PATCH 1/3] gh-158401: Implement Py_INCREF() as function call in stable ABI Py_INCREF(), Py_XINCREF(), Py_DECREF() and Py_XDECREF() are now implemented as function calls on all limited C API versions. Previously, they were only implemented as function calls on the limited C API version 3.12 or newer. Previously, if a C extension was built with the limited C API version 3.11 or newer, Py_DECREF() was implemented as "op->ob_refcnt--". But Python 3.12 made Py_DECREF() more complicated to implement immortal objects in an efficient way. "op->ob_refcnt--" can convert an immortal object to a mortal object which causes various kinds of nasty bugs. --- Include/refcount.h | 23 +++++++++---------- ...-11-16-17-25-12.gh-issue-158401.ADBBRG.rst | 4 ++++ 2 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 Misc/NEWS.d/next/C_API/2026-11-16-17-25-12.gh-issue-158401.ADBBRG.rst diff --git a/Include/refcount.h b/Include/refcount.h index 9b681f6a254a42..34ea22bb97a33c 100644 --- a/Include/refcount.h +++ b/Include/refcount.h @@ -256,11 +256,11 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op) { #if (defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))) \ || defined(_Py_OPAQUE_PYOBJECT) - // Stable ABI implements Py_INCREF() as a function call on limited C API - // version 3.12 and newer, abi3t, and on Python built in debug mode. - // _Py_IncRef() was added to Python 3.10.0a7, use Py_IncRef() on older versions. - // Py_IncRef() accepts NULL whereas _Py_IncRef() doesn't. -# if Py_LIMITED_API+0 >= 0x030a00A7 + // Since Python 3.16, the atable ABI implements Py_INCREF() as a function + // call on all limited C API versions. _Py_IncRef() was added to Python + // 3.10.0a7, use Py_IncRef() on older versions. Py_IncRef() accepts NULL, + // whereas _Py_IncRef() doesn't. +# if Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10) _Py_IncRef(op); # else Py_IncRef(op); @@ -324,14 +324,13 @@ PyAPI_FUNC(void) _Py_MergeZeroLocalRefcount(PyObject *); #endif // Py_GIL_DISABLED #endif // Py_LIMITED_API -#if (defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))) \ - || defined(_Py_OPAQUE_PYOBJECT) -// Stable ABI implements Py_DECREF() as a function call on limited C API -// version 3.12 and newer, abi3t, and on Python built in debug mode. -// _Py_DecRef() was added to Python 3.10.0a7, use Py_DecRef() on older versions. -// Py_DecRef() accepts NULL whereas _Py_DecRef() doesn't. +#if defined(Py_LIMITED_API) || defined(_Py_OPAQUE_PYOBJECT) +// Since Python 3.16, the stable ABI implements Py_DECREF() as a function call +// on all limited C API version. _Py_DecRef() was added to Python 3.10.0a7, +// use Py_DecRef() on older versions. Py_DecRef() accepts NULL, whereas +// _Py_DecRef() doesn't. static inline void Py_DECREF(PyObject *op) { -# if Py_LIMITED_API+0 >= 0x030a00A7 +# if Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10) _Py_DecRef(op); # else Py_DecRef(op); diff --git a/Misc/NEWS.d/next/C_API/2026-11-16-17-25-12.gh-issue-158401.ADBBRG.rst b/Misc/NEWS.d/next/C_API/2026-11-16-17-25-12.gh-issue-158401.ADBBRG.rst new file mode 100644 index 00000000000000..f1b59a75095b2a --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-11-16-17-25-12.gh-issue-158401.ADBBRG.rst @@ -0,0 +1,4 @@ +:c:func:`Py_INCREF`, :c:func:`Py_XINCREF`, :c:func:`Py_DECREF` and +:c:func:`Py_XDECREF` are now implemented as function calls on all limited C +API versions. Previously, they were only implemented as function calls on +the limited C API version 3.12 or newer. Patch by Victor Stinner: From 91909216f5cfb3058552efff53b838478eb7259d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 16 Nov 2026 22:29:38 +0100 Subject: [PATCH 2/3] Apply suggestion from @hoodmane Co-authored-by: Hood Chatham --- Include/refcount.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Include/refcount.h b/Include/refcount.h index 34ea22bb97a33c..136045734ed3b6 100644 --- a/Include/refcount.h +++ b/Include/refcount.h @@ -256,7 +256,7 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op) { #if (defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))) \ || defined(_Py_OPAQUE_PYOBJECT) - // Since Python 3.16, the atable ABI implements Py_INCREF() as a function + // Since Python 3.16, the stable ABI implements Py_INCREF() as a function // call on all limited C API versions. _Py_IncRef() was added to Python // 3.10.0a7, use Py_IncRef() on older versions. Py_IncRef() accepts NULL, // whereas _Py_IncRef() doesn't. From efeb67d936685296f4724999e38272616dd3e878 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 16 Nov 2026 22:30:49 +0100 Subject: [PATCH 3/3] Update Py_INCREF #ifdef --- Include/refcount.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Include/refcount.h b/Include/refcount.h index 136045734ed3b6..f5764497d1beee 100644 --- a/Include/refcount.h +++ b/Include/refcount.h @@ -254,8 +254,7 @@ PyAPI_FUNC(void) _Py_DecRef(PyObject *); static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op) { -#if (defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))) \ - || defined(_Py_OPAQUE_PYOBJECT) +#if defined(Py_LIMITED_API) || defined(_Py_OPAQUE_PYOBJECT) // Since Python 3.16, the stable ABI implements Py_INCREF() as a function // call on all limited C API versions. _Py_IncRef() was added to Python // 3.10.0a7, use Py_IncRef() on older versions. Py_IncRef() accepts NULL,