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

Co-authored-by: Victor Stinner <vstinner@redhat.com>
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
index 6f15c03..d9e1b20 100644
--- a/Lib/test/test_weakref.py
+++ b/Lib/test/test_weakref.py
@@ -1785,6 +1785,11 @@
         # 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