Skip to content

Commit 13a0007

Browse files
authored
gh-108634: Py_TRACE_REFS uses a hash table (#108663)
Python built with "configure --with-trace-refs" (tracing references) is now ABI compatible with Python release build and debug build. Moreover, it now also supports the Limited API. Change Py_TRACE_REFS build: * Remove _PyObject_EXTRA_INIT macro. * The PyObject structure no longer has two extra members (_ob_prev and _ob_next). * Use a hash table (_Py_hashtable_t) to trace references (all objects): PyInterpreterState.object_state.refchain. * Py_TRACE_REFS build is now ABI compatible with release build and debug build. * Limited C API extensions can now be built with Py_TRACE_REFS: xxlimited, xxlimited_35, _testclinic_limited. * No longer rename PyModule_Create2() and PyModule_FromDefAndSpec2() functions to PyModule_Create2TraceRefs() and PyModule_FromDefAndSpec2TraceRefs(). * _Py_PrintReferenceAddresses() is now called before finalize_interp_delete() which deletes the refchain hash table. * test_tracemalloc find_trace() now also filters by size to ignore the memory allocated by _PyRefchain_Trace(). Test changes for Py_TRACE_REFS: * Add test.support.Py_TRACE_REFS constant. * Add test_sys.test_getobjects() to test sys.getobjects() function. * test_exceptions skips test_recursion_normalizing_with_no_memory() and test_memory_error_in_PyErr_PrintEx() if Python is built with Py_TRACE_REFS. * test_repl skips test_no_memory(). * test_capi skisp test_set_nomemory().
1 parent 013a99a commit 13a0007

31 files changed

+292
-243
lines changed

Doc/c-api/typeobj.rst

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -528,28 +528,6 @@ type objects) *must* have the :c:member:`~PyVarObject.ob_size` field.
528528
This field is inherited by subtypes.
529529

530530

531-
.. c:member:: PyObject* PyObject._ob_next
532-
PyObject* PyObject._ob_prev
533-
534-
These fields are only present when the macro ``Py_TRACE_REFS`` is defined
535-
(see the :option:`configure --with-trace-refs option <--with-trace-refs>`).
536-
537-
Their initialization to ``NULL`` is taken care of by the
538-
``PyObject_HEAD_INIT`` macro. For :ref:`statically allocated objects
539-
<static-types>`, these fields always remain ``NULL``. For :ref:`dynamically
540-
allocated objects <heap-types>`, these two fields are used to link the
541-
object into a doubly linked list of *all* live objects on the heap.
542-
543-
This could be used for various debugging purposes; currently the only uses
544-
are the :func:`sys.getobjects` function and to print the objects that are
545-
still alive at the end of a run when the environment variable
546-
:envvar:`PYTHONDUMPREFS` is set.
547-
548-
**Inheritance:**
549-
550-
These fields are not inherited by subtypes.
551-
552-
553531
PyVarObject Slots
554532
-----------------
555533

Doc/using/configure.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,7 @@ See also the :ref:`Python Development Mode <devmode>` and the
425425
.. versionchanged:: 3.8
426426
Release builds and debug builds are now ABI compatible: defining the
427427
``Py_DEBUG`` macro no longer implies the ``Py_TRACE_REFS`` macro (see the
428-
:option:`--with-trace-refs` option), which introduces the only ABI
429-
incompatibility.
428+
:option:`--with-trace-refs` option).
430429

431430

432431
Debug options
@@ -447,8 +446,14 @@ Debug options
447446
* Add :func:`sys.getobjects` function.
448447
* Add :envvar:`PYTHONDUMPREFS` environment variable.
449448

450-
This build is not ABI compatible with release build (default build) or debug
451-
build (``Py_DEBUG`` and ``Py_REF_DEBUG`` macros).
449+
The :envvar:`PYTHONDUMPREFS` environment variable can be used to dump
450+
objects and reference counts still alive at Python exit.
451+
452+
:ref:`Statically allocated objects <static-types>` are not traced.
453+
454+
.. versionchanged:: 3.13
455+
This build is now ABI compatible with release build and :ref:`debug build
456+
<debug-build>`.
452457

453458
.. versionadded:: 3.8
454459

Doc/whatsnew/3.13.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,11 @@ Build Changes
828828
* SQLite 3.15.2 or newer is required to build the :mod:`sqlite3` extension module.
829829
(Contributed by Erlend Aasland in :gh:`105875`.)
830830

831+
* Python built with :file:`configure` :option:`--with-trace-refs` (tracing
832+
references) is now ABI compatible with Python release build and
833+
:ref:`debug build <debug-build>`.
834+
(Contributed by Victor Stinner in :gh:`108634`.)
835+
831836

832837
C API Changes
833838
=============
@@ -900,6 +905,10 @@ New Features
900905
(with an underscore prefix).
901906
(Contributed by Victor Stinner in :gh:`108014`.)
902907

908+
* Python built with :file:`configure` :option:`--with-trace-refs` (tracing
909+
references) now supports the :ref:`Limited API <limited-c-api>`.
910+
(Contributed by Victor Stinner in :gh:`108634`.)
911+
903912
Porting to Python 3.13
904913
----------------------
905914

Include/internal/pycore_object.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
5555
backwards compatible solution */
5656
#define _PyObject_HEAD_INIT(type) \
5757
{ \
58-
_PyObject_EXTRA_INIT \
5958
.ob_refcnt = _Py_IMMORTAL_REFCNT, \
6059
.ob_type = (type) \
6160
},
@@ -184,6 +183,8 @@ _PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
184183
extern void _PyType_InitCache(PyInterpreterState *interp);
185184

186185
extern void _PyObject_InitState(PyInterpreterState *interp);
186+
extern void _PyObject_FiniState(PyInterpreterState *interp);
187+
extern bool _PyRefchain_IsTraced(PyInterpreterState *interp, PyObject *obj);
187188

188189
/* Inline functions trading binary compatibility for speed:
189190
_PyObject_Init() is the fast version of PyObject_Init(), and
@@ -302,7 +303,7 @@ extern void _PyDebug_PrintTotalRefs(void);
302303
#endif
303304

304305
#ifdef Py_TRACE_REFS
305-
extern void _Py_AddToAllObjects(PyObject *op, int force);
306+
extern void _Py_AddToAllObjects(PyObject *op);
306307
extern void _Py_PrintReferences(PyInterpreterState *, FILE *);
307308
extern void _Py_PrintReferenceAddresses(PyInterpreterState *, FILE *);
308309
#endif

Include/internal/pycore_object_state.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_hashtable.h" // _Py_hashtable_t
12+
1113
struct _py_object_runtime_state {
1214
#ifdef Py_REF_DEBUG
1315
Py_ssize_t interpreter_leaks;
@@ -20,11 +22,10 @@ struct _py_object_state {
2022
Py_ssize_t reftotal;
2123
#endif
2224
#ifdef Py_TRACE_REFS
23-
/* Head of circular doubly-linked list of all objects. These are linked
24-
* together via the _ob_prev and _ob_next members of a PyObject, which
25-
* exist only in a Py_TRACE_REFS build.
26-
*/
27-
PyObject refchain;
25+
// Hash table storing all objects. The key is the object pointer
26+
// (PyObject*) and the value is always the number 1 (as uintptr_t).
27+
// See _PyRefchain_IsTraced() and _PyRefchain_Trace() functions.
28+
_Py_hashtable_t *refchain;
2829
#endif
2930
int _not_used;
3031
};

Include/internal/pycore_runtime_init.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ extern PyTypeObject _PyExc_MemoryError;
192192
#ifdef Py_TRACE_REFS
193193
# define _py_object_state_INIT(INTERP) \
194194
{ \
195-
.refchain = {&INTERP.object_state.refchain, &INTERP.object_state.refchain}, \
195+
.refchain = NULL, \
196196
}
197197
#else
198198
# define _py_object_state_INIT(INTERP) \

Include/modsupport.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,6 @@ PyAPI_FUNC(int) PyModule_ExecDef(PyObject *module, PyModuleDef *def);
111111
#define PYTHON_ABI_VERSION 3
112112
#define PYTHON_ABI_STRING "3"
113113

114-
#ifdef Py_TRACE_REFS
115-
/* When we are tracing reference counts, rename module creation functions so
116-
modules compiled with incompatible settings will generate a
117-
link-time error. */
118-
#define PyModule_Create2 PyModule_Create2TraceRefs
119-
#define PyModule_FromDefAndSpec2 PyModule_FromDefAndSpec2TraceRefs
120-
#endif
121-
122114
PyAPI_FUNC(PyObject *) PyModule_Create2(PyModuleDef*, int apiver);
123115

124116
#ifdef Py_LIMITED_API

Include/object.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,6 @@ whose size is determined when the object is allocated.
5858
# define Py_REF_DEBUG
5959
#endif
6060

61-
#if defined(Py_LIMITED_API) && defined(Py_TRACE_REFS)
62-
# error Py_LIMITED_API is incompatible with Py_TRACE_REFS
63-
#endif
64-
65-
#ifdef Py_TRACE_REFS
66-
/* Define pointers to support a doubly-linked list of all live heap objects. */
67-
#define _PyObject_HEAD_EXTRA \
68-
PyObject *_ob_next; \
69-
PyObject *_ob_prev;
70-
71-
#define _PyObject_EXTRA_INIT _Py_NULL, _Py_NULL,
72-
73-
#else
74-
# define _PyObject_HEAD_EXTRA
75-
# define _PyObject_EXTRA_INIT
76-
#endif
77-
7861
/* PyObject_HEAD defines the initial segment of every PyObject. */
7962
#define PyObject_HEAD PyObject ob_base;
8063

@@ -130,14 +113,12 @@ check by comparing the reference count field to the immortality reference count.
130113
#ifdef Py_BUILD_CORE
131114
#define PyObject_HEAD_INIT(type) \
132115
{ \
133-
_PyObject_EXTRA_INIT \
134116
{ _Py_IMMORTAL_REFCNT }, \
135117
(type) \
136118
},
137119
#else
138120
#define PyObject_HEAD_INIT(type) \
139121
{ \
140-
_PyObject_EXTRA_INIT \
141122
{ 1 }, \
142123
(type) \
143124
},
@@ -164,8 +145,6 @@ check by comparing the reference count field to the immortality reference count.
164145
* in addition, be cast to PyVarObject*.
165146
*/
166147
struct _object {
167-
_PyObject_HEAD_EXTRA
168-
169148
#if (defined(__GNUC__) || defined(__clang__)) \
170149
&& !(defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L)
171150
// On C99 and older, anonymous union is a GCC and clang extension

Include/pyport.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -684,12 +684,6 @@ extern char * _getpty(int *, int, mode_t, int);
684684
# endif
685685
#endif
686686

687-
/* Check that ALT_SOABI is consistent with Py_TRACE_REFS:
688-
./configure --with-trace-refs should must be used to define Py_TRACE_REFS */
689-
#if defined(ALT_SOABI) && defined(Py_TRACE_REFS)
690-
# error "Py_TRACE_REFS ABI is not compatible with release and debug ABI"
691-
#endif
692-
693687
#if defined(__ANDROID__) || defined(__VXWORKS__)
694688
// Use UTF-8 as the locale encoding, ignore the LC_CTYPE locale.
695689
// See _Py_GetLocaleEncoding(), PyUnicode_DecodeLocale()

Lib/test/support/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,6 @@ def python_is_optimized():
779779

780780
_header = 'nP'
781781
_align = '0n'
782-
if hasattr(sys, "getobjects"):
783-
_header = '2P' + _header
784-
_align = '0P'
785782
_vheader = _header + 'n'
786783

787784
def calcobjsize(fmt):
@@ -2469,3 +2466,5 @@ def adjust_int_max_str_digits(max_digits):
24692466
#Windows doesn't have os.uname() but it doesn't support s390x.
24702467
skip_on_s390x = unittest.skipIf(hasattr(os, 'uname') and os.uname().machine == 's390x',
24712468
'skipped on s390x')
2469+
2470+
Py_TRACE_REFS = hasattr(sys, 'getobjects')

0 commit comments

Comments
 (0)