2016-05-02 12:35:45 +01:00
|
|
|
try:
|
2022-08-18 07:57:45 +01:00
|
|
|
import io
|
|
|
|
import sys
|
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
|
2020-03-23 02:26:08 +00:00
|
|
|
|
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
|
|
|
|
2020-03-23 02:26:08 +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
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
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)
|
|
|
|
|
2023-02-02 00:51:48 +00:00
|
|
|
|
2015-02-27 00:36:39 +00:00
|
|
|
# exception message with more than 1 source-code line
|
|
|
|
def f():
|
|
|
|
g()
|
2020-03-23 02:26:08 +00:00
|
|
|
|
|
|
|
|
2015-02-27 00:36:39 +00:00
|
|
|
def g():
|
2018-07-22 15:19:22 +01:00
|
|
|
raise Exception("fail")
|
2020-03-23 02:26:08 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-08-21 07:07:12 +01:00
|
|
|
# Test that an exception propagated through a finally doesn't have a traceback added there
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
f()
|
|
|
|
finally:
|
|
|
|
print("finally")
|
|
|
|
except Exception as e:
|
|
|
|
print("caught")
|
|
|
|
print_exc(e)
|
|
|
|
|
2019-08-21 07:08:43 +01:00
|
|
|
# Test that re-raising an exception doesn't add traceback info
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
f()
|
|
|
|
except Exception as e:
|
|
|
|
print("reraise")
|
|
|
|
print_exc(e)
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
print("caught")
|
|
|
|
print_exc(e)
|
|
|
|
|
2023-02-02 00:51:48 +00:00
|
|
|
|
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
|
2020-03-23 02:26:08 +00:00
|
|
|
|
|
|
|
|
2017-02-10 01:02:02 +00:00
|
|
|
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
|