From 3c2b2acd8c21294d046f4d2ef9195a976a5ef999 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 26 Mar 2014 19:27:52 +0200 Subject: [PATCH] tests: Add testcases for yield from. --- tests/basics/gen-yield-from-close.py | 87 ++++++++++++++++++++++++++++ tests/basics/gen-yield-from-exc.py | 13 +++++ tests/basics/gen-yield-from-send.py | 14 +++++ tests/basics/gen-yield-from-throw.py | 19 ++++++ tests/basics/gen-yield-from.py | 42 ++++++++++++++ 5 files changed, 175 insertions(+) create mode 100644 tests/basics/gen-yield-from-close.py create mode 100644 tests/basics/gen-yield-from-exc.py create mode 100644 tests/basics/gen-yield-from-send.py create mode 100644 tests/basics/gen-yield-from-throw.py create mode 100644 tests/basics/gen-yield-from.py diff --git a/tests/basics/gen-yield-from-close.py b/tests/basics/gen-yield-from-close.py new file mode 100644 index 0000000000..7982d5c990 --- /dev/null +++ b/tests/basics/gen-yield-from-close.py @@ -0,0 +1,87 @@ +def gen(): + yield 1 + yield 2 + yield 3 + yield 4 + +def gen2(): + yield -1 + print((yield from gen())) + yield 10 + yield 11 + +g = gen2() +print(next(g)) +print(next(g)) +g.close() +try: + print(next(g)) +except StopIteration: + print("StopIteration") + + +# Now variation of same test, but with leaf generator +# swallowing GeneratorExit exception - its upstream gen +# generator should still receive one. +def gen3(): + yield 1 + try: + yield 2 + except GeneratorExit: + print("leaf caught GeneratorExit and swallowed it") + return + yield 3 + yield 4 + +def gen4(): + yield -1 + try: + print((yield from gen3())) + except GeneratorExit: + print("delegating caught GeneratorExit") + raise + yield 10 + yield 11 + +g = gen4() +print(next(g)) +print(next(g)) +print(next(g)) +g.close() +try: + print(next(g)) +except StopIteration: + print("StopIteration") + + +# Yet another variation - leaf generator gets GeneratorExit, +# but raises StopIteration instead. This still should close chain properly. +def gen5(): + yield 1 + try: + yield 2 + except GeneratorExit: + print("leaf caught GeneratorExit and raised StopIteration instead") + raise StopIteration(123) + yield 3 + yield 4 + +def gen6(): + yield -1 + try: + print((yield from gen5())) + except GeneratorExit: + print("delegating caught GeneratorExit") + raise + yield 10 + yield 11 + +g = gen6() +print(next(g)) +print(next(g)) +print(next(g)) +g.close() +try: + print(next(g)) +except StopIteration: + print("StopIteration") diff --git a/tests/basics/gen-yield-from-exc.py b/tests/basics/gen-yield-from-exc.py new file mode 100644 index 0000000000..01c954f5b7 --- /dev/null +++ b/tests/basics/gen-yield-from-exc.py @@ -0,0 +1,13 @@ +def gen(): + yield 1 + yield 2 + raise ValueError + +def gen2(): + try: + print((yield from gen())) + except ValueError: + print("caught ValueError from downstream") + +g = gen2() +print(list(g)) diff --git a/tests/basics/gen-yield-from-send.py b/tests/basics/gen-yield-from-send.py new file mode 100644 index 0000000000..c2e94b5ad8 --- /dev/null +++ b/tests/basics/gen-yield-from-send.py @@ -0,0 +1,14 @@ +def gen(): + print("sent:", (yield 1)) + yield 2 + +def gen2(): + print((yield from gen())) + +g = gen2() +next(g) +print("yielded:", g.send("val")) +try: + next(g) +except StopIteration: + print("StopIteration") diff --git a/tests/basics/gen-yield-from-throw.py b/tests/basics/gen-yield-from-throw.py new file mode 100644 index 0000000000..30960fb9c1 --- /dev/null +++ b/tests/basics/gen-yield-from-throw.py @@ -0,0 +1,19 @@ +def gen(): + try: + yield 1 + except ValueError: + print("got ValueError from upstream!") + yield "str1" + raise TypeError + +def gen2(): + print((yield from gen())) + +g = gen2() +print(next(g)) +print(g.throw(ValueError)) +try: + print(next(g)) +except TypeError: + print("got TypeError from downstream!") + diff --git a/tests/basics/gen-yield-from.py b/tests/basics/gen-yield-from.py new file mode 100644 index 0000000000..5196b48d2b --- /dev/null +++ b/tests/basics/gen-yield-from.py @@ -0,0 +1,42 @@ +# Case of terminating subgen using return with value +def gen(): + yield 1 + yield 2 + return 3 + +def gen2(): + print("here1") + print((yield from gen())) + print("here2") + +g = gen2() +print(list(g)) + + +# Like above, but terminate subgen using StopIteration +def gen3(): + yield 1 + yield 2 + raise StopIteration + +def gen4(): + print("here1") + print((yield from gen3())) + print("here2") + +g = gen4() +print(list(g)) + +# Like above, but terminate subgen using StopIteration with value +def gen5(): + yield 1 + yield 2 + raise StopIteration(123) + +def gen6(): + print("here1") + print((yield from gen5())) + print("here2") + +g = gen6() +print(list(g))