Skip to content

Commit fee188d

Browse files
committed
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. (cherry picked from commit 92022d8)
1 parent 82ab13c commit fee188d

File tree

5 files changed

+17
-27
lines changed

5 files changed

+17
-27
lines changed

Doc/whatsnew/3.12.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,15 @@ Build Changes
15351535
:file:`!configure`.
15361536
(Contributed by Christian Heimes in :gh:`89886`.)
15371537

1538+
* C extensions built with the :ref:`limited C API <limited-c-api>`
1539+
on :ref:`Python build in debug mode <debug-build>` no longer support Python
1540+
3.9 and older. In this configuration, :c:func:`Py_INCREF` and
1541+
:c:func:`Py_DECREF` are now always implemented as opaque function calls,
1542+
but the called functions were added to Python 3.10. Build C extensions
1543+
with a release build of Python or with Python 3.12 and older, to keep support
1544+
for Python 3.9 and older.
1545+
(Contributed by Victor Stinner in :gh:`102304`.)
1546+
15381547

15391548
C API Changes
15401549
=============

Include/object.h

Lines changed: 8 additions & 14 deletions
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

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Misc/stable_abi.toml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,12 +2406,3 @@
24062406
added = '3.12'
24072407
[const.Py_TPFLAGS_ITEMS_AT_END]
24082408
added = '3.12'
2409-
2410-
[function._Py_IncRefTotal_DO_NOT_USE_THIS]
2411-
added = '3.12'
2412-
ifdef = 'Py_REF_DEBUG'
2413-
abi_only = true
2414-
[function._Py_DecRefTotal_DO_NOT_USE_THIS]
2415-
added = '3.12'
2416-
ifdef = 'Py_REF_DEBUG'
2417-
abi_only = true

PC/python3dll.c

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)