diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-09-09 09:38:05 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-09 09:38:05 (GMT) |
commit | 2fb6921ab296f933caf361a662e6471e143abefc (patch) | |
tree | 7d2241b4fd7d9f6809a5de02cbf8c0d6c1806832 /Lib/test/test_itertools.py | |
parent | 0229b56d8c0cb65b8ad789e69dcd281fd92a6d96 (diff) | |
download | cpython-2fb6921ab296f933caf361a662e6471e143abefc.zip cpython-2fb6921ab296f933caf361a662e6471e143abefc.tar.gz cpython-2fb6921ab296f933caf361a662e6471e143abefc.tar.bz2 |
[2.7] bpo-34410: Fix a crash in the tee iterator when re-enter it. (GH-15625) (GH-15740)
RuntimeError is now raised in this case.
(cherry picked from commit 526a01467b3277f9fcf7f91e66c23321caa1245d)
Diffstat (limited to 'Lib/test/test_itertools.py')
-rw-r--r-- | Lib/test/test_itertools.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index 0427979..2cdcbb2 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -10,6 +10,10 @@ import random import copy import pickle from functools import reduce +try: + import threading +except ImportError: + threading = None maxsize = test_support.MAX_Py_ssize_t minsize = -maxsize-1 @@ -984,6 +988,43 @@ 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.assertRaisesRegexp(RuntimeError, "tee"): + next(a) + + @unittest.skipUnless(threading, 'Threading required for this test.') + 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.assertRaisesRegexp(RuntimeError, "tee"): + next(b) + finally: + finish.set() + thread.join() + def test_StopIteration(self): self.assertRaises(StopIteration, izip().next) |