]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #20006: Fix sporadic failures in test_weakset.
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 17 Dec 2013 23:28:36 +0000 (00:28 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 17 Dec 2013 23:28:36 +0000 (00:28 +0100)
Lib/_weakrefset.py
Lib/test/test_weakset.py

index 6a98b88e33550c51438e41e7974b46458dca90a9..7f9923c6341a54398e346d95f988222dbd8016b0 100644 (file)
@@ -60,6 +60,8 @@ class WeakSet:
             for itemref in self.data:
                 item = itemref()
                 if item is not None:
+                    # Caveat: the iterator will keep a strong reference to
+                    # `item` until it is resumed or closed.
                     yield item
 
     def __len__(self):
index d8abe5e2b3bf3347f4be104afffdf8674fcd6021..fb22879dfab92de7c301a73423a4319ea4b96904 100644 (file)
@@ -370,9 +370,14 @@ class TestWeakSet(unittest.TestCase):
         def testcontext():
             try:
                 it = iter(s)
-                next(it)
+                # Start iterator
+                yielded = ustr(str(next(it)))
                 # Schedule an item for removal and recreate it
                 u = ustr(str(items.pop()))
+                if yielded == u:
+                    # The iterator still has a reference to the removed item,
+                    # advance it (issue #20006).
+                    next(it)
                 gc.collect()      # just in case
                 yield u
             finally: