From 6fc6f10b1ec61ecaa1c72ed767a41fbf5bacaac9 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 20 Feb 2017 04:22:32 +0300 Subject: [PATCH] tests/heapalloc_exc_raise.py: Heap alloc test for raising/catching exc. --- tests/micropython/heapalloc_exc_raise.py | 23 ++++++++++++++++++++ tests/micropython/heapalloc_exc_raise.py.exp | 2 ++ 2 files changed, 25 insertions(+) create mode 100644 tests/micropython/heapalloc_exc_raise.py create mode 100644 tests/micropython/heapalloc_exc_raise.py.exp diff --git a/tests/micropython/heapalloc_exc_raise.py b/tests/micropython/heapalloc_exc_raise.py new file mode 100644 index 0000000000..d60e14ce5d --- /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() diff --git a/tests/micropython/heapalloc_exc_raise.py.exp b/tests/micropython/heapalloc_exc_raise.py.exp new file mode 100644 index 0000000000..1e9edc75d3 --- /dev/null +++ b/tests/micropython/heapalloc_exc_raise.py.exp @@ -0,0 +1,2 @@ +error +ok