Skip to content

Commit 6b09207

Browse files
vstinnerhoodmaneencukou
committed
gh-158401: Implement Py_INCREF() as function call in stable ABI (#162120)
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. Backport to 3.15: mention the change in "Notable changes in 3.15.3" of What's New in Python 3.15. Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com> Co-authored-by: Petr Viktorin <encukou@gmail.com> (cherry picked from commit d1e3cae)
1 parent dea81c5 commit 6b09207

3 files changed

Lines changed: 29 additions & 14 deletions

File tree

‎Doc/whatsnew/3.15.rst‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2952,3 +2952,16 @@ ssl
29522952
that applies even when an application supplies its own root CA bundle, and
29532953
regardless of whether the platform trust store has removed trust.
29542954
(Contributed by Christian Heimes in :gh:`160040` for :cve:`2026-115557`.)
2955+
2956+
2957+
Notable changes in 3.15.3
2958+
=========================
2959+
2960+
C API changes
2961+
-------------
2962+
2963+
* :c:func:`Py_INCREF`, :c:func:`Py_XINCREF`, :c:func:`Py_DECREF` and
2964+
:c:func:`Py_XDECREF` are now implemented as function calls on all limited C
2965+
API versions. Previously, they were only implemented as function calls on
2966+
the limited C API version 3.12 or newer. Patch by Victor Stinner:
2967+
(Contributed by Victor Stinner in :gh:`158401`.)

‎Include/refcount.h‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,12 @@ PyAPI_FUNC(void) _Py_DecRef(PyObject *);
254254

255255
static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
256256
{
257-
#if (defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))) \
258-
|| defined(_Py_OPAQUE_PYOBJECT)
259-
// Stable ABI implements Py_INCREF() as a function call on limited C API
260-
// version 3.12 and newer, abi3t, and on Python built in debug mode.
261-
// _Py_IncRef() was added to Python 3.10.0a7, use Py_IncRef() on older versions.
262-
// Py_IncRef() accepts NULL whereas _Py_IncRef() doesn't.
263-
# if Py_LIMITED_API+0 >= 0x030a00A7
257+
#if defined(Py_LIMITED_API) || defined(_Py_OPAQUE_PYOBJECT)
258+
// gh-158401: The stable ABI now implements Py_INCREF() as a function
259+
// call on all limited C API versions. _Py_IncRef() was added to Python
260+
// 3.10.0a7, use Py_IncRef() on older versions. Py_IncRef() accepts NULL,
261+
// whereas _Py_IncRef() doesn't.
262+
# if Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10)
264263
_Py_IncRef(op);
265264
# else
266265
Py_IncRef(op);
@@ -324,14 +323,13 @@ PyAPI_FUNC(void) _Py_MergeZeroLocalRefcount(PyObject *);
324323
#endif // Py_GIL_DISABLED
325324
#endif // Py_LIMITED_API
326325

327-
#if (defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))) \
328-
|| defined(_Py_OPAQUE_PYOBJECT)
329-
// Stable ABI implements Py_DECREF() as a function call on limited C API
330-
// version 3.12 and newer, abi3t, and on Python built in debug mode.
331-
// _Py_DecRef() was added to Python 3.10.0a7, use Py_DecRef() on older versions.
332-
// Py_DecRef() accepts NULL whereas _Py_DecRef() doesn't.
326+
#if defined(Py_LIMITED_API) || defined(_Py_OPAQUE_PYOBJECT)
327+
// gh-158401: The stable ABI now implements Py_DECREF() as a function call
328+
// on all limited C API versions. _Py_DecRef() was added to Python 3.10.0a7,
329+
// use Py_DecRef() on older versions. Py_DecRef() accepts NULL, whereas
330+
// _Py_DecRef() doesn't.
333331
static inline void Py_DECREF(PyObject *op) {
334-
# if Py_LIMITED_API+0 >= 0x030a00A7
332+
# if Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10)
335333
_Py_DecRef(op);
336334
# else
337335
Py_DecRef(op);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:c:func:`Py_INCREF`, :c:func:`Py_XINCREF`, :c:func:`Py_DECREF` and
2+
:c:func:`Py_XDECREF` are now implemented as function calls on all limited C
3+
API versions. Previously, they were only implemented as function calls on
4+
the limited C API version 3.12 or newer. Patch by Victor Stinner:

0 commit comments

Comments
 (0)