[pypy-commit] pypy py3.5: Try to fix Weak{Key, Value}Dictionary.__len__()

arigo pypy.commits at gmail.com
Mon Nov 28 14:56:15 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r88719:338b6c45ed55
Date: 2016-11-28 20:22 +0100
http://bitbucket.org/pypy/pypy/changeset/338b6c45ed55/

Log:	Try to fix Weak{Key,Value}Dictionary.__len__()

diff --git a/lib-python/3/weakref.py b/lib-python/3/weakref.py
--- a/lib-python/3/weakref.py
+++ b/lib-python/3/weakref.py
@@ -147,7 +147,14 @@
         del self.data[key]
 
     def __len__(self):
-        return len(self.data) - len(self._pending_removals)
+        # PyPy change: we can't rely on len(self.data) at all, because
+        # the weakref callbacks may be called at an unknown later time.
+#        return len(self.data) - len(self._pending_removals)
+#
+        result = 0
+        for wr in self.data.values():
+            result += (wr() is not None)
+        return result
 
     def __contains__(self, key):
         try:
@@ -376,11 +383,18 @@
         return self.data[ref(key)]
 
     def __len__(self):
-        if self._dirty_len and self._pending_removals:
-            # self._pending_removals may still contain keys which were
-            # explicitly removed, we have to scrub them (see issue #21173).
-            self._scrub_removals()
-        return len(self.data) - len(self._pending_removals)
+        # PyPy change: we can't rely on len(self.data) at all, because
+        # the weakref callbacks may be called at an unknown later time.
+#        if self._dirty_len and self._pending_removals:
+#            # self._pending_removals may still contain keys which were
+#            # explicitly removed, we have to scrub them (see issue #21173).
+#            self._scrub_removals()
+#        return len(self.data) - len(self._pending_removals)
+#
+        result = 0
+        for wr in self.data:
+            result += (wr() is not None)
+        return result
 
     def __repr__(self):
         return "<%s at %#x>" % (self.__class__.__name__, id(self))


More information about the pypy-commit mailing list