Skip to content

Commit 07923f3

Browse files
authored
bpo-35059: Enhance _PyObject_GC_TRACK() macros (GH-20931)
* Rename _PyObject_GC_TRACK_impl() to _PyObject_GC_TRACK() * Rename _PyObject_GC_UNTRACK_impl() to _PyObject_GC_UNTRACK() * Omit filename and lineno parameters if NDEBUG is defined.
1 parent 2c4928d commit 07923f3

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

Include/internal/pycore_object.h

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ _PyObject_InitVar(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
5353

5454

5555
/* Tell the GC to track this object.
56+
*
57+
* The object must not be tracked by the GC.
5658
*
5759
* NB: While the object is tracked by the collector, it must be safe to call the
5860
* ob_traverse method.
@@ -61,20 +63,24 @@ _PyObject_InitVar(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
6163
* because it's not object header. So we don't use _PyGCHead_PREV() and
6264
* _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations.
6365
*
64-
* The PyObject_GC_Track() function is the public version of this macro.
66+
* See also the public PyObject_GC_Track() function.
6567
*/
66-
static inline void _PyObject_GC_TRACK_impl(const char *filename, int lineno,
67-
PyObject *op)
68+
static inline void _PyObject_GC_TRACK(
69+
// The preprocessor removes _PyObject_ASSERT_FROM() calls if NDEBUG is defined
70+
#ifndef NDEBUG
71+
const char *filename, int lineno,
72+
#endif
73+
PyObject *op)
6874
{
6975
_PyObject_ASSERT_FROM(op, !_PyObject_GC_IS_TRACKED(op),
7076
"object already tracked by the garbage collector",
71-
filename, lineno, "_PyObject_GC_TRACK");
77+
filename, lineno, __func__);
7278

7379
PyGC_Head *gc = _Py_AS_GC(op);
7480
_PyObject_ASSERT_FROM(op,
7581
(gc->_gc_prev & _PyGC_PREV_MASK_COLLECTING) == 0,
7682
"object is in generation which is garbage collected",
77-
filename, lineno, "_PyObject_GC_TRACK");
83+
filename, lineno, __func__);
7884

7985
PyThreadState *tstate = _PyThreadState_GET();
8086
PyGC_Head *generation0 = tstate->interp->gc.generation0;
@@ -85,24 +91,26 @@ static inline void _PyObject_GC_TRACK_impl(const char *filename, int lineno,
8591
generation0->_gc_prev = (uintptr_t)gc;
8692
}
8793

88-
#define _PyObject_GC_TRACK(op) \
89-
_PyObject_GC_TRACK_impl(__FILE__, __LINE__, _PyObject_CAST(op))
90-
9194
/* Tell the GC to stop tracking this object.
9295
*
9396
* Internal note: This may be called while GC. So _PyGC_PREV_MASK_COLLECTING
9497
* must be cleared. But _PyGC_PREV_MASK_FINALIZED bit is kept.
9598
*
9699
* The object must be tracked by the GC.
97100
*
98-
* The PyObject_GC_UnTrack() function is the public version of this macro.
101+
* See also the public PyObject_GC_UnTrack() which accept an object which is
102+
* not tracked.
99103
*/
100-
static inline void _PyObject_GC_UNTRACK_impl(const char *filename, int lineno,
101-
PyObject *op)
104+
static inline void _PyObject_GC_UNTRACK(
105+
// The preprocessor removes _PyObject_ASSERT_FROM() calls if NDEBUG is defined
106+
#ifndef NDEBUG
107+
const char *filename, int lineno,
108+
#endif
109+
PyObject *op)
102110
{
103111
_PyObject_ASSERT_FROM(op, _PyObject_GC_IS_TRACKED(op),
104112
"object not tracked by the garbage collector",
105-
filename, lineno, "_PyObject_GC_UNTRACK");
113+
filename, lineno, __func__);
106114

107115
PyGC_Head *gc = _Py_AS_GC(op);
108116
PyGC_Head *prev = _PyGCHead_PREV(gc);
@@ -113,8 +121,20 @@ static inline void _PyObject_GC_UNTRACK_impl(const char *filename, int lineno,
113121
gc->_gc_prev &= _PyGC_PREV_MASK_FINALIZED;
114122
}
115123

116-
#define _PyObject_GC_UNTRACK(op) \
117-
_PyObject_GC_UNTRACK_impl(__FILE__, __LINE__, _PyObject_CAST(op))
124+
// Macros to accept any type for the parameter, and to automatically pass
125+
// the filename and the filename (if NDEBUG is not defined) where the macro
126+
// is called.
127+
#ifdef NDEBUG
128+
# define _PyObject_GC_TRACK(op) \
129+
_PyObject_GC_TRACK(_PyObject_CAST(op))
130+
# define _PyObject_GC_UNTRACK(op) \
131+
_PyObject_GC_UNTRACK(_PyObject_CAST(op))
132+
#else
133+
# define _PyObject_GC_TRACK(op) \
134+
_PyObject_GC_TRACK(__FILE__, __LINE__, _PyObject_CAST(op))
135+
# define _PyObject_GC_UNTRACK(op) \
136+
_PyObject_GC_UNTRACK(__FILE__, __LINE__, _PyObject_CAST(op))
137+
#endif
118138

119139
#ifdef Py_REF_DEBUG
120140
extern void _PyDebug_PrintTotalRefs(void);

0 commit comments

Comments
 (0)