circuitpython/tests/micropython/heapalloc_exc_raise.py
Paul Sokolovsky e02cb9ec31 tests/heapalloc_*: Refactor some tests to work in strict stackless mode.
In strict stackless mode, it's not possible to make a function call with
heap locked (because function activation record aka frame is allocated on
heap). So, if the only purpose of function is to introduce local variable
scope, move heap lock/unlock calls inside the function.
2017-12-11 12:00:41 +02:00

24 lines
669 B
Python

# Test that we can raise and catch (preallocated) exception
# without memory allocation.
import micropython
e = ValueError("error")
def func():
micropython.heap_lock()
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)
micropython.heap_unlock()
func()
print("ok")