Issue #20006: Fix sporadic failures in test_weakset.

This commit is contained in:
Antoine Pitrou 2013-12-18 00:32:02 +01:00
commit 74940dc928
2 changed files with 8 additions and 2 deletions

View File

@ -60,6 +60,8 @@ class WeakSet:
for itemref in self.data: for itemref in self.data:
item = itemref() item = itemref()
if item is not None: if item is not None:
# Caveat: the iterator will keep a strong reference to
# `item` until it is resumed or closed.
yield item yield item
def __len__(self): def __len__(self):

View File

@ -370,10 +370,14 @@ class TestWeakSet(unittest.TestCase):
def testcontext(): def testcontext():
try: try:
it = iter(s) it = iter(s)
next(it) # Start iterator
del it yielded = ustr(str(next(it)))
# Schedule an item for removal and recreate it # Schedule an item for removal and recreate it
u = ustr(str(items.pop())) 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 gc.collect() # just in case
yield u yield u
finally: finally: