Skip to content

Commit d1e3cae

Browse files
vstinnerhoodmaneencukou
authored
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. Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent a3ea5d2 commit d1e3cae

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

‎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)