2018-04-04 11:06:40 -04:00
|
|
|
# test some extreme cases of allocating exceptions and tracebacks
|
|
|
|
|
|
|
|
import micropython
|
|
|
|
|
2018-04-04 12:33:48 -04:00
|
|
|
# Check for stackless build, which can't call functions without
|
|
|
|
# allocating a frame on the heap.
|
|
|
|
try:
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
def stackless():
|
|
|
|
pass
|
|
|
|
|
|
|
|
micropython.heap_lock()
|
|
|
|
stackless()
|
|
|
|
micropython.heap_unlock()
|
2018-04-04 12:33:48 -04:00
|
|
|
except RuntimeError:
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
2018-04-04 11:06:40 -04:00
|
|
|
# some ports need to allocate heap for the emergency exception
|
|
|
|
try:
|
|
|
|
micropython.alloc_emergency_exception_buf(256)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2018-04-04 11:06:40 -04:00
|
|
|
def main():
|
|
|
|
# create an exception with many args while heap is locked
|
|
|
|
# should revert to empty tuple for args
|
|
|
|
micropython.heap_lock()
|
2021-03-15 09:57:36 -04:00
|
|
|
e = Exception(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
)
|
2018-04-04 11:06:40 -04:00
|
|
|
micropython.heap_unlock()
|
|
|
|
print(repr(e))
|
|
|
|
|
|
|
|
# create an exception with a long formatted error message while heap is locked
|
|
|
|
# should use emergency exception buffer and truncate the message
|
|
|
|
def f():
|
|
|
|
pass
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2018-04-04 11:06:40 -04:00
|
|
|
micropython.heap_lock()
|
|
|
|
try:
|
2021-03-15 09:57:36 -04:00
|
|
|
f(
|
|
|
|
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=1
|
|
|
|
)
|
2018-04-04 11:06:40 -04:00
|
|
|
except Exception as er:
|
|
|
|
e = er
|
|
|
|
micropython.heap_unlock()
|
2018-04-04 13:03:16 -04:00
|
|
|
print(repr(e)[:10])
|
2018-04-04 11:06:40 -04:00
|
|
|
|
|
|
|
# create an exception with a long formatted error message while heap is low
|
|
|
|
# should use the heap and truncate the message
|
|
|
|
lst = []
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
lst = [lst]
|
|
|
|
except MemoryError:
|
|
|
|
break
|
|
|
|
try:
|
2021-03-15 09:57:36 -04:00
|
|
|
f(
|
|
|
|
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=1
|
|
|
|
)
|
2018-04-04 11:06:40 -04:00
|
|
|
except Exception as er:
|
|
|
|
e = er
|
2018-04-04 12:33:48 -04:00
|
|
|
lst[0] = None
|
2018-04-04 11:06:40 -04:00
|
|
|
lst = None
|
2018-04-04 13:03:16 -04:00
|
|
|
print(repr(e)[:10])
|
2018-04-04 11:06:40 -04:00
|
|
|
|
|
|
|
# raise a deep exception with the heap locked
|
|
|
|
# should use emergency exception and be unable to resize traceback array
|
|
|
|
def g():
|
|
|
|
g()
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2018-04-04 11:06:40 -04:00
|
|
|
micropython.heap_lock()
|
|
|
|
try:
|
|
|
|
g()
|
|
|
|
except Exception as er:
|
|
|
|
e = er
|
|
|
|
micropython.heap_unlock()
|
2018-04-04 13:03:16 -04:00
|
|
|
print(repr(e)[:13])
|
2018-04-04 11:06:40 -04:00
|
|
|
|
|
|
|
# create an exception on the heap with some traceback on the heap, but then
|
|
|
|
# raise it with the heap locked so it can't allocate any more traceback
|
2021-03-15 09:57:36 -04:00
|
|
|
exc = Exception("my exception")
|
2018-04-04 11:06:40 -04:00
|
|
|
try:
|
|
|
|
raise exc
|
|
|
|
except:
|
|
|
|
pass
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2018-04-04 11:06:40 -04:00
|
|
|
def h(e):
|
|
|
|
raise e
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2018-04-04 11:06:40 -04:00
|
|
|
micropython.heap_lock()
|
|
|
|
try:
|
|
|
|
h(exc)
|
|
|
|
except Exception as er:
|
|
|
|
e = er
|
|
|
|
micropython.heap_unlock()
|
|
|
|
print(repr(e))
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2018-04-04 11:06:40 -04:00
|
|
|
main()
|