Skip to content

Commit

Permalink
gh-102304: Fix Py_INCREF() stable ABI in debug mode
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vstinner committed May 31, 2023
1 parent 58a2e09 commit d1cd7db
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 33 deletions.
22 changes: 8 additions & 14 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,20 +585,14 @@ decision that's up to the implementer of each new type so if you want,
you can count such references to the type object.)
*/

#ifdef Py_REF_DEBUG
# if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030A0000
extern Py_ssize_t _Py_RefTotal;
# define _Py_INC_REFTOTAL() _Py_RefTotal++
# define _Py_DEC_REFTOTAL() _Py_RefTotal--
# elif !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
#if defined(Py_REF_DEBUG) && !defined(Py_LIMITED_API)
PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
PyObject *op);
PyAPI_FUNC(void) _Py_IncRefTotal_DO_NOT_USE_THIS(void);
PyAPI_FUNC(void) _Py_DecRefTotal_DO_NOT_USE_THIS(void);
# define _Py_INC_REFTOTAL() _Py_IncRefTotal_DO_NOT_USE_THIS()
# define _Py_DEC_REFTOTAL() _Py_DecRefTotal_DO_NOT_USE_THIS()
# endif
PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
PyObject *op);
#endif /* Py_REF_DEBUG */
#endif // Py_REF_DEBUG && !Py_LIMITED_API

PyAPI_FUNC(void) _Py_Dealloc(PyObject *);

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

static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
{
#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
// Stable ABI for Python 3.10 built in debug mode.
#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API)
// Stable ABI for Python built in debug mode
_Py_IncRef(op);
#else
// Non-limited C API and limited C API for Python 3.9 and older access
Expand Down Expand Up @@ -647,8 +641,8 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
# define Py_INCREF(op) Py_INCREF(_PyObject_CAST(op))
#endif

#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
// Stable ABI for limited C API version 3.10 of Python debug build
#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API)
// Stable ABI for Python built in debug mode
static inline void Py_DECREF(PyObject *op) {
_Py_DecRef(op);
}
Expand Down
5 changes: 1 addition & 4 deletions Lib/test/test_stable_abi_ctypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -2729,6 +2729,10 @@ patchcheck: all

.PHONY: check-limited-abi
check-limited-abi: all
# Regenerate 3 files using using Tools/build/stable_abi.py:
# - Lib/test/test_stable_abi_ctypes.py
# - Modules/_testcapi_feature_macros.inc
# - PC/python3dll.c
$(RUNSHARED) ./$(BUILDPYTHON) $(srcdir)/Tools/build/stable_abi.py --all $(srcdir)/Misc/stable_abi.toml

.PHONY: update-config
Expand Down
9 changes: 0 additions & 9 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2409,12 +2409,3 @@
added = '3.12'
[const.Py_TPFLAGS_ITEMS_AT_END]
added = '3.12'

[function._Py_IncRefTotal_DO_NOT_USE_THIS]
added = '3.12'
ifdef = 'Py_REF_DEBUG'
abi_only = true
[function._Py_DecRefTotal_DO_NOT_USE_THIS]
added = '3.12'
ifdef = 'Py_REF_DEBUG'
abi_only = true
2 changes: 1 addition & 1 deletion Modules/_testcapi_feature_macros.inc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated by Tools/scripts/stable_abi.py
// Generated by Tools/build/stable_abi.py

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

Expand Down
2 changes: 0 additions & 2 deletions PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Tools/build/stable_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ def gen_doc_annotations(manifest, args, outfile):
def gen_ctypes_test(manifest, args, outfile):
"""Generate/check the ctypes-based test for exported symbols"""
write = partial(print, file=outfile)
write(textwrap.dedent('''
# Generated by Tools/scripts/stable_abi.py
write(textwrap.dedent(f'''\
# Generated by {SCRIPT_NAME}
"""Test that all symbols of the Stable ABI are accessible using ctypes
"""
Expand Down Expand Up @@ -341,7 +341,7 @@ def test_windows_feature_macros(self):
def gen_testcapi_feature_macros(manifest, args, outfile):
"""Generate/check the stable ABI list for documentation annotations"""
write = partial(print, file=outfile)
write('// Generated by Tools/scripts/stable_abi.py')
write(f'// Generated by {SCRIPT_NAME}')
write()
write('// Add an entry in dict `result` for each Stable ABI feature macro.')
write()
Expand Down

0 comments on commit d1cd7db

Please sign in to comment.