diff options
author | Damien George <damien.p.george@gmail.com> | 2018-09-04 14:37:07 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2018-09-04 14:37:07 +1000 |
commit | b14c705c180f10b52af95fbe2a76ff0c9d8c39d4 (patch) | |
tree | 14a56dbaf51dcc139d9b2df6cea5b4ed989fbce8 | |
parent | 938daa4ff91f980c257cc1896b22c830e86a52ba (diff) |
tests/basics: Add more tests for return within try-finally.
-rw-r--r-- | tests/basics/try_finally_return4.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/tests/basics/try_finally_return4.py b/tests/basics/try_finally_return4.py new file mode 100644 index 000000000..8b54fe92c --- /dev/null +++ b/tests/basics/try_finally_return4.py @@ -0,0 +1,83 @@ +# test try-finally with return, where unwinding return has to go through +# another try-finally which may affect the behaviour of the return + +# case where a simple try-finally executes during an unwinding return +def f(x): + try: + try: + if x: + return 42 + finally: + try: + print(1) + finally: + print(2) + print(3) + print(4) + finally: + print(5) +print(f(0)) +print(f(1)) + +# case where an unwinding return is replaced by another one +def f(x): + try: + try: + if x: + return 42 + finally: + try: + print(1) + return 43 + finally: + print(2) + print(3) + print(4) + finally: + print(5) +print(f(0)) +print(f(1)) + +# case where an unwinding return is cancelled by an exception +def f(x): + try: + try: + if x: + return 42 + finally: + try: + print(1) + raise ValueError # cancels any active return + finally: + print(2) + print(3) + print(4) + finally: + print(5) +try: + print(f(0)) +except: + print('caught') +try: + print(f(1)) +except: + print('caught') + +# case where an unwinding return is cancelled then resumed +def f(x): + try: + try: + if x: + return 42 + finally: + try: + print(1) + raise Exception # cancels any active return + except: # cancels the exception and resumes any active return + print(2) + print(3) + print(4) + finally: + print(5) +print(f(0)) +print(f(1)) |