From a2af05a0d3f0da06b8d432f52efa3ecf29038532 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 9 Sep 2019 16:55:58 +0200 Subject: 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. --- Lib/test/test_weakref.py | 5 +++++ Lib/weakref.py | 4 ++-- Misc/NEWS.d/next/Library/2019-09-02-13-37-27.bpo-38006.Y7vA0Q.rst | 3 +++ 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-09-02-13-37-27.bpo-38006.Y7vA0Q.rst diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index 41f78e7..e14df9e 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -1792,6 +1792,11 @@ class MappingTestCase(TestBase): # 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 diff --git a/Lib/weakref.py b/Lib/weakref.py index 560deee..d17b3ed 100644 --- a/Lib/weakref.py +++ b/Lib/weakref.py @@ -108,12 +108,12 @@ class WeakValueDictionary(_collections_abc.MutableMapping): 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): diff --git a/Misc/NEWS.d/next/Library/2019-09-02-13-37-27.bpo-38006.Y7vA0Q.rst b/Misc/NEWS.d/next/Library/2019-09-02-13-37-27.bpo-38006.Y7vA0Q.rst new file mode 100644 index 0000000..ff064ad --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-09-02-13-37-27.bpo-38006.Y7vA0Q.rst @@ -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. -- cgit v0.12