Skip to content

Commit 38aefc5

Browse files
authored
bpo-40170: PyObject_GET_WEAKREFS_LISTPTR() becomes a function (GH-19377)
Convert the PyObject_GET_WEAKREFS_LISTPTR() macro to a function to hide implementation details: the macro accessed directly to the PyTypeObject.tp_weaklistoffset member. Add _PyObject_GET_WEAKREFS_LISTPTR() static inline function to the internal C API.
1 parent 08050e9 commit 38aefc5

File tree

8 files changed

+25
-6
lines changed

8 files changed

+25
-6
lines changed

Include/cpython/objimpl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
138138
/* Test if a type supports weak references */
139139
#define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
140140

141-
#define PyObject_GET_WEAKREFS_LISTPTR(o) \
142-
((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset))
141+
PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op);
143142

144143
#ifdef __cplusplus
145144
}

Include/internal/pycore_object.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ extern void _Py_PrintReferences(FILE *);
8787
extern void _Py_PrintReferenceAddresses(FILE *);
8888
#endif
8989

90+
static inline PyObject **
91+
_PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
92+
{
93+
Py_ssize_t offset = Py_TYPE(op)->tp_weaklistoffset;
94+
return (PyObject **)((char *)op + offset);
95+
}
96+
9097
#ifdef __cplusplus
9198
}
9299
#endif
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Convert the :c:func:`PyObject_GET_WEAKREFS_LISTPTR` macro to a function to hide
2+
implementation details: the macro accessed directly to the
3+
:c:member:`PyTypeObject.tp_weaklistoffset` member.

Modules/_weakref.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#include "Python.h"
2+
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
23

34

45
#define GET_WEAKREFS_LISTPTR(o) \
5-
((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
6+
((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o))
67

78
/*[clinic input]
89
module _weakref

Modules/gcmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
788788

789789
/* It supports weakrefs. Does it have any? */
790790
wrlist = (PyWeakReference **)
791-
PyObject_GET_WEAKREFS_LISTPTR(op);
791+
_PyObject_GET_WEAKREFS_LISTPTR(op);
792792

793793
/* `op` may have some weakrefs. March over the list, clear
794794
* all the weakrefs, and move the weakrefs with callbacks

Objects/object.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,14 @@ _Py_Dealloc(PyObject *op)
22062206
(*dealloc)(op);
22072207
}
22082208

2209+
2210+
PyObject **
2211+
PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
2212+
{
2213+
return _PyObject_GET_WEAKREFS_LISTPTR(op);
2214+
}
2215+
2216+
22092217
#ifdef __cplusplus
22102218
}
22112219
#endif

Objects/typeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ subtype_dealloc(PyObject *self)
12711271
if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
12721272
/* Modeled after GET_WEAKREFS_LISTPTR() */
12731273
PyWeakReference **list = (PyWeakReference **) \
1274-
PyObject_GET_WEAKREFS_LISTPTR(self);
1274+
_PyObject_GET_WEAKREFS_LISTPTR(self);
12751275
while (*list)
12761276
_PyWeakref_ClearRef(*list);
12771277
}

Objects/weakrefobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "Python.h"
2+
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
23
#include "structmember.h"
34

45

56
#define GET_WEAKREFS_LISTPTR(o) \
6-
((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
7+
((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o))
78

89

910
Py_ssize_t

0 commit comments

Comments
 (0)