-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
gh-95348: Split clear_weakref into two functions #95449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
06c9d7e
gh-95348: Split clear_weakref into two functions
PurityLake 40f247f
📜🤖 Added by blurb_it.
blurb-it[bot] 8a24c4f
gh-95348 Update NEWS blub
PurityLake 355625a
gh-95348: Update new functions in weakrefobject.c
PurityLake 8ec2929
Merge branch 'python:main' into fix-issue-95348
PurityLake 1e8e522
gh-95348: Update weakrefobject to use new function
PurityLake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -46,38 +46,56 @@ new_weakref(PyObject *ob, PyObject *callback) | |||||
} | ||||||
|
||||||
|
||||||
/* This function clears the passed-in reference and removes it from the | ||||||
* list of weak references for the referent. This is the only code that | ||||||
* removes an item from the doubly-linked list of weak references for an | ||||||
* object; it is also responsible for clearing the callback slot. | ||||||
/* This function clears the passed-in reference. It must have been | ||||||
* from the weaklist already. (See weaklist_remove_weakref().) | ||||||
*/ | ||||||
static void | ||||||
clear_weakref(PyWeakReference *self) | ||||||
{ | ||||||
PyObject *callback = self->wr_callback; | ||||||
|
||||||
if (self->wr_object != Py_None) { | ||||||
PyWeakReference **list = GET_WEAKREFS_LISTPTR(self->wr_object); | ||||||
self->wr_object = Py_None; | ||||||
|
||||||
if (*list == self) | ||||||
/* If 'self' is the end of the list (and thus self->wr_next == NULL) | ||||||
then the weakref list itself (and thus the value of *list) will | ||||||
end up being set to NULL. */ | ||||||
*list = self->wr_next; | ||||||
self->wr_object = Py_None; | ||||||
if (self->wr_prev != NULL) | ||||||
self->wr_prev->wr_next = self->wr_next; | ||||||
if (self->wr_next != NULL) | ||||||
self->wr_next->wr_prev = self->wr_prev; | ||||||
self->wr_prev = NULL; | ||||||
self->wr_next = NULL; | ||||||
} | ||||||
if (callback != NULL) { | ||||||
Py_DECREF(callback); | ||||||
self->wr_callback = NULL; | ||||||
} | ||||||
} | ||||||
|
||||||
/* This function removes the passed-in reference from the list of weak | ||||||
* references for the referent. | ||||||
*/ | ||||||
static void | ||||||
weaklist_remove_weakref(PyWeakReference **list, PyWeakReference *ref) | ||||||
{ | ||||||
if (*list == ref) { | ||||||
/* If 'ref' is the end of the list (and thus ref->wr_next == NULL) | ||||||
then the weakref list itself (and thus the value of *list) will | ||||||
end up being set to NULL. */ | ||||||
*list = ref->wr_next; | ||||||
} | ||||||
if (ref->wr_prev != NULL) { | ||||||
ref->wr_prev->wr_next = ref->wr_next; | ||||||
} | ||||||
if (ref->wr_next != NULL) { | ||||||
ref->wr_next->wr_prev = ref->wr_prev; | ||||||
} | ||||||
ref->wr_prev = NULL; | ||||||
ref->wr_next = NULL; | ||||||
} | ||||||
|
||||||
/* This function removes the pass in reference from the list of weak references | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* for the referent. | ||||||
*/ | ||||||
static void | ||||||
remove_weakref_from_referent(PyWeakReference *self) | ||||||
{ | ||||||
if (self->wr_object != Py_None) { | ||||||
PyWeakReference **list = GET_WEAKREFS_LISTPTR(self->wr_object); | ||||||
weaklist_remove_weakref(list, self); | ||||||
} | ||||||
} | ||||||
|
||||||
/* Cyclic gc uses this to *just* clear the passed-in reference, leaving | ||||||
* the callback intact and uncalled. It must be possible to call self's | ||||||
* tp_dealloc() after calling this, so self has to be left in a sane enough | ||||||
|
@@ -99,6 +117,7 @@ _PyWeakref_ClearRef(PyWeakReference *self) | |||||
/* Preserve and restore the callback around clear_weakref. */ | ||||||
callback = self->wr_callback; | ||||||
self->wr_callback = NULL; | ||||||
remove_weakref_from_referent(self); | ||||||
clear_weakref(self); | ||||||
self->wr_callback = callback; | ||||||
} | ||||||
|
@@ -107,6 +126,7 @@ static void | |||||
weakref_dealloc(PyObject *self) | ||||||
{ | ||||||
PyObject_GC_UnTrack(self); | ||||||
remove_weakref_from_referent((PyWeakReference *) self); | ||||||
clear_weakref((PyWeakReference *) self); | ||||||
Py_TYPE(self)->tp_free(self); | ||||||
} | ||||||
|
@@ -123,6 +143,7 @@ gc_traverse(PyWeakReference *self, visitproc visit, void *arg) | |||||
static int | ||||||
gc_clear(PyWeakReference *self) | ||||||
{ | ||||||
remove_weakref_from_referent(self); | ||||||
clear_weakref(self); | ||||||
return 0; | ||||||
} | ||||||
|
@@ -562,6 +583,7 @@ proxy_dealloc(PyWeakReference *self) | |||||
if (self->wr_callback != NULL) | ||||||
PyObject_GC_UnTrack((PyObject *)self); | ||||||
clear_weakref(self); | ||||||
remove_weakref_from_referent(self); | ||||||
PyObject_GC_Del(self); | ||||||
} | ||||||
|
||||||
|
@@ -958,8 +980,11 @@ PyObject_ClearWeakRefs(PyObject *object) | |||||
/* Remove the callback-less basic and proxy references */ | ||||||
if (*list != NULL && (*list)->wr_callback == NULL) { | ||||||
clear_weakref(*list); | ||||||
if (*list != NULL && (*list)->wr_callback == NULL) | ||||||
weaklist_remove_weakref(list, *list); | ||||||
if (*list != NULL && (*list)->wr_callback == NULL) { | ||||||
clear_weakref(*list); | ||||||
weaklist_remove_weakref(list, *list); | ||||||
} | ||||||
} | ||||||
if (*list != NULL) { | ||||||
PyWeakReference *current = *list; | ||||||
|
@@ -972,6 +997,7 @@ PyObject_ClearWeakRefs(PyObject *object) | |||||
|
||||||
current->wr_callback = NULL; | ||||||
clear_weakref(current); | ||||||
weaklist_remove_weakref(list, current); | ||||||
if (callback != NULL) { | ||||||
if (Py_REFCNT((PyObject *)current) > 0) { | ||||||
handle_callback(current, callback); | ||||||
|
@@ -1002,6 +1028,7 @@ PyObject_ClearWeakRefs(PyObject *object) | |||||
} | ||||||
current->wr_callback = NULL; | ||||||
clear_weakref(current); | ||||||
weaklist_remove_weakref(list, current); | ||||||
current = next; | ||||||
} | ||||||
for (i = 0; i < count; ++i) { | ||||||
|
@@ -1036,5 +1063,7 @@ _PyStaticType_ClearWeakRefs(PyTypeObject *type) | |||||
weaklist before clearing its wr_object and wr_callback. | ||||||
That is how we're able to loop over the list. */ | ||||||
clear_weakref((PyWeakReference *)*list); | ||||||
weaklist_remove_weakref((PyWeakReference **)list, | ||||||
(PyWeakReference *)list); | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clearing
wr_object
should still happen inclear_weakref()
, notremove_weakref()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing is, the program segfaults when I reverse the order of remove before clear
causes a segfault for me.