2017-03-11 00:16:26 +00:00
|
|
|
import sys
|
2016-05-02 12:35:45 +01:00
|
|
|
try:
|
2017-03-11 00:16:26 +00:00
|
|
|
try:
|
|
|
|
import uio as io
|
|
|
|
except ImportError:
|
|
|
|
import io
|
2016-05-02 12:35:45 +01:00
|
|
|
except ImportError:
|
2017-03-11 00:16:26 +00:00
|
|
|
print("SKIP")
|
2017-06-10 18:14:16 +01:00
|
|
|
raise SystemExit
|
2017-03-11 00:16:26 +00:00
|
|
|
|
2014-12-10 18:35:10 +00:00
|
|
|
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:
|
2018-07-22 15:19:22 +01:00
|
|
|
raise Exception('msg')
|
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():
|
2018-07-22 15:19:22 +01:00
|
|
|
raise Exception('fail')
|
2015-02-27 00:36:39 +00:00
|
|
|
try:
|
|
|
|
f()
|
|
|
|
except Exception as e:
|
|
|
|
print('caught')
|
|
|
|
print_exc(e)
|
2017-02-10 01:02:02 +00: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():
|
2018-06-20 12:02:11 +01:00
|
|
|
f([1, 2], [1, 2], [1, 2], {1:1, 1:1, 1:1, 1:1, 1:1, 1:1, 1:f.X})
|
2017-02-10 01:02:02 +00:00
|
|
|
return 1
|
|
|
|
try:
|
|
|
|
f()
|
|
|
|
except Exception as e:
|
|
|
|
print_exc(e)
|
2018-06-20 07:08:25 +01: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
|