Skip to content

Commit 54a489a

Browse files
committed
Rewrite test to exhaust sibling iterator without self-recursion
1 parent f21835b commit 54a489a

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

Lib/test/test_itertools.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,20 +1507,22 @@ def test_zip_longest_result_gc(self):
15071507

15081508
def test_zip_longest_reentrant_iterator(self):
15091509
# A re-entrant iterator exhaust must not cause use-after-free.
1510-
entered = False
1511-
class ReentrantIter:
1512-
def __init__(self, it):
1510+
class ExhaustingIter:
1511+
def __init__(self, it, sibling):
15131512
self.it = it
1513+
self.sibling = sibling
1514+
self.first = True
15141515
def __iter__(self):
15151516
return self
15161517
def __next__(self):
1517-
nonlocal entered
1518-
if not entered:
1519-
entered = True
1520-
for _ in zl:
1518+
val = next(self.it)
1519+
if self.first:
1520+
self.first = False
1521+
for _ in self.sibling:
15211522
pass
1522-
return next(self.it)
1523-
zl = zip_longest(ReentrantIter(iter([1])), iter([2, 3]))
1523+
return val
1524+
sib = iter([2, 3])
1525+
zl = zip_longest(ExhaustingIter(iter([1, 4]), sib), sib)
15241526
list(zl)
15251527

15261528
@support.cpython_only

0 commit comments

Comments
 (0)