Skip to content

Commit 92022d8

Browse files
authored
gh-102304: Fix Py_INCREF() stable ABI in debug mode (#104763)
When Python is built in debug mode (if the Py_REF_DEBUG macro is defined), the Py_INCREF() and Py_DECREF() function are now always implemented as opaque functions to avoid leaking implementation details like the "_Py_RefTotal" variable or the _Py_DecRefTotal_DO_NOT_USE_THIS() function. * Remove _Py_IncRefTotal_DO_NOT_USE_THIS() and _Py_DecRefTotal_DO_NOT_USE_THIS() from the stable ABI. * Remove _Py_NegativeRefcount() from limited C API.
1 parent bae415a commit 92022d8

File tree

5 files changed

+17
-27
lines changed

5 files changed

+17
-27
lines changed

Doc/whatsnew/3.13.rst

+9
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,15 @@ Build Changes
343343
:file:`!configure`.
344344
(Contributed by Christian Heimes in :gh:`89886`.)
345345

346+
* C extensions built with the :ref:`limited C API <limited-c-api>`
347+
on :ref:`Python build in debug mode <debug-build>` no longer support Python
348+
3.9 and older. In this configuration, :c:func:`Py_INCREF` and
349+
:c:func:`Py_DECREF` are now always implemented as opaque function calls,
350+
but the called functions were added to Python 3.10. Build C extensions
351+
with a release build of Python or with Python 3.12 and older, to keep support
352+
for Python 3.9 and older.
353+
(Contributed by Victor Stinner in :gh:`102304`.)
354+
346355

347356
C API Changes
348357
=============

Include/object.h

+8-14
Original file line numberDiff line numberDiff line change
@@ -585,20 +585,14 @@ decision that's up to the implementer of each new type so if you want,
585585
you can count such references to the type object.)
586586
*/
587587

588-
#ifdef Py_REF_DEBUG
589-
# if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030A0000
590-
extern Py_ssize_t _Py_RefTotal;
591-
# define _Py_INC_REFTOTAL() _Py_RefTotal++
592-
# define _Py_DEC_REFTOTAL() _Py_RefTotal--
593-
# elif !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
588+
#if defined(Py_REF_DEBUG) && !defined(Py_LIMITED_API)
589+
PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
590+
PyObject *op);
594591
PyAPI_FUNC(void) _Py_IncRefTotal_DO_NOT_USE_THIS(void);
595592
PyAPI_FUNC(void) _Py_DecRefTotal_DO_NOT_USE_THIS(void);
596593
# define _Py_INC_REFTOTAL() _Py_IncRefTotal_DO_NOT_USE_THIS()
597594
# define _Py_DEC_REFTOTAL() _Py_DecRefTotal_DO_NOT_USE_THIS()
598-
# endif
599-
PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
600-
PyObject *op);
601-
#endif /* Py_REF_DEBUG */
595+
#endif // Py_REF_DEBUG && !Py_LIMITED_API
602596

603597
PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
604598

@@ -616,8 +610,8 @@ PyAPI_FUNC(void) _Py_DecRef(PyObject *);
616610

617611
static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
618612
{
619-
#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
620-
// Stable ABI for Python 3.10 built in debug mode.
613+
#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API)
614+
// Stable ABI for Python built in debug mode
621615
_Py_IncRef(op);
622616
#else
623617
// Non-limited C API and limited C API for Python 3.9 and older access
@@ -647,8 +641,8 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
647641
# define Py_INCREF(op) Py_INCREF(_PyObject_CAST(op))
648642
#endif
649643

650-
#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
651-
// Stable ABI for limited C API version 3.10 of Python debug build
644+
#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API)
645+
// Stable ABI for Python built in debug mode
652646
static inline void Py_DECREF(PyObject *op) {
653647
_Py_DecRef(op);
654648
}

Lib/test/test_stable_abi_ctypes.py

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Misc/stable_abi.toml

-9
Original file line numberDiff line numberDiff line change
@@ -2428,12 +2428,3 @@
24282428
added = '3.12'
24292429
[const.Py_TPFLAGS_ITEMS_AT_END]
24302430
added = '3.12'
2431-
2432-
[function._Py_IncRefTotal_DO_NOT_USE_THIS]
2433-
added = '3.12'
2434-
ifdef = 'Py_REF_DEBUG'
2435-
abi_only = true
2436-
[function._Py_DecRefTotal_DO_NOT_USE_THIS]
2437-
added = '3.12'
2438-
ifdef = 'Py_REF_DEBUG'
2439-
abi_only = true

PC/python3dll.c

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)