diff --git a/tests/micropython/native_try.py b/tests/micropython/native_try.py new file mode 100644 index 0000000000..2e41bf2ea1 --- /dev/null +++ b/tests/micropython/native_try.py @@ -0,0 +1,39 @@ +# test native try handling + +# basic try-finally +@micropython.native +def f(): + try: + fail + finally: + print('finally') +try: + f() +except NameError: + print('NameError') + +# nested try-except with try-finally +@micropython.native +def f(): + try: + try: + fail + finally: + print('finally') + except NameError: + print('NameError') +f() + +# check that locals written to in try blocks keep their values +@micropython.native +def f(): + a = 100 + try: + print(a) + a = 200 + fail + except NameError: + print(a) + a = 300 + print(a) +f() diff --git a/tests/micropython/native_try.py.exp b/tests/micropython/native_try.py.exp new file mode 100644 index 0000000000..96596ce5f5 --- /dev/null +++ b/tests/micropython/native_try.py.exp @@ -0,0 +1,7 @@ +finally +NameError +finally +NameError +100 +200 +300 diff --git a/tests/micropython/native_try_deep.py b/tests/micropython/native_try_deep.py new file mode 100644 index 0000000000..7fac4f0f38 --- /dev/null +++ b/tests/micropython/native_try_deep.py @@ -0,0 +1,34 @@ +# test native try handling + +# deeply nested try (9 deep) +@micropython.native +def f(): + try: + try: + try: + try: + try: + try: + try: + try: + try: + raise ValueError + finally: + print(8) + finally: + print(7) + finally: + print(6) + finally: + print(5) + finally: + print(4) + finally: + print(3) + finally: + print(2) + finally: + print(1) + except ValueError: + print('ValueError') +f() diff --git a/tests/micropython/native_try_deep.py.exp b/tests/micropython/native_try_deep.py.exp new file mode 100644 index 0000000000..84c6beae31 --- /dev/null +++ b/tests/micropython/native_try_deep.py.exp @@ -0,0 +1,9 @@ +8 +7 +6 +5 +4 +3 +2 +1 +ValueError diff --git a/tests/micropython/native_with.py b/tests/micropython/native_with.py new file mode 100644 index 0000000000..343f3e8d38 --- /dev/null +++ b/tests/micropython/native_with.py @@ -0,0 +1,28 @@ +# test with handling within a native function + +class C: + def __init__(self): + print('__init__') + def __enter__(self): + print('__enter__') + def __exit__(self, a, b, c): + print('__exit__', a, b, c) + +# basic with +@micropython.native +def f(): + with C(): + print(1) +f() + +# nested with and try-except +@micropython.native +def f(): + try: + with C(): + print(1) + fail + print(2) + except NameError: + print('NameError') +f() diff --git a/tests/micropython/native_with.py.exp b/tests/micropython/native_with.py.exp new file mode 100644 index 0000000000..6eef7822fb --- /dev/null +++ b/tests/micropython/native_with.py.exp @@ -0,0 +1,9 @@ +__init__ +__enter__ +1 +__exit__ None None None +__init__ +__enter__ +1 +__exit__ name 'fail' is not defined None +NameError diff --git a/tests/micropython/viper_try.py b/tests/micropython/viper_try.py new file mode 100644 index 0000000000..d75b3418e3 --- /dev/null +++ b/tests/micropython/viper_try.py @@ -0,0 +1,40 @@ +# test try handling within a viper function + +# basic try-finally +@micropython.viper +def f(): + try: + fail + finally: + print('finally') +try: + f() +except NameError: + print('NameError') + +# nested try-except with try-finally +@micropython.viper +def f(): + try: + try: + fail + finally: + print('finally') + except NameError: + print('NameError') +f() + +# check that locals written to in try blocks keep their values +@micropython.viper +def f(): + a = 100 + try: + print(a) + a = 200 + fail + except NameError: + print(a) + a = 300 + print(a) +f() + diff --git a/tests/micropython/viper_try.py.exp b/tests/micropython/viper_try.py.exp new file mode 100644 index 0000000000..96596ce5f5 --- /dev/null +++ b/tests/micropython/viper_try.py.exp @@ -0,0 +1,7 @@ +finally +NameError +finally +NameError +100 +200 +300 diff --git a/tests/micropython/viper_with.py b/tests/micropython/viper_with.py new file mode 100644 index 0000000000..2bc3c4f1b2 --- /dev/null +++ b/tests/micropython/viper_with.py @@ -0,0 +1,28 @@ +# test with handling within a viper function + +class C: + def __init__(self): + print('__init__') + def __enter__(self): + print('__enter__') + def __exit__(self, a, b, c): + print('__exit__', a, b, c) + +# basic with +@micropython.viper +def f(): + with C(): + print(1) +f() + +# nested with and try-except +@micropython.viper +def f(): + try: + with C(): + print(1) + fail + print(2) + except NameError: + print('NameError') +f() diff --git a/tests/micropython/viper_with.py.exp b/tests/micropython/viper_with.py.exp new file mode 100644 index 0000000000..6eef7822fb --- /dev/null +++ b/tests/micropython/viper_with.py.exp @@ -0,0 +1,9 @@ +__init__ +__enter__ +1 +__exit__ None None None +__init__ +__enter__ +1 +__exit__ name 'fail' is not defined None +NameError