forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathheapalloc_exc_compressed.py
46 lines (35 loc) · 1.12 KB
/
heapalloc_exc_compressed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import micropython
# Tests both code paths for built-in exception raising.
# mp_obj_new_exception_msg_varg (exception requires decompression at raise-time to format)
# mp_obj_new_exception_msg (decompression can be deferred)
# NameError uses mp_obj_new_exception_msg_varg for NameError("name '%q' is not defined")
# `raise 0` uses mp_obj_new_exception_msg for TypeError("exceptions must derive from BaseException")
# Tests that deferred decompression works both via print(e) and accessing the message directly via e.args.
# First test the regular case (can use heap for allocating the decompression buffer).
try:
name()
except NameError as e:
print(type(e).__name__, e)
try:
raise 0
except TypeError as e:
print(type(e).__name__, e)
try:
name()
except NameError as e:
print(e.args[0])
try:
raise 0
except TypeError as e:
print(e.args[0])
# Then test that it still works when the heap is locked (i.e. in ISR context).
micropython.heap_lock()
try:
name()
except NameError as e:
print(type(e).__name__)
try:
raise 0
except TypeError as e:
print(type(e).__name__)
micropython.heap_unlock()