]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38006: Avoid closure in weakref.WeakValueDictionary (GH-15641) (GH-15789)
authorVictor Stinner <vstinner@python.org>
Mon, 9 Sep 2019 22:31:20 +0000 (00:31 +0200)
committerGitHub <noreply@github.com>
Mon, 9 Sep 2019 22:31:20 +0000 (00:31 +0200)
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 a2af05a0d3f0da06b8d432f52efa3ecf29038532)

Lib/test/test_weakref.py
Lib/weakref.py
Misc/NEWS.d/next/Library/2019-09-02-13-37-27.bpo-38006.Y7vA0Q.rst [new file with mode: 0644]

index ad7a6acfcc7d0bea01d7e59a0b0c4aca68d7550f..d3396fc9cf90ced651450f8df3e82ed59e14698a 100644 (file)
@@ -1770,6 +1770,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
 
index 59b3aa5621a33fc74473073c736018ff4bb0b72e..461c997a1293aedadf92c0d9686f405a5f9bc0cf 100644 (file)
@@ -114,12 +114,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(*args, **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 (file)
index 0000000..ff064ad
--- /dev/null
@@ -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.