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.
This commit is contained in:
parent
b3b48c81f0
commit
a2af05a0d3
@ -1792,6 +1792,11 @@ class MappingTestCase(TestBase):
|
|||||||
# copying should not result in a crash.
|
# copying should not result in a crash.
|
||||||
self.check_threaded_weak_dict_copy(weakref.WeakValueDictionary, True)
|
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
|
from test import mapping_tests
|
||||||
|
|
||||||
|
@ -108,12 +108,12 @@ class WeakValueDictionary(_collections_abc.MutableMapping):
|
|||||||
else:
|
else:
|
||||||
# Atomic removal is necessary since this function
|
# Atomic removal is necessary since this function
|
||||||
# can be called asynchronously by the GC
|
# can be called asynchronously by the GC
|
||||||
_atomic_removal(d, wr.key)
|
_atomic_removal(self.data, wr.key)
|
||||||
self._remove = remove
|
self._remove = remove
|
||||||
# A list of keys to be removed
|
# A list of keys to be removed
|
||||||
self._pending_removals = []
|
self._pending_removals = []
|
||||||
self._iterating = set()
|
self._iterating = set()
|
||||||
self.data = d = {}
|
self.data = {}
|
||||||
self.update(other, **kw)
|
self.update(other, **kw)
|
||||||
|
|
||||||
def _commit_removals(self):
|
def _commit_removals(self):
|
||||||
|
@ -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.
|
Loading…
x
Reference in New Issue
Block a user