Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-38006: Avoid closure in weakref.WeakValueDictionary #15641

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

Merged
merged 2 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/test_weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,11 @@ def test_threaded_weak_value_dict_deepcopy(self):
# copying should not result in a crash.
self.check_threaded_weak_dict_copy(weakref.WeakValueDictionary, True)

@support.cpython_only
def test_remove_closure(self):
d = weakref.WeakValueDictionary()
self.assertIsNone(d._remove.__closure__)


from test import mapping_tests

Expand Down
4 changes: 2 additions & 2 deletions Lib/weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ def remove(wr, selfref=ref(self), _atomic_removal=_remove_dead_weakref):
else:
# Atomic removal is necessary since this function
# can be called asynchronously by the GC
_atomic_removal(d, wr.key)
_atomic_removal(self.data, wr.key)
self._remove = remove
# A list of keys to be removed
self._pending_removals = []
self._iterating = set()
self.data = d = {}
self.data = {}
self.update(other, **kw)

def _commit_removals(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
weakref.WeakValueDictionary defines a local remove() function used as
callback for weak references. This function was created with a closure.
Modify the implementation to avoid the closure.