Skip to content

Commit 1ea6672

Browse files
authored
gh-111178: Fix function signatures in weakrefobject.c (#124903)
1 parent 8d7d257 commit 1ea6672

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

Include/cpython/weakrefobject.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ struct _PyWeakReference {
4242

4343
PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
4444

45+
#define _PyWeakref_CAST(op) \
46+
(assert(PyWeakref_Check(op)), _Py_CAST(PyWeakReference*, (op)))
47+
4548
Py_DEPRECATED(3.13) static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj)
4649
{
47-
PyWeakReference *ref;
48-
PyObject *obj;
49-
assert(PyWeakref_Check(ref_obj));
50-
ref = _Py_CAST(PyWeakReference*, ref_obj);
51-
obj = ref->wr_object;
50+
PyWeakReference *ref = _PyWeakref_CAST(ref_obj);
51+
PyObject *obj = ref->wr_object;
5252
// Explanation for the Py_REFCNT() check: when a weakref's target is part
5353
// of a long chain of deallocations which triggers the trashcan mechanism,
5454
// clearing the weakrefs can be delayed long after the target's refcount

Objects/weakrefobject.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,11 @@ clear_weakref_lock_held(PyWeakReference *self, PyObject **callback)
104104

105105
// Clear the weakref and its callback
106106
static void
107-
clear_weakref(PyWeakReference *self)
107+
clear_weakref(PyObject *op)
108108
{
109+
PyWeakReference *self = _PyWeakref_CAST(op);
109110
PyObject *callback = NULL;
111+
110112
// self->wr_object may be Py_None if the GC cleared the weakref, so lock
111113
// using the pointer in the weakref.
112114
LOCK_WEAKREFS_FOR_WR(self);
@@ -139,22 +141,24 @@ static void
139141
weakref_dealloc(PyObject *self)
140142
{
141143
PyObject_GC_UnTrack(self);
142-
clear_weakref((PyWeakReference *) self);
144+
clear_weakref(self);
143145
Py_TYPE(self)->tp_free(self);
144146
}
145147

146148

147149
static int
148-
gc_traverse(PyWeakReference *self, visitproc visit, void *arg)
150+
gc_traverse(PyObject *op, visitproc visit, void *arg)
149151
{
152+
PyWeakReference *self = _PyWeakref_CAST(op);
150153
Py_VISIT(self->wr_callback);
151154
return 0;
152155
}
153156

154157

155158
static int
156-
gc_clear(PyWeakReference *self)
159+
gc_clear(PyObject *op)
157160
{
161+
PyWeakReference *self = _PyWeakref_CAST(op);
158162
PyObject *callback;
159163
// The world is stopped during GC in free-threaded builds. It's safe to
160164
// call this without holding the lock.
@@ -198,8 +202,9 @@ weakref_hash_lock_held(PyWeakReference *self)
198202
}
199203

200204
static Py_hash_t
201-
weakref_hash(PyWeakReference *self)
205+
weakref_hash(PyObject *op)
202206
{
207+
PyWeakReference *self = _PyWeakref_CAST(op);
203208
Py_hash_t hash;
204209
Py_BEGIN_CRITICAL_SECTION(self);
205210
hash = weakref_hash_lock_held(self);
@@ -499,11 +504,11 @@ _PyWeakref_RefType = {
499504
.tp_vectorcall_offset = offsetof(PyWeakReference, vectorcall),
500505
.tp_call = PyVectorcall_Call,
501506
.tp_repr = weakref_repr,
502-
.tp_hash = (hashfunc)weakref_hash,
507+
.tp_hash = weakref_hash,
503508
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
504509
Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_BASETYPE,
505-
.tp_traverse = (traverseproc)gc_traverse,
506-
.tp_clear = (inquiry)gc_clear,
510+
.tp_traverse = gc_traverse,
511+
.tp_clear = gc_clear,
507512
.tp_richcompare = weakref_richcompare,
508513
.tp_methods = weakref_methods,
509514
.tp_members = weakref_members,
@@ -687,7 +692,7 @@ proxy_bool(PyObject *proxy)
687692
}
688693

689694
static void
690-
proxy_dealloc(PyWeakReference *self)
695+
proxy_dealloc(PyObject *self)
691696
{
692697
PyObject_GC_UnTrack(self);
693698
clear_weakref(self);
@@ -850,7 +855,7 @@ _PyWeakref_ProxyType = {
850855
sizeof(PyWeakReference),
851856
0,
852857
/* methods */
853-
(destructor)proxy_dealloc, /* tp_dealloc */
858+
proxy_dealloc, /* tp_dealloc */
854859
0, /* tp_vectorcall_offset */
855860
0, /* tp_getattr */
856861
0, /* tp_setattr */
@@ -868,8 +873,8 @@ _PyWeakref_ProxyType = {
868873
0, /* tp_as_buffer */
869874
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
870875
0, /* tp_doc */
871-
(traverseproc)gc_traverse, /* tp_traverse */
872-
(inquiry)gc_clear, /* tp_clear */
876+
gc_traverse, /* tp_traverse */
877+
gc_clear, /* tp_clear */
873878
proxy_richcompare, /* tp_richcompare */
874879
0, /* tp_weaklistoffset */
875880
proxy_iter, /* tp_iter */
@@ -885,7 +890,7 @@ _PyWeakref_CallableProxyType = {
885890
sizeof(PyWeakReference),
886891
0,
887892
/* methods */
888-
(destructor)proxy_dealloc, /* tp_dealloc */
893+
proxy_dealloc, /* tp_dealloc */
889894
0, /* tp_vectorcall_offset */
890895
0, /* tp_getattr */
891896
0, /* tp_setattr */
@@ -902,8 +907,8 @@ _PyWeakref_CallableProxyType = {
902907
0, /* tp_as_buffer */
903908
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
904909
0, /* tp_doc */
905-
(traverseproc)gc_traverse, /* tp_traverse */
906-
(inquiry)gc_clear, /* tp_clear */
910+
gc_traverse, /* tp_traverse */
911+
gc_clear, /* tp_clear */
907912
proxy_richcompare, /* tp_richcompare */
908913
0, /* tp_weaklistoffset */
909914
proxy_iter, /* tp_iter */

0 commit comments

Comments
 (0)