Commit 5d06a743 authored by Rami Ali's avatar Rami Ali Committed by Damien George
Browse files

tests/basics: Improve test coverage for generators.

parent 1e7a801e
...@@ -85,3 +85,17 @@ try: ...@@ -85,3 +85,17 @@ try:
print(next(g)) print(next(g))
except StopIteration: except StopIteration:
print("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')
...@@ -17,3 +17,14 @@ try: ...@@ -17,3 +17,14 @@ try:
except TypeError: except TypeError:
print("got TypeError from downstream!") 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!')
# 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')
123
GeneratorExit
RuntimeError
...@@ -20,3 +20,6 @@ for val in f(3): ...@@ -20,3 +20,6 @@ for val in f(3):
#print(gen.__next__()) #print(gen.__next__())
#print(gen.__next__()) #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])
...@@ -271,7 +271,7 @@ def run_tests(pyb, tests, args): ...@@ -271,7 +271,7 @@ def run_tests(pyb, tests, args):
# Some tests are known to fail with native emitter # Some tests are known to fail with native emitter
# Remove them from the below when they work # Remove them from the below when they work
if args.emit == 'native': 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/%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/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 skip_tests.update({'basics/%s.py' % t for t in 'try_reraise try_reraise2'.split()}) # require raise_varargs
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment