2016-05-02 12:35:45 +01:00
|
|
|
try:
|
|
|
|
import uio as io
|
|
|
|
except ImportError:
|
|
|
|
import io
|
2014-12-10 18:35:10 +00:00
|
|
|
import sys
|
|
|
|
if hasattr(sys, 'print_exception'):
|
|
|
|
print_exception = sys.print_exception
|
|
|
|
else:
|
|
|
|
import traceback
|
2015-10-02 13:01:47 +01:00
|
|
|
print_exception = lambda e, f: traceback.print_exception(None, e, sys.exc_info()[2], file=f)
|
2014-12-10 18:35:10 +00:00
|
|
|
|
2015-02-27 00:36:39 +00:00
|
|
|
def print_exc(e):
|
2014-12-10 18:35:10 +00:00
|
|
|
buf = io.StringIO()
|
|
|
|
print_exception(e, buf)
|
|
|
|
s = buf.getvalue()
|
|
|
|
for l in s.split("\n"):
|
2014-12-18 13:37:56 +00:00
|
|
|
# uPy on pyboard prints <stdin> as file, so remove filename.
|
|
|
|
if l.startswith(" File "):
|
2015-02-27 00:36:39 +00:00
|
|
|
l = l.split('"')
|
|
|
|
print(l[0], l[2])
|
2014-12-10 18:35:10 +00:00
|
|
|
# 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.
|
2014-12-18 13:37:56 +00:00
|
|
|
elif not l.startswith(" "):
|
2014-12-10 18:35:10 +00:00
|
|
|
print(l)
|
2015-02-27 00:36:39 +00:00
|
|
|
|
|
|
|
# basic exception message
|
|
|
|
try:
|
2016-02-13 15:55:26 +00:00
|
|
|
1/0
|
2015-02-27 00:36:39 +00:00
|
|
|
except Exception as e:
|
|
|
|
print('caught')
|
|
|
|
print_exc(e)
|
|
|
|
|
|
|
|
# exception message with more than 1 source-code line
|
|
|
|
def f():
|
|
|
|
g()
|
|
|
|
def g():
|
2016-02-13 15:55:26 +00:00
|
|
|
2/0
|
2015-02-27 00:36:39 +00:00
|
|
|
try:
|
|
|
|
f()
|
|
|
|
except Exception as e:
|
|
|
|
print('caught')
|
|
|
|
print_exc(e)
|