Skip to content

Commit f7d27fe

Browse files
Add _PyStaticType_ClearWeakRefs().
1 parent 8116dde commit f7d27fe

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Include/internal/pycore_typeobject.h

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ extern PyStatus _PyTypes_InitSlotDefs(void);
7070

7171
extern int _PyStaticType_InitBuiltin(PyTypeObject *type);
7272
extern static_builtin_state * _PyStaticType_GetState(PyTypeObject *);
73+
extern void _PyStaticType_ClearWeakRefs(PyTypeObject *type);
7374
extern void _PyStaticType_Dealloc(PyTypeObject *type);
7475

7576

Objects/typeobject.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static_builtin_state_clear(PyTypeObject *self)
140140

141141
static_builtin_state *state = static_builtin_state_get(interp, self);
142142
state->type = NULL;
143-
/* state->tp_weaklist should already have been cleared out. */
143+
assert(state->tp_weaklist == NULL); // It was already cleared out.
144144
static_builtin_index_clear(self);
145145

146146
assert(interp->types.num_builtins_initialized > 0);
@@ -4358,6 +4358,7 @@ _PyStaticType_Dealloc(PyTypeObject *type)
43584358
type->tp_flags &= ~Py_TPFLAGS_READY;
43594359

43604360
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
4361+
_PyStaticType_ClearWeakRefs(type);
43614362
static_builtin_state_clear(type);
43624363
/* We leave _Py_TPFLAGS_STATIC_BUILTIN set on tp_flags. */
43634364
}

Objects/weakrefobject.c

+16
Original file line numberDiff line numberDiff line change
@@ -1018,3 +1018,19 @@ PyObject_ClearWeakRefs(PyObject *object)
10181018
PyErr_Restore(err_type, err_value, err_tb);
10191019
}
10201020
}
1021+
1022+
/* This function is called by _PyStaticType_Dealloc() to clear weak references.
1023+
*
1024+
* This is called at the end of runtime finalization, so we can just
1025+
* wipe out the type's weaklist. We don't bother with callbacks
1026+
* or anything else.
1027+
*/
1028+
void
1029+
_PyStaticType_ClearWeakRefs(PyTypeObject *type)
1030+
{
1031+
static_builtin_state *state = _PyStaticType_GetState(type);
1032+
PyObject **list = _PyStaticType_GET_WEAKREFS_LISTPTR(state);
1033+
while (*list != NULL) {
1034+
clear_weakref((PyWeakReference *)*list);
1035+
}
1036+
}

0 commit comments

Comments
 (0)