2020-01-28 22:27:33 -05:00
|
|
|
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)
|
|
|
|
|
2023-08-15 12:56:22 -04:00
|
|
|
# NameError uses mp_obj_new_exception_msg_varg for NameError("name '%q' is not defined")
|
2022-03-07 00:47:33 -05:00
|
|
|
# `raise 0` uses mp_obj_new_exception_msg for TypeError("exceptions must derive from BaseException")
|
2020-01-28 22:27:33 -05:00
|
|
|
|
|
|
|
# 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:
|
2022-03-07 00:47:33 -05:00
|
|
|
raise 0
|
|
|
|
except TypeError as e:
|
2020-01-28 22:27:33 -05:00
|
|
|
print(type(e).__name__, e)
|
|
|
|
|
|
|
|
try:
|
|
|
|
name()
|
|
|
|
except NameError as e:
|
|
|
|
print(e.args[0])
|
|
|
|
|
|
|
|
try:
|
2022-03-07 00:47:33 -05:00
|
|
|
raise 0
|
|
|
|
except TypeError as e:
|
2020-01-28 22:27:33 -05:00
|
|
|
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:
|
2022-03-07 00:47:33 -05:00
|
|
|
raise 0
|
|
|
|
except TypeError as e:
|
2020-01-28 22:27:33 -05:00
|
|
|
print(type(e).__name__)
|
|
|
|
|
|
|
|
micropython.heap_unlock()
|