2018-08-16 00:43:35 -04:00
|
|
|
# test with handling within a viper function
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-08-16 00:43:35 -04:00
|
|
|
class C:
|
|
|
|
def __init__(self):
|
|
|
|
print("__init__")
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-08-16 00:43:35 -04:00
|
|
|
def __enter__(self):
|
|
|
|
print("__enter__")
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-08-16 00:43:35 -04:00
|
|
|
def __exit__(self, a, b, c):
|
|
|
|
print("__exit__", a, b, c)
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2018-08-16 00:43:35 -04:00
|
|
|
|
|
|
|
# basic with
|
|
|
|
@micropython.viper
|
|
|
|
def f():
|
|
|
|
with C():
|
|
|
|
print(1)
|
2020-03-22 22:26:08 -04:00
|
|
|
|
|
|
|
|
2018-08-16 00:43:35 -04:00
|
|
|
f()
|
|
|
|
|
2023-02-01 19:51:48 -05:00
|
|
|
|
2018-08-16 00:43:35 -04:00
|
|
|
# nested with and try-except
|
|
|
|
@micropython.viper
|
|
|
|
def f():
|
|
|
|
try:
|
|
|
|
with C():
|
|
|
|
print(1)
|
|
|
|
fail
|
|
|
|
print(2)
|
|
|
|
except NameError:
|
|
|
|
print("NameError")
|
2020-03-22 22:26:08 -04:00
|
|
|
|
|
|
|
|
2018-08-16 00:43:35 -04:00
|
|
|
f()
|