diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2013-12-28 23:25:38 +0100 |
---|---|---|
committer | Stefan Behnel <stefan_ml@behnel.de> | 2013-12-28 23:25:38 +0100 |
commit | 51dbca9bda082e84f34c725083f37a987753fc37 (patch) | |
tree | c7652552c3474c1f4b9ba18007f9ebea15ce66e1 /tests/run/tryfinally.pyx | |
parent | 5af2dfe29781a32b1515612d98955740729c55f2 (diff) | |
download | cython-51dbca9bda082e84f34c725083f37a987753fc37.tar.gz |
extend try-finally tests
--HG--
extra : amend_source : c193bf636bba3b0f0439a25f1078e81a065b99fe
Diffstat (limited to 'tests/run/tryfinally.pyx')
-rw-r--r-- | tests/run/tryfinally.pyx | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/tests/run/tryfinally.pyx b/tests/run/tryfinally.pyx index 49ac5b7d1..878fbbd5a 100644 --- a/tests/run/tryfinally.pyx +++ b/tests/run/tryfinally.pyx @@ -1,6 +1,9 @@ # mode: run # tag: tryfinally +import sys +IS_PY3 = sys.version_info[0] >= 3 + cimport cython try: @@ -26,6 +29,166 @@ def finally_except(): finally: raise TypeError + +def finally_pass(): + """ + >>> finally_pass() + Traceback (most recent call last): + ValueError + """ + try: + raise ValueError() + finally: + pass + + +cdef void swallow(): + try: + raise TypeError() + finally: + return + + +def finally_exception_check_return(): + """ + >>> if not IS_PY3: + ... sys.exc_clear() + >>> def py_check(): + ... try: raise ValueError() + ... finally: + ... if IS_PY3: + ... assert sys.exc_info()[0] == ValueError, str(sys.exc_info()) + ... else: + ... assert sys.exc_info() == (None, None, None), str(sys.exc_info()) + ... return 1 + >>> py_check() + 1 + >>> finally_exception_check_return() + 1 + """ + try: + raise ValueError() + finally: + if IS_PY3: + pass # FIXME: + #assert sys.exc_info()[0] == ValueError, str(sys.exc_info()) + else: + assert sys.exc_info() == (None, None, None), str(sys.exc_info()) + return 1 + + +def finally_exception_check_swallow(): + """ + >>> if not IS_PY3: + ... sys.exc_clear() + >>> def swallow(): + ... try: raise TypeError() + ... finally: return + >>> def py_check(): + ... try: raise ValueError() + ... finally: + ... swallow() + ... if IS_PY3: + ... assert sys.exc_info()[0] == ValueError, str(sys.exc_info()) + ... else: + ... assert sys.exc_info() == (None, None, None), str(sys.exc_info()) + >>> py_check() + Traceback (most recent call last): + ValueError + >>> if not IS_PY3: + ... sys.exc_clear() + >>> finally_exception_check_swallow() + Traceback (most recent call last): + ValueError + """ + try: + raise ValueError() + finally: + swallow() + if IS_PY3: + pass # FIXME: + #assert sys.exc_info()[0] == ValueError, str(sys.exc_info()) + else: + assert sys.exc_info() == (None, None, None), str(sys.exc_info()) + + +def finally_exception_break_check(): + """ + >>> if not IS_PY3: + ... sys.exc_clear() + >>> def py_check(): + ... i = None + ... for i in range(2): + ... try: raise ValueError() + ... finally: + ... if IS_PY3: + ... assert sys.exc_info()[0] == ValueError, str(sys.exc_info()) + ... else: + ... assert sys.exc_info() == (None, None, None), str(sys.exc_info()) + ... break + ... assert sys.exc_info() == (None, None, None), str(sys.exc_info()) + ... return i + >>> py_check() + 0 + >>> finally_exception_break_check() + 0 + """ + i = None + for i in range(2): + try: + raise ValueError() + finally: + if IS_PY3: + pass # FIXME: + #assert sys.exc_info()[0] == ValueError, str(sys.exc_info()) + else: + assert sys.exc_info() == (None, None, None), str(sys.exc_info()) + break + assert sys.exc_info() == (None, None, None), str(sys.exc_info()) + return i + + +def finally_exception_break_check_with_swallowed_raise(): + """ + >>> if not IS_PY3: + ... sys.exc_clear() + >>> def swallow(): + ... try: raise TypeError() + ... finally: return + >>> def py_check(): + ... i = None + ... for i in range(2): + ... try: raise ValueError() + ... finally: + ... swallow() + ... if IS_PY3: + ... assert sys.exc_info()[0] == ValueError, str(sys.exc_info()) + ... else: + ... assert sys.exc_info() == (None, None, None), str(sys.exc_info()) + ... break + ... assert sys.exc_info() == (None, None, None), str(sys.exc_info()) + ... return i + >>> py_check() + 0 + >>> finally_exception_break_check_with_swallowed_raise() + 0 + """ + i = None + for i in range(2): + try: + raise ValueError() + finally: + swallow() + if IS_PY3: + pass # FIXME: + #assert sys.exc_info()[0] == ValueError, str(sys.exc_info()) + else: + assert sys.exc_info() == (None, None, None), str(sys.exc_info()) + break + assert sys.exc_info() == (None, None, None), str(sys.exc_info()) + return i + + def try_return_cy(): """ >>> def try_return_py(): |