diff options
| author | Damien George | 2018-09-28 11:33:08 +1000 |
|---|---|---|
| committer | Damien George | 2018-09-28 11:35:31 +1000 |
| commit | e6078dfed21470dd3b0f3a5e33c78b7db3501711 (patch) | |
| tree | 7b2d0c78ae045c1034d3fb9ca3a1c837effdc421 /tests/basics | |
| parent | e9012a20f7311805f1106e079542c6e8ab89817f (diff) | |
tests/basics: Split out gen throw tests from yield-from-throw tests.
Diffstat (limited to 'tests/basics')
| -rw-r--r-- | tests/basics/gen_yield_from_throw.py | 26 | ||||
| -rw-r--r-- | tests/basics/generator_throw.py | 43 |
2 files changed, 43 insertions, 26 deletions
diff --git a/tests/basics/gen_yield_from_throw.py b/tests/basics/gen_yield_from_throw.py index 4d65b3c17..703158f4d 100644 --- a/tests/basics/gen_yield_from_throw.py +++ b/tests/basics/gen_yield_from_throw.py @@ -16,29 +16,3 @@ try: print(next(g)) except TypeError: print("got TypeError from downstream!") - -# case where generator doesn't intercept the thrown/injected exception -def gen3(): - yield 123 - yield 456 - -g3 = gen3() -print(next(g3)) -try: - g3.throw(KeyError) -except KeyError: - print('got KeyError from downstream!') - -# case where a thrown exception is caught and stops the generator -def gen4(): - try: - yield 1 - yield 2 - except: - pass -g4 = gen4() -print(next(g4)) -try: - g4.throw(ValueError) -except StopIteration: - print('got StopIteration') diff --git a/tests/basics/generator_throw.py b/tests/basics/generator_throw.py new file mode 100644 index 000000000..bf5ff33a2 --- /dev/null +++ b/tests/basics/generator_throw.py @@ -0,0 +1,43 @@ +# case where generator doesn't intercept the thrown/injected exception +def gen(): + yield 123 + yield 456 + +g = gen() +print(next(g)) +try: + g.throw(KeyError) +except KeyError: + print('got KeyError from downstream!') + +# case where a thrown exception is caught and stops the generator +def gen(): + try: + yield 1 + yield 2 + except: + pass +g = gen() +print(next(g)) +try: + g.throw(ValueError) +except StopIteration: + print('got StopIteration') + +# generator ignores a thrown GeneratorExit (this is allowed) +def gen(): + try: + yield 123 + except GeneratorExit: + print('GeneratorExit') + yield 456 + +# thrown a class +g = gen() +print(next(g)) +print(g.throw(GeneratorExit)) + +# thrown an instance +g = gen() +print(next(g)) +print(g.throw(GeneratorExit())) |
