Skip to content

Commit 1356bc5

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.14: mention the change in "Notable changes in 3.14.11" of What's New in Python 3.14. Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent 3eb2787 commit 1356bc5

3 files changed

Lines changed: 29 additions & 12 deletions

File tree

‎Doc/whatsnew/3.14.rst‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3526,3 +3526,16 @@ ssl
35263526
that applies even when an application supplies its own root CA bundle, and
35273527
regardless of whether the platform trust store has removed trust.
35283528
(Contributed by Christian Heimes in :gh:`160040` for :cve:`2026-115557`.)
3529+
3530+
3531+
Notable changes in 3.14.11
3532+
==========================
3533+
3534+
C API changes
3535+
-------------
3536+
3537+
* :c:func:`Py_INCREF`, :c:func:`Py_XINCREF`, :c:func:`Py_DECREF` and
3538+
:c:func:`Py_XDECREF` are now implemented as function calls on all limited C
3539+
API versions. Previously, they were only implemented as function calls on
3540+
the limited C API version 3.12 or newer. Patch by Victor Stinner:
3541+
(Contributed by Victor Stinner in :gh:`158401`.)

‎Include/refcount.h‎

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

252252
static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
253253
{
254-
#if defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))
255-
// Stable ABI implements Py_INCREF() as a function call on limited C API
256-
// version 3.12 and newer, and on Python built in debug mode. _Py_IncRef()
257-
// was added to Python 3.10.0a7, use Py_IncRef() on older Python versions.
258-
// Py_IncRef() accepts NULL whereas _Py_IncRef() doesn't.
259-
# if Py_LIMITED_API+0 >= 0x030a00A7
254+
#if defined(Py_LIMITED_API)
255+
// gh-158401: The stable ABI now implements Py_INCREF() as a function
256+
// call on all limited C API versions. _Py_IncRef() was added to Python
257+
// 3.10.0a7, use Py_IncRef() on older versions. Py_IncRef() accepts NULL,
258+
// whereas _Py_IncRef() doesn't.
259+
# if Py_LIMITED_API+0 >= 0x030a0000
260260
_Py_IncRef(op);
261261
# else
262262
Py_IncRef(op);
@@ -319,13 +319,13 @@ PyAPI_FUNC(void) _Py_DecRefSharedDebug(PyObject *, const char *, int);
319319
PyAPI_FUNC(void) _Py_MergeZeroLocalRefcount(PyObject *);
320320
#endif
321321

322-
#if defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))
323-
// Stable ABI implements Py_DECREF() as a function call on limited C API
324-
// version 3.12 and newer, and on Python built in debug mode. _Py_DecRef() was
325-
// added to Python 3.10.0a7, use Py_DecRef() on older Python versions.
326-
// Py_DecRef() accepts NULL whereas _Py_DecRef() doesn't.
322+
#if defined(Py_LIMITED_API)
323+
// gh-158401: The stable ABI now implements Py_DECREF() as a function call
324+
// on all limited C API versions. _Py_DecRef() was added to Python 3.10.0a7,
325+
// use Py_DecRef() on older versions. Py_DecRef() accepts NULL, whereas
326+
// _Py_DecRef() doesn't.
327327
static inline void Py_DECREF(PyObject *op) {
328-
# if Py_LIMITED_API+0 >= 0x030a00A7
328+
# if Py_LIMITED_API+0 >= 0x030a0000
329329
_Py_DecRef(op);
330330
# else
331331
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)