2017-02-19 20:22:32 -05:00
|
|
|
# Test that we can raise and catch (preallocated) exception
|
|
|
|
# without memory allocation.
|
|
|
|
import micropython
|
|
|
|
|
|
|
|
e = ValueError("error")
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2017-02-19 20:22:32 -05:00
|
|
|
def func():
|
2017-12-11 04:56:20 -05:00
|
|
|
micropython.heap_lock()
|
2017-02-19 20:22:32 -05:00
|
|
|
try:
|
|
|
|
# This works as is because traceback is not allocated
|
|
|
|
# if not possible (heap is locked, no memory). If heap
|
|
|
|
# is not locked, this would allocate a traceback entry.
|
|
|
|
# To avoid that, traceback should be warmed up (by raising
|
|
|
|
# it once after creation) and then cleared before each
|
|
|
|
# raise with:
|
|
|
|
# e.__traceback__ = None
|
|
|
|
raise e
|
|
|
|
except Exception as e2:
|
|
|
|
print(e2)
|
2017-12-11 04:56:20 -05:00
|
|
|
micropython.heap_unlock()
|
2017-02-19 20:22:32 -05:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2017-02-19 20:22:32 -05:00
|
|
|
func()
|
|
|
|
print("ok")
|