diff options
Diffstat (limited to 'tests/basics')
-rw-r--r-- | tests/basics/gen_yield_from_throw2.py | 15 | ||||
-rw-r--r-- | tests/basics/gen_yield_from_throw2.py.exp | 3 | ||||
-rw-r--r-- | tests/basics/gen_yield_from_throw3.py | 30 |
3 files changed, 39 insertions, 9 deletions
diff --git a/tests/basics/gen_yield_from_throw2.py b/tests/basics/gen_yield_from_throw2.py index 2cff9e08ba..0abfdd8cc3 100644 --- a/tests/basics/gen_yield_from_throw2.py +++ b/tests/basics/gen_yield_from_throw2.py @@ -1,5 +1,5 @@ -# uPy differs from CPython for this test -# generator ignored GeneratorExit +# generator ignores a thrown GeneratorExit (this is allowed) + def gen(): try: yield 123 @@ -7,9 +7,12 @@ def gen(): print('GeneratorExit') yield 456 +# thrown a class g = gen() print(next(g)) -try: - g.throw(GeneratorExit) -except RuntimeError: - print('RuntimeError') +print(g.throw(GeneratorExit)) + +# thrown an instance +g = gen() +print(next(g)) +print(g.throw(GeneratorExit())) diff --git a/tests/basics/gen_yield_from_throw2.py.exp b/tests/basics/gen_yield_from_throw2.py.exp deleted file mode 100644 index d5805b4947..0000000000 --- a/tests/basics/gen_yield_from_throw2.py.exp +++ /dev/null @@ -1,3 +0,0 @@ -123 -GeneratorExit -RuntimeError diff --git a/tests/basics/gen_yield_from_throw3.py b/tests/basics/gen_yield_from_throw3.py new file mode 100644 index 0000000000..0f6c7c8429 --- /dev/null +++ b/tests/basics/gen_yield_from_throw3.py @@ -0,0 +1,30 @@ +# yield-from a user-defined generator with a throw() method + +class Iter: + def __iter__(self): + return self + + def __next__(self): + return 1 + + def throw(self, x): + print('throw', x) + return 456 + +def gen(): + yield from Iter() + +# calling close() should not call throw() +g = gen() +print(next(g)) +g.close() + +# can throw a non-exception object +g = gen() +print(next(g)) +print(g.throw(123)) + +# throwing an exception class just injects that class +g = gen() +print(next(g)) +print(g.throw(ZeroDivisionError)) |