diff options
author | Benjamin Peterson <benjamin@python.org> | 2012-03-07 23:57:04 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2012-03-07 23:57:04 (GMT) |
commit | 099a78fe6d0a4c20126497940529d2b1fd41c5de (patch) | |
tree | 2a5940815471ae87cf7baf8830253d7db8a379da /Lib/test | |
parent | 33d21a24fa3da625e20eb4d543eb1f9f6392f96c (diff) | |
download | cpython-099a78fe6d0a4c20126497940529d2b1fd41c5de.zip cpython-099a78fe6d0a4c20126497940529d2b1fd41c5de.tar.gz cpython-099a78fe6d0a4c20126497940529d2b1fd41c5de.tar.bz2 |
make delegating generators say they running (closes #14220)
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_pep380.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/Lib/test/test_pep380.py b/Lib/test/test_pep380.py index 3a1db29..bdcfacd 100644 --- a/Lib/test/test_pep380.py +++ b/Lib/test/test_pep380.py @@ -847,6 +847,78 @@ class TestPEP380Operation(unittest.TestCase): yield from () self.assertRaises(StopIteration, next, g()) + def test_delegating_generators_claim_to_be_running(self): + # Check with basic iteration + def one(): + yield 0 + yield from two() + yield 3 + def two(): + yield 1 + try: + yield from g1 + except ValueError: + pass + yield 2 + g1 = one() + self.assertEqual(list(g1), [0, 1, 2, 3]) + # Check with send + g1 = one() + res = [next(g1)] + try: + while True: + res.append(g1.send(42)) + except StopIteration: + pass + self.assertEqual(res, [0, 1, 2, 3]) + # Check with throw + class MyErr(Exception): + pass + def one(): + try: + yield 0 + except MyErr: + pass + yield from two() + try: + yield 3 + except MyErr: + pass + def two(): + try: + yield 1 + except MyErr: + pass + try: + yield from g1 + except ValueError: + pass + try: + yield 2 + except MyErr: + pass + g1 = one() + res = [next(g1)] + try: + while True: + res.append(g1.throw(MyErr)) + except StopIteration: + pass + # Check with close + class MyIt(object): + def __iter__(self): + return self + def __next__(self): + return 42 + def close(self_): + self.assertTrue(g1.gi_running) + self.assertRaises(ValueError, next, g1) + def one(): + yield from MyIt() + g1 = one() + next(g1) + g1.close() + def test_main(): from test import support |