Skip to content

Commit 9969570

Browse files
committed
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.
1 parent f4c13c8 commit 9969570

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

‎Include/refcount.h‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,11 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
256256
{
257257
#if (defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))) \
258258
|| 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
259+
// Since Python 3.16, the atable ABI implements Py_INCREF() as a function
260+
// call on all limited C API versions. _Py_IncRef() was added to Python
261+
// 3.10.0a7, use Py_IncRef() on older versions. Py_IncRef() accepts NULL,
262+
// whereas _Py_IncRef() doesn't.
263+
# if Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10)
264264
_Py_IncRef(op);
265265
# else
266266
Py_IncRef(op);
@@ -324,14 +324,13 @@ PyAPI_FUNC(void) _Py_MergeZeroLocalRefcount(PyObject *);
324324
#endif // Py_GIL_DISABLED
325325
#endif // Py_LIMITED_API
326326

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.
327+
#if defined(Py_LIMITED_API) || defined(_Py_OPAQUE_PYOBJECT)
328+
// Since Python 3.16, the stable ABI implements Py_DECREF() as a function call
329+
// on all limited C API version. _Py_DecRef() was added to Python 3.10.0a7,
330+
// use Py_DecRef() on older versions. Py_DecRef() accepts NULL, whereas
331+
// _Py_DecRef() doesn't.
333332
static inline void Py_DECREF(PyObject *op) {
334-
# if Py_LIMITED_API+0 >= 0x030a00A7
333+
# if Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10)
335334
_Py_DecRef(op);
336335
# else
337336
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)