-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
Open
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement
Description
(inspired by #95302 (comment))
In Objects/weakrefobject.c, the clear_weakref() function is used to:
- pop the
PyWeakReferenceobject off its referenced object'stp_weaklist(a linked list) - clear (but not call) the callback, if any
However, in the places it is used, it isn't clear what it's doing. When adding a new use of clear_weakref() in one of my PRs it was particularly confusing to a reviewer what the relationship was between clear_weakref() and an object's weakref list (i.e. tp_weaklist).
While we could simply rename it to "remove_and_clear_weakref", it may make sense to do the following:
- move the linked-list part to a separate
remove_weakref()that has an explicitPyWeakReference **parameter - (hence, remove the linked list parts from
clear_weakref()) - add
remove_weakref_from_referent()which callsGET_WEAKREFS_LISTPTR(self->wr_object)and callsremove_weakref()on it - add a call to it everywhere we already call
clear_weakref()
Other observations:
- currently we aren't reseting
self->hashorself->vectorcallto NULL inclear_weakref() - we may be leaking weakref objects
PyObject_ClearWeakRefs()when the weakref doesn't have a callback
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement