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

Skip to content

Commit 2366933

Browse files
authored
bpo-38006: Avoid closure in weakref.WeakValueDictionary (GH-15641) (GH-15789)
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. (cherry picked from commit a2af05a)
1 parent 12228ce commit 2366933

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

Lib/test/test_weakref.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,11 @@ def test_threaded_weak_value_dict_deepcopy(self):
17701770
# copying should not result in a crash.
17711771
self.check_threaded_weak_dict_copy(weakref.WeakValueDictionary, True)
17721772

1773+
@support.cpython_only
1774+
def test_remove_closure(self):
1775+
d = weakref.WeakValueDictionary()
1776+
self.assertIsNone(d._remove.__closure__)
1777+
17731778

17741779
from test import mapping_tests
17751780

Lib/weakref.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ def remove(wr, selfref=ref(self), _atomic_removal=_remove_dead_weakref):
114114
else:
115115
# Atomic removal is necessary since this function
116116
# can be called asynchronously by the GC
117-
_atomic_removal(d, wr.key)
117+
_atomic_removal(self.data, wr.key)
118118
self._remove = remove
119119
# A list of keys to be removed
120120
self._pending_removals = []
121121
self._iterating = set()
122-
self.data = d = {}
122+
self.data = {}
123123
self.update(*args, **kw)
124124

125125
def _commit_removals(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
weakref.WeakValueDictionary defines a local remove() function used as
2+
callback for weak references. This function was created with a closure.
3+
Modify the implementation to avoid the closure.

0 commit comments

Comments
 (0)