bpo-34410: Fix a crash in the tee iterator when re-enter it. by serhiy-storchaka · Pull Request #15625 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/itertools.rst
37 changes: 37 additions & 0 deletions Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from functools import reduce
import sys
import struct
import threading
maxsize = support.MAX_Py_ssize_t
minsize = -maxsize-1

Expand Down Expand Up @@ -1494,6 +1495,42 @@ def test_tee_del_backward(self):
del forward, backward
raise

def test_tee_reenter(self):
class I:
first = True
def __iter__(self):
return self
def __next__(self):
first = self.first
self.first = False
if first:
return next(b)

a, b = tee(I())
with self.assertRaisesRegex(RuntimeError, "tee"):
next(a)

def test_tee_concurrent(self):
start = threading.Event()
finish = threading.Event()
class I:
def __iter__(self):
return self
def __next__(self):
start.set()
finish.wait()

a, b = tee(I())
thread = threading.Thread(target=next, args=[a])
thread.start()
try:
start.wait()
with self.assertRaisesRegex(RuntimeError, "tee"):
next(b)
finally:
finish.set()
thread.join()

def test_StopIteration(self):
self.assertRaises(StopIteration, next, zip())

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a crash in the :func:`tee` iterator when re-enter it. RuntimeError is
now raised in this case.
9 changes: 9 additions & 0 deletions Modules/itertoolsmodule.c