From d52b1e99480194618c7970ddff17442a8a25f137 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 5 Dec 2023 10:37:57 +0100 Subject: [PATCH] gh-111506: Implement Py_REFCNT() as opaque function call In the limited C API version 3.13, the Py_REFCNT() function is now implemented as an opaque function call. --- Include/object.h | 8 +++++++- .../C API/2023-12-05-10-39-06.gh-issue-111506.gz90Ft.rst | 2 ++ Objects/object.c | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/C API/2023-12-05-10-39-06.gh-issue-111506.gz90Ft.rst diff --git a/Include/object.h b/Include/object.h index dfeb43bda7d8411..dd73da54ea73de5 100644 --- a/Include/object.h +++ b/Include/object.h @@ -290,8 +290,14 @@ _Py_IsOwnedByCurrentThread(PyObject *ob) } #endif +PyAPI_FUNC(Py_ssize_t) _Py_GetRefcnt(PyObject *ob); + static inline Py_ssize_t Py_REFCNT(PyObject *ob) { -#if !defined(Py_GIL_DISABLED) +#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030d0000 + // Stable ABI implements Py_SET_REFCNT() as a function call + // on limited C API version 3.13 and newer. + return _Py_GetRefcnt(ob); +#elif !defined(Py_GIL_DISABLED) return ob->ob_refcnt; #else uint32_t local = _Py_atomic_load_uint32_relaxed(&ob->ob_ref_local); diff --git a/Misc/NEWS.d/next/C API/2023-12-05-10-39-06.gh-issue-111506.gz90Ft.rst b/Misc/NEWS.d/next/C API/2023-12-05-10-39-06.gh-issue-111506.gz90Ft.rst new file mode 100644 index 000000000000000..9a9706c2b339f15 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2023-12-05-10-39-06.gh-issue-111506.gz90Ft.rst @@ -0,0 +1,2 @@ +In the limited C API version 3.13, the :c:func:`Py_REFCNT` function is now +implemented as an opaque function call. Patch by Victor Stinner. diff --git a/Objects/object.c b/Objects/object.c index d145674cb3ba34d..2d9eb92f092ac34 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -2932,6 +2932,14 @@ int Py_IsFalse(PyObject *x) } +// Py_REFCNT() implementation for stable ABI +Py_ssize_t +_Py_GetRefcnt(PyObject *ob) +{ + return Py_REFCNT(ob); +} + + // Py_SET_REFCNT() implementation for stable ABI void _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt)