Skip to content

Commit 6fc6f10

Browse files
committed
tests/heapalloc_exc_raise.py: Heap alloc test for raising/catching exc.
1 parent 3d739eb commit 6fc6f10

File tree

2 files changed

+25
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)