summaryrefslogtreecommitdiffstatshomepage
path: root/tests/misc/print_exception.py
blob: 8ca63a000bc73ea7ea8667941134b94bd45d89e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import io
import sys
if hasattr(sys, 'print_exception'):
    print_exception = sys.print_exception
else:
    import traceback
    print_exception = lambda e, f: traceback.print_exception(None, e, None, file=f)

try:
    XXX
except Exception as e:
    print('caught')
    buf = io.StringIO()
    print_exception(e, buf)
    s = buf.getvalue()
    for l in s.split("\n"):
        # uPy and CPy tracebacks differ in that CPy prints a source line for
        # each traceback entry. In this case, we know that offending line
        # has 4-space indent, so filter it out.
        if not l.startswith("    "):
            print(l)