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

Skip to content

Commit 78d15fa

Browse files
bpo-38006: Avoid closure in weakref.WeakValueDictionary (GH-15641)
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) Co-authored-by: Victor Stinner <[email protected]>
1 parent b6ef8f2 commit 78d15fa

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
@@ -1785,6 +1785,11 @@ def test_threaded_weak_value_dict_deepcopy(self):
17851785
# copying should not result in a crash.
17861786
self.check_threaded_weak_dict_copy(weakref.WeakValueDictionary, True)
17871787

1788+
@support.cpython_only
1789+
def test_remove_closure(self):
1790+
d = weakref.WeakValueDictionary()
1791+
self.assertIsNone(d._remove.__closure__)
1792+
17881793

17891794
from test import mapping_tests
17901795

Lib/weakref.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ def remove(wr, selfref=ref(self), _atomic_removal=_remove_dead_weakref):
108108
else:
109109
# Atomic removal is necessary since this function
110110
# can be called asynchronously by the GC
111-
_atomic_removal(d, wr.key)
111+
_atomic_removal(self.data, wr.key)
112112
self._remove = remove
113113
# A list of keys to be removed
114114
self._pending_removals = []
115115
self._iterating = set()
116-
self.data = d = {}
116+
self.data = {}
117117
self.update(other, **kw)
118118

119119
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)