From 5d06a743035d942e9fbde924f85dd56992d97419 Mon Sep 17 00:00:00 2001 From: Rami Ali Date: Tue, 20 Dec 2016 12:57:39 +1100 Subject: [PATCH] tests/basics: Improve test coverage for generators. --- tests/basics/gen_yield_from_close.py | 14 ++++++++++++++ tests/basics/gen_yield_from_throw.py | 11 +++++++++++ tests/basics/gen_yield_from_throw2.py | 15 +++++++++++++++ tests/basics/gen_yield_from_throw2.py.exp | 3 +++ tests/basics/generator1.py | 3 +++ tests/run-tests | 2 +- 6 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/basics/gen_yield_from_throw2.py create mode 100644 tests/basics/gen_yield_from_throw2.py.exp diff --git a/tests/basics/gen_yield_from_close.py b/tests/basics/gen_yield_from_close.py index 7982d5c990..674e117a08 100644 --- a/tests/basics/gen_yield_from_close.py +++ b/tests/basics/gen_yield_from_close.py @@ -85,3 +85,17 @@ try: print(next(g)) except StopIteration: print("StopIteration") + +# case where generator ignores the close request and yields instead +def gen7(): + try: + yield 123 + except GeneratorExit: + yield 456 + +g = gen7() +print(next(g)) +try: + g.close() +except RuntimeError: + print('RuntimeError') diff --git a/tests/basics/gen_yield_from_throw.py b/tests/basics/gen_yield_from_throw.py index 30960fb9c1..829bf0f3b4 100644 --- a/tests/basics/gen_yield_from_throw.py +++ b/tests/basics/gen_yield_from_throw.py @@ -17,3 +17,14 @@ try: 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(StopIteration) +except StopIteration: + print('got StopIteration from downstream!') diff --git a/tests/basics/gen_yield_from_throw2.py b/tests/basics/gen_yield_from_throw2.py new file mode 100644 index 0000000000..2cff9e08ba --- /dev/null +++ b/tests/basics/gen_yield_from_throw2.py @@ -0,0 +1,15 @@ +# uPy differs from CPython for this test +# generator ignored GeneratorExit +def gen(): + try: + yield 123 + except GeneratorExit: + print('GeneratorExit') + yield 456 + +g = gen() +print(next(g)) +try: + g.throw(GeneratorExit) +except RuntimeError: + print('RuntimeError') diff --git a/tests/basics/gen_yield_from_throw2.py.exp b/tests/basics/gen_yield_from_throw2.py.exp new file mode 100644 index 0000000000..d5805b4947 --- /dev/null +++ b/tests/basics/gen_yield_from_throw2.py.exp @@ -0,0 +1,3 @@ +123 +GeneratorExit +RuntimeError diff --git a/tests/basics/generator1.py b/tests/basics/generator1.py index d4028b0ce3..ea1d3b38bc 100644 --- a/tests/basics/generator1.py +++ b/tests/basics/generator1.py @@ -20,3 +20,6 @@ for val in f(3): #print(gen.__next__()) #print(gen.__next__()) #print(gen.__next__()) + +# test printing, but only the first chars that match CPython +print(repr(f(0))[0:17]) diff --git a/tests/run-tests b/tests/run-tests index b858e96358..1a686584c3 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -271,7 +271,7 @@ def run_tests(pyb, tests, args): # Some tests are known to fail with native emitter # Remove them from the below when they work if args.emit == 'native': - skip_tests.update({'basics/%s.py' % t for t in 'gen_yield_from gen_yield_from_close gen_yield_from_ducktype gen_yield_from_exc gen_yield_from_iter gen_yield_from_send gen_yield_from_stopped gen_yield_from_throw generator1 generator2 generator_args generator_close generator_closure generator_exc generator_return generator_send'.split()}) # require yield + skip_tests.update({'basics/%s.py' % t for t in 'gen_yield_from gen_yield_from_close gen_yield_from_ducktype gen_yield_from_exc gen_yield_from_iter gen_yield_from_send gen_yield_from_stopped gen_yield_from_throw gen_yield_from_throw2 generator1 generator2 generator_args generator_close generator_closure generator_exc generator_return generator_send'.split()}) # require yield skip_tests.update({'basics/%s.py' % t for t in 'bytes_gen class_store_class globals_del string_join'.split()}) # require yield skip_tests.update({'basics/async_%s.py' % t for t in 'def await await2 for for2 with with2'.split()}) # require yield skip_tests.update({'basics/%s.py' % t for t in 'try_reraise try_reraise2'.split()}) # require raise_varargs