1# Test that we can raise and catch (preallocated) exception 2# without memory allocation. 3import micropython 4 5e = ValueError("error") 6 7 8def func(): 9 micropython.heap_lock() 10 try: 11 # This works as is because traceback is not allocated 12 # if not possible (heap is locked, no memory). If heap 13 # is not locked, this would allocate a traceback entry. 14 # To avoid that, traceback should be warmed up (by raising 15 # it once after creation) and then cleared before each 16 # raise with: 17 # e.__traceback__ = None 18 raise e 19 except Exception as e2: 20 print(e2) 21 micropython.heap_unlock() 22 23 24func() 25print("ok") 26