summaryrefslogtreecommitdiff
path: root/tests/micropython/heapalloc_exc_raise.py
blob: d60e14ce5de6faac753f8857fb02a9f29166b050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Test that we can raise and catch (preallocated) exception
# without memory allocation.
import micropython

e = ValueError("error")

def func():
    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_lock()
func()
print("ok")
micropython.heap_unlock()