Skip to content

Commit f21835b

Browse files
committed
Fix test: guard against infinite recursion in re-entrant iterator
1 parent 1f51f66 commit f21835b

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

‎Lib/test/test_itertools.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,18 +1507,20 @@ 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-
zl = None
1511-
class ReentrantFilter:
1510+
entered = False
1511+
class ReentrantIter:
15121512
def __init__(self, it):
15131513
self.it = it
15141514
def __iter__(self):
15151515
return self
15161516
def __next__(self):
1517-
if zl is not None:
1517+
nonlocal entered
1518+
if not entered:
1519+
entered = True
15181520
for _ in zl:
15191521
pass
15201522
return next(self.it)
1521-
zl = zip_longest(ReentrantFilter(iter([1])), iter([2, 3]))
1523+
zl = zip_longest(ReentrantIter(iter([1])), iter([2, 3]))
15221524
list(zl)
15231525

15241526
@support.cpython_only

0 commit comments

Comments
 (0)