summaryrefslogtreecommitdiff
path: root/tests/micropython/heapalloc_exc_raise.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/micropython/heapalloc_exc_raise.py')
-rw-r--r--tests/micropython/heapalloc_exc_raise.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/tests/micropython/heapalloc_exc_raise.py b/tests/micropython/heapalloc_exc_raise.py
new file mode 100644
index 000000000..d60e14ce5
--- /dev/null
+++ b/tests/micropython/heapalloc_exc_raise.py
@@ -0,0 +1,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()