1# test that we can generate a traceback without allocating
2
3import micropython
4import usys
5
6try:
7    import uio
8except ImportError:
9    print("SKIP")
10    raise SystemExit
11
12# preallocate exception instance with some room for a traceback
13global_exc = StopIteration()
14try:
15    raise global_exc
16except:
17    pass
18
19
20def test():
21    micropython.heap_lock()
22    global global_exc
23    global_exc.__traceback__ = None
24    try:
25        raise global_exc
26    except StopIteration:
27        print("StopIteration")
28    micropython.heap_unlock()
29
30
31# call test() with heap allocation disabled
32test()
33
34# print the exception that was raised
35buf = uio.StringIO()
36usys.print_exception(global_exc, buf)
37for l in buf.getvalue().split("\n"):
38    # uPy on pyboard prints <stdin> as file, so remove filename.
39    if l.startswith("  File "):
40        l = l.split('"')
41        print(l[0], l[2])
42    else:
43        print(l)
44