[3.12] gh-102304: Fix Py_INCREF() for limited C API 3.9 (#105553)

* gh-102304: Fix Py_INCREF() for limited C API 3.9 (#105550)

When Python is built in debug mode (Py_REF_DEBUG macro), Py_INCREF()
and Py_DECREF() of the limited C API 3.9 (and older) now call
Py_IncRef() and Py_DecRef(), since _Py_IncRef() and _Py_DecRef() were
added to Python 3.10.

(cherry picked from commit 7ba0fd9f87)

* gh-102304: Remove Py_INCREF() doc change (#105552)

Py_INCREF() was made compatible again with Python 3.9 and older in
the limited API of Python debug mode.

(cherry picked from commit 58e4b69f69)
This commit is contained in:
Victor Stinner 2023-06-09 12:32:46 +02:00 committed by GitHub
parent 2f4a2d6c1b
commit d29e86bea3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 11 deletions

View file

@ -1536,15 +1536,6 @@ Build Changes
:file:`!configure`. :file:`!configure`.
(Contributed by Christian Heimes in :gh:`89886`.) (Contributed by Christian Heimes in :gh:`89886`.)
* C extensions built with the :ref:`limited C API <limited-c-api>`
on :ref:`Python build in debug mode <debug-build>` no longer support Python
3.9 and older. In this configuration, :c:func:`Py_INCREF` and
:c:func:`Py_DECREF` are now always implemented as opaque function calls,
but the called functions were added to Python 3.10. Build C extensions
with a release build of Python or with Python 3.12 and older, to keep support
for Python 3.9 and older.
(Contributed by Victor Stinner in :gh:`102304`.)
C API Changes C API Changes
============= =============

View file

@ -611,8 +611,14 @@ PyAPI_FUNC(void) _Py_DecRef(PyObject *);
static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op) static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
{ {
#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) #if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API)
// Stable ABI for Python built in debug mode // Stable ABI for Python built in debug mode. _Py_IncRef() was added to
// Python 3.10.0a7, use Py_IncRef() on older Python versions. Py_IncRef()
// accepts NULL whereas _Py_IncRef() doesn't.
# if Py_LIMITED_API+0 >= 0x030a00A7
_Py_IncRef(op); _Py_IncRef(op);
# else
Py_IncRef(op);
# endif
#else #else
// Non-limited C API and limited C API for Python 3.9 and older access // Non-limited C API and limited C API for Python 3.9 and older access
// directly PyObject.ob_refcnt. // directly PyObject.ob_refcnt.
@ -642,9 +648,15 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
#endif #endif
#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) #if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API)
// Stable ABI for Python built in debug mode // Stable ABI for Python built in debug mode. _Py_DecRef() was added to Python
// 3.10.0a7, use Py_DecRef() on older Python versions. Py_DecRef() accepts NULL
// whereas _Py_IncRef() doesn't.
static inline void Py_DECREF(PyObject *op) { static inline void Py_DECREF(PyObject *op) {
# if Py_LIMITED_API+0 >= 0x030a00A7
_Py_DecRef(op); _Py_DecRef(op);
# else
Py_DecRef(op);
# endif
} }
#define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op)) #define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))