circuitpython/tests/micropython/heapalloc_exc_raise.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

26 lines
671 B
Python
Raw Permalink Normal View History

# 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
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()
2021-03-15 09:57:36 -04:00
func()
print("ok")