mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
bpo-34410: Fix a crash in the tee iterator when re-enter it. (GH-15625)
RuntimeError is now raised in this case.
This commit is contained in:
parent
918b468b7d
commit
526a01467b
4 changed files with 52 additions and 0 deletions
|
@ -11,6 +11,7 @@ import pickle
|
|||
from functools import reduce
|
||||
import sys
|
||||
import struct
|
||||
import threading
|
||||
maxsize = support.MAX_Py_ssize_t
|
||||
minsize = -maxsize-1
|
||||
|
||||
|
@ -1494,6 +1495,42 @@ class TestBasicOps(unittest.TestCase):
|
|||
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())
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue