Author: Armin Rigo <[email protected]>
Branch: py3.5
Changeset: r88720:8891136318b8
Date: 2016-11-28 20:27 +0100
http://bitbucket.org/pypy/pypy/changeset/8891136318b8/
Log: also fix WeakSet.__len__
diff --git a/lib-python/3/_weakrefset.py b/lib-python/3/_weakrefset.py
--- a/lib-python/3/_weakrefset.py
+++ b/lib-python/3/_weakrefset.py
@@ -65,7 +65,14 @@
yield item
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:
+ result += (wr() is not None)
+ return result
def __contains__(self, item):
try:
diff --git a/lib-python/3/test/test_weakref.py
b/lib-python/3/test/test_weakref.py
--- a/lib-python/3/test/test_weakref.py
+++ b/lib-python/3/test/test_weakref.py
@@ -1103,7 +1103,7 @@
# Keep an iterator alive
it = dct.items()
try:
- next(it)
+ print(next(it))
except StopIteration:
pass
del items
@@ -1112,7 +1112,10 @@
del it
gc.collect()
gc.collect()
+ print(list(dct.items()))
n2 = len(dct)
+ print(len(dct))
+ print(weakref)
# one item may be kept alive inside the iterator
self.assertIn(n1, (0, 1))
self.assertEqual(n2, 0)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit