Skip to content

Commit 0b6d791

Browse files
Drop _PyObject_GET_WEAKREFS_LISTPTR().
1 parent 3368840 commit 0b6d791

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

Include/internal/pycore_object.h

-13
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,6 @@ _PyObject_GET_BASIC_WEAKREFS_LISTPTR(PyObject *op)
241241
return (PyObject **)((char *)op + offset);
242242
}
243243

244-
/* Return the *address* of the object's weaklist. */
245-
static inline PyObject **
246-
_PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
247-
{
248-
if (PyType_Check(op) &&
249-
((PyTypeObject *)op)->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
250-
static_builtin_state *state = _PyStaticType_GetState(
251-
(PyTypeObject *)op);
252-
return _PyStaticType_GET_WEAKREFS_LISTPTR(state);
253-
}
254-
return _PyObject_GET_BASIC_WEAKREFS_LISTPTR(op);
255-
}
256-
257244

258245
// Fast inlined version of PyObject_IS_GC()
259246
static inline int

Include/internal/pycore_typeobject.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ typedef struct {
4949
they will effectively never get triggered. However, there
5050
are also some diagnostic uses for the list of weakrefs,
5151
so we still keep it. */
52-
PyObject *tp_weaklist;
52+
PyWeakReference *tp_weaklist;
5353
} static_builtin_state;
5454

55-
static inline PyObject **
55+
static inline PyWeakReference **
5656
_PyStaticType_GET_WEAKREFS_LISTPTR(static_builtin_state *state)
5757
{
5858
assert(state != NULL);

Objects/weakrefobject.c

+26-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#include "Python.h"
2-
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR()
2+
#include "pycore_object.h" // _PyType_SUPPORTS_WEAKREFS
33
#include "structmember.h" // PyMemberDef
44

55

6-
#define GET_WEAKREFS_LISTPTR(o) \
7-
((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o))
6+
static inline PyWeakReference ** get_weakrefs_listptr(PyObject *op);
87

98

109
/***** weakref objects *****/
@@ -59,7 +58,7 @@ clear_weakref(PyWeakReference *self)
5958
PyObject *callback = self->wr_callback;
6059

6160
if (self->wr_object != Py_None) {
62-
PyWeakReference **list = GET_WEAKREFS_LISTPTR(self->wr_object);
61+
PyWeakReference **list = get_weakrefs_listptr(self->wr_object);
6362

6463
if (*list == self)
6564
/* If 'self' is the end of the list (and thus self->wr_next == NULL)
@@ -308,7 +307,7 @@ weakref___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
308307
}
309308
if (callback == Py_None)
310309
callback = NULL;
311-
list = GET_WEAKREFS_LISTPTR(ob);
310+
list = get_weakrefs_listptr(ob);
312311
get_basic_refs(*list, &ref, &proxy);
313312
if (callback == NULL && type == &_PyWeakref_RefType) {
314313
if (ref != NULL) {
@@ -801,7 +800,7 @@ PyWeakref_NewRef(PyObject *ob, PyObject *callback)
801800
Py_TYPE(ob)->tp_name);
802801
return NULL;
803802
}
804-
list = GET_WEAKREFS_LISTPTR(ob);
803+
list = get_weakrefs_listptr(ob);
805804
get_basic_refs(*list, &ref, &proxy);
806805
if (callback == Py_None)
807806
callback = NULL;
@@ -860,7 +859,7 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
860859
Py_TYPE(ob)->tp_name);
861860
return NULL;
862861
}
863-
list = GET_WEAKREFS_LISTPTR(ob);
862+
list = get_weakrefs_listptr(ob);
864863
get_basic_refs(*list, &ref, &proxy);
865864
if (callback == Py_None)
866865
callback = NULL;
@@ -941,16 +940,30 @@ handle_callback(PyWeakReference *ref, PyObject *callback)
941940

942941
/***** object weakrefs *****/
943942

943+
/* Return the *address* of the object's weaklist. */
944+
static inline PyWeakReference **
945+
get_weakrefs_listptr(PyObject *op)
946+
{
947+
if (PyType_Check(op) &&
948+
((PyTypeObject *)op)->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
949+
static_builtin_state *state = _PyStaticType_GetState(
950+
(PyTypeObject *)op);
951+
return _PyStaticType_GET_WEAKREFS_LISTPTR(state);
952+
}
953+
Py_ssize_t offset = Py_TYPE(op)->tp_weaklistoffset;
954+
return (PyWeakReference **)((char *)op + offset);
955+
}
956+
944957
PyObject **
945958
PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
946959
{
947-
return _PyObject_GET_WEAKREFS_LISTPTR(op);
960+
return (PyObject **)get_weakrefs_listptr(op);
948961
}
949962

950963
PyWeakReference *
951964
_PyObject_GetWeakRefsHead(PyObject *op)
952965
{
953-
return (PyWeakReference *)(*_PyObject_GET_WEAKREFS_LISTPTR(op));
966+
return (PyWeakReference *)(*get_weakrefs_listptr(op));
954967
}
955968

956969
Py_ssize_t
@@ -959,7 +972,7 @@ _PyObject_GetWeakRefCount(PyObject *op)
959972
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(op))) {
960973
return 0;
961974
}
962-
PyWeakReference **wrlist = GET_WEAKREFS_LISTPTR(op);
975+
PyWeakReference **wrlist = get_weakrefs_listptr(op);
963976
return _PyWeakref_GetWeakrefCount(*wrlist);
964977
}
965978

@@ -995,7 +1008,7 @@ PyObject_ClearWeakRefs(PyObject *object)
9951008
PyErr_BadInternalCall();
9961009
return;
9971010
}
998-
list = GET_WEAKREFS_LISTPTR(object);
1011+
list = get_weakrefs_listptr(object);
9991012
/* Remove the callback-less basic and proxy references */
10001013
if (*list != NULL && (*list)->wr_callback == NULL) {
10011014
clear_weakref(*list);
@@ -1071,11 +1084,11 @@ void
10711084
_PyStaticType_ClearWeakRefs(PyTypeObject *type)
10721085
{
10731086
static_builtin_state *state = _PyStaticType_GetState(type);
1074-
PyObject **list = _PyStaticType_GET_WEAKREFS_LISTPTR(state);
1087+
PyWeakReference **list = _PyStaticType_GET_WEAKREFS_LISTPTR(state);
10751088
while (*list != NULL) {
10761089
/* Note that clear_weakref() pops the first ref off the type's
10771090
weaklist before clearing its wr_object and wr_callback.
10781091
That is how we're able to loop over the list. */
1079-
clear_weakref((PyWeakReference *)*list);
1092+
clear_weakref(*list);
10801093
}
10811094
}

0 commit comments

Comments
 (0)