2017-03-10 19:16:26 -05:00
|
|
|
import sys
|
2016-05-02 07:35:45 -04:00
|
|
|
try:
|
2017-03-10 19:16:26 -05:00
|
|
|
try:
|
|
|
|
import uio as io
|
|
|
|
except ImportError:
|
|
|
|
import io
|
2016-05-02 07:35:45 -04:00
|
|
|
except ImportError:
|
2017-03-10 19:16:26 -05:00
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2017-03-10 19:16:26 -05:00
|
|
|
|
2014-12-10 13:35:10 -05:00
|
|
|
if hasattr(sys, 'print_exception'):
|
|
|
|
print_exception = sys.print_exception
|
|
|
|
else:
|
|
|
|
import traceback
|
2015-10-02 08:01:47 -04:00
|
|
|
print_exception = lambda e, f: traceback.print_exception(None, e, sys.exc_info()[2], file=f)
|
2014-12-10 13:35:10 -05:00
|
|
|
|
2015-02-26 19:36:39 -05:00
|
|
|
def print_exc(e):
|
2014-12-10 13:35:10 -05:00
|
|
|
buf = io.StringIO()
|
|
|
|
print_exception(e, buf)
|
|
|
|
s = buf.getvalue()
|
|
|
|
for l in s.split("\n"):
|
2014-12-18 08:37:56 -05:00
|
|
|
# uPy on pyboard prints <stdin> as file, so remove filename.
|
|
|
|
if l.startswith(" File "):
|
2015-02-26 19:36:39 -05:00
|
|
|
l = l.split('"')
|
|
|
|
print(l[0], l[2])
|
2014-12-10 13:35:10 -05: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 08:37:56 -05:00
|
|
|
elif not l.startswith(" "):
|
2014-12-10 13:35:10 -05:00
|
|
|
print(l)
|
2015-02-26 19:36:39 -05:00
|
|
|
|
|
|
|
# basic exception message
|
|
|
|
try:
|
2018-07-22 10:19:22 -04:00
|
|
|
raise Exception('msg')
|
2015-02-26 19:36:39 -05:00
|
|
|
except Exception as e:
|
|
|
|
print('caught')
|
|
|
|
print_exc(e)
|
|
|
|
|
|
|
|
# exception message with more than 1 source-code line
|
|
|
|
def f():
|
|
|
|
g()
|
|
|
|
def g():
|
2018-07-22 10:19:22 -04:00
|
|
|
raise Exception('fail')
|
2015-02-26 19:36:39 -05:00
|
|
|
try:
|
|
|
|
f()
|
|
|
|
except Exception as e:
|
|
|
|
print('caught')
|
|
|
|
print_exc(e)
|
2017-02-09 20:02:02 -05:00
|
|
|
|
|
|
|
# Here we have a function with lots of bytecode generated for a single source-line, and
|
|
|
|
# there is an error right at the end of the bytecode. It should report the correct line.
|
|
|
|
def f():
|
|
|
|
f([1, 2], [1, 2], [1, 2], {1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:X})
|
|
|
|
return 1
|
|
|
|
try:
|
|
|
|
f()
|
|
|
|
except Exception as e:
|
|
|
|
print_exc(e)
|
2018-06-20 02:08:25 -04:00
|
|
|
|
|
|
|
# Test non-stream object passed as output object, only valid for uPy
|
|
|
|
if hasattr(sys, 'print_exception'):
|
|
|
|
try:
|
|
|
|
sys.print_exception(Exception, 1)
|
|
|
|
had_exception = False
|
|
|
|
except OSError:
|
|
|
|
had_exception = True
|
|
|
|
assert had_exception
|