From: Antoine Pitrou Date: Tue, 17 Dec 2013 23:28:36 +0000 (+0100) Subject: Issue #20006: Fix sporadic failures in test_weakset. X-Git-Tag: v2.7.8~187 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bd4b667ea4003647477a136e0b677e943af795a2;p=python Issue #20006: Fix sporadic failures in test_weakset. --- diff --git a/Lib/_weakrefset.py b/Lib/_weakrefset.py index 990c3a6bbc..627959b624 100644 --- a/Lib/_weakrefset.py +++ b/Lib/_weakrefset.py @@ -60,6 +60,8 @@ class WeakSet(object): 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): diff --git a/Lib/test/test_weakset.py b/Lib/test/test_weakset.py index 6b34a8d682..d735d9c255 100644 --- a/Lib/test/test_weakset.py +++ b/Lib/test/test_weakset.py @@ -473,9 +473,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: