mirror of
https://github.com/micropython/micropython.git
synced 2026-01-11 06:27:14 +01:00
py/objexcept: Allow compression of exception message text.
The decompression of error-strings is only done if the string is accessed via printing or via er.args. Tests are added for this feature to ensure the decompression works.
This commit is contained in:
committed by
Damien George
parent
92c83bd16b
commit
85858e72df
48
tests/micropython/heapalloc_exc_compressed.py
Normal file
48
tests/micropython/heapalloc_exc_compressed.py
Normal file
@@ -0,0 +1,48 @@
|
||||
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' isn't defined")
|
||||
# set.pop uses mp_obj_new_exception_msg for KeyError("pop from an empty set")
|
||||
|
||||
# Tests that deferred decompression works both via print(e) and accessing the message directly via e.args.
|
||||
|
||||
a = set()
|
||||
|
||||
# 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:
|
||||
a.pop()
|
||||
except KeyError as e:
|
||||
print(type(e).__name__, e)
|
||||
|
||||
try:
|
||||
name()
|
||||
except NameError as e:
|
||||
print(e.args[0])
|
||||
|
||||
try:
|
||||
a.pop()
|
||||
except KeyError 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:
|
||||
a.pop()
|
||||
except KeyError as e:
|
||||
print(type(e).__name__)
|
||||
|
||||
micropython.heap_unlock()
|
||||
6
tests/micropython/heapalloc_exc_compressed.py.exp
Normal file
6
tests/micropython/heapalloc_exc_compressed.py.exp
Normal file
@@ -0,0 +1,6 @@
|
||||
NameError name 'name' isn't defined
|
||||
KeyError pop from an empty set
|
||||
name 'name' isn't defined
|
||||
pop from an empty set
|
||||
NameError
|
||||
KeyError
|
||||
41
tests/micropython/heapalloc_exc_compressed_emg_exc.py
Normal file
41
tests/micropython/heapalloc_exc_compressed_emg_exc.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import micropython
|
||||
|
||||
# Does the full test from heapalloc_exc_compressed.py but while the heap is
|
||||
# locked (this can only work when the emergency exception buf is enabled).
|
||||
|
||||
# Some ports need to allocate heap for the emgergency exception buffer.
|
||||
try:
|
||||
micropython.alloc_emergency_exception_buf(256)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
a = set()
|
||||
|
||||
|
||||
def test():
|
||||
micropython.heap_lock()
|
||||
|
||||
try:
|
||||
name()
|
||||
except NameError as e:
|
||||
print(type(e).__name__, e)
|
||||
|
||||
try:
|
||||
a.pop()
|
||||
except KeyError as e:
|
||||
print(type(e).__name__, e)
|
||||
|
||||
try:
|
||||
name()
|
||||
except NameError as e:
|
||||
print(e.args[0])
|
||||
|
||||
try:
|
||||
a.pop()
|
||||
except KeyError as e:
|
||||
print(e.args[0])
|
||||
|
||||
micropython.heap_unlock()
|
||||
|
||||
|
||||
test()
|
||||
@@ -0,0 +1,4 @@
|
||||
NameError name 'name' isn't defined
|
||||
KeyError pop from an empty set
|
||||
name 'name' isn't defined
|
||||
pop from an empty set
|
||||
Reference in New Issue
Block a user