1import micropython
2
3# Tests both code paths for built-in exception raising.
4# mp_obj_new_exception_msg_varg (exception requires decompression at raise-time to format)
5# mp_obj_new_exception_msg (decompression can be deferred)
6
7# NameError uses mp_obj_new_exception_msg_varg for NameError("name '%q' isn't defined")
8# set.pop uses mp_obj_new_exception_msg for KeyError("pop from an empty set")
9
10# Tests that deferred decompression works both via print(e) and accessing the message directly via e.args.
11
12a = set()
13
14# First test the regular case (can use heap for allocating the decompression buffer).
15try:
16    name()
17except NameError as e:
18    print(type(e).__name__, e)
19
20try:
21    a.pop()
22except KeyError as e:
23    print(type(e).__name__, e)
24
25try:
26    name()
27except NameError as e:
28    print(e.args[0])
29
30try:
31    a.pop()
32except KeyError as e:
33    print(e.args[0])
34
35# Then test that it still works when the heap is locked (i.e. in ISR context).
36micropython.heap_lock()
37
38try:
39    name()
40except NameError as e:
41    print(type(e).__name__)
42
43try:
44    a.pop()
45except KeyError as e:
46    print(type(e).__name__)
47
48micropython.heap_unlock()
49