Skip to content

Commit d1cd7db

Browse files
committed
gh-102304: Fix Py_INCREF() stable ABI in debug mode
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. * 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. * Fix script name in Lib/test/test_stable_abi_ctypes.py
1 parent 58a2e09 commit d1cd7db

File tree

7 files changed

+17
-33
lines changed

7 files changed

+17
-33
lines changed

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

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

Makefile.pre.in

+4
Original file line numberDiff line numberDiff line change
@@ -2729,6 +2729,10 @@ patchcheck: all
27292729

27302730
.PHONY: check-limited-abi
27312731
check-limited-abi: all
2732+
# Regenerate 3 files using using Tools/build/stable_abi.py:
2733+
# - Lib/test/test_stable_abi_ctypes.py
2734+
# - Modules/_testcapi_feature_macros.inc
2735+
# - PC/python3dll.c
27322736
$(RUNSHARED) ./$(BUILDPYTHON) $(srcdir)/Tools/build/stable_abi.py --all $(srcdir)/Misc/stable_abi.toml
27332737

27342738
.PHONY: update-config

Misc/stable_abi.toml

-9
Original file line numberDiff line numberDiff line change
@@ -2409,12 +2409,3 @@
24092409
added = '3.12'
24102410
[const.Py_TPFLAGS_ITEMS_AT_END]
24112411
added = '3.12'
2412-
2413-
[function._Py_IncRefTotal_DO_NOT_USE_THIS]
2414-
added = '3.12'
2415-
ifdef = 'Py_REF_DEBUG'
2416-
abi_only = true
2417-
[function._Py_DecRefTotal_DO_NOT_USE_THIS]
2418-
added = '3.12'
2419-
ifdef = 'Py_REF_DEBUG'
2420-
abi_only = true

Modules/_testcapi_feature_macros.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by Tools/scripts/stable_abi.py
1+
// Generated by Tools/build/stable_abi.py
22

33
// Add an entry in dict `result` for each Stable ABI feature macro.
44

PC/python3dll.c

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

Tools/build/stable_abi.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ def gen_doc_annotations(manifest, args, outfile):
267267
def gen_ctypes_test(manifest, args, outfile):
268268
"""Generate/check the ctypes-based test for exported symbols"""
269269
write = partial(print, file=outfile)
270-
write(textwrap.dedent('''
271-
# Generated by Tools/scripts/stable_abi.py
270+
write(textwrap.dedent(f'''\
271+
# Generated by {SCRIPT_NAME}
272272
273273
"""Test that all symbols of the Stable ABI are accessible using ctypes
274274
"""
@@ -341,7 +341,7 @@ def test_windows_feature_macros(self):
341341
def gen_testcapi_feature_macros(manifest, args, outfile):
342342
"""Generate/check the stable ABI list for documentation annotations"""
343343
write = partial(print, file=outfile)
344-
write('// Generated by Tools/scripts/stable_abi.py')
344+
write(f'// Generated by {SCRIPT_NAME}')
345345
write()
346346
write('// Add an entry in dict `result` for each Stable ABI feature macro.')
347347
write()

0 commit comments

Comments
 (0)