summaryrefslogtreecommitdiffstatshomepage
path: root/tests
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2021-07-27 00:41:27 +1000
committerDamien George <damien@micropython.org>2022-03-10 10:43:21 +1100
commitcac939ddc3625da7e6cf1cf0309daba25fc1cedb (patch)
tree8e0417aab0ea5c9e22d6679edefb6a265cd31ab5 /tests
parentbc181550a4790f4dcfaf10e7e61ecfcdf346d5a8 (diff)
downloadmicropython-cac939ddc3625da7e6cf1cf0309daba25fc1cedb.tar.gz
micropython-cac939ddc3625da7e6cf1cf0309daba25fc1cedb.zip
py/modsys: Add optional sys.tracebacklimit attribute.
With behaviour as per CPython. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/basics/sys_tracebacklimit.py78
-rwxr-xr-xtests/run-tests.py1
-rw-r--r--tests/unix/extra_coverage.py.exp4
3 files changed, 81 insertions, 2 deletions
diff --git a/tests/basics/sys_tracebacklimit.py b/tests/basics/sys_tracebacklimit.py
new file mode 100644
index 0000000000..1ee638967f
--- /dev/null
+++ b/tests/basics/sys_tracebacklimit.py
@@ -0,0 +1,78 @@
+# test sys.tracebacklimit
+
+try:
+ try:
+ import usys as sys
+ import uio as io
+ except ImportError:
+ import sys
+ import io
+except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+try:
+ sys.tracebacklimit = 1000
+except AttributeError:
+ print("SKIP")
+ raise SystemExit
+
+if hasattr(sys, "print_exception"):
+ print_exception = sys.print_exception
+else:
+ import traceback
+
+ print_exception = lambda e, f: traceback.print_exception(None, e, sys.exc_info()[2], file=f)
+
+
+def print_exc(e):
+ buf = io.StringIO()
+ print_exception(e, buf)
+ s = buf.getvalue()
+ for l in s.split("\n"):
+ # Remove filename.
+ if l.startswith(" File "):
+ l = l.split('"')
+ print(l[0], l[2])
+ # 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.
+ elif not l.startswith(" "):
+ print(l)
+
+
+def f0():
+ raise ValueError("value")
+
+
+def f1():
+ f0()
+
+
+def f2():
+ f1()
+
+
+def f3():
+ f2()
+
+
+def ftop():
+ try:
+ f3()
+ except ValueError as er:
+ print_exc(er)
+
+
+ftop()
+
+for limit in range(4, -2, -1):
+ print("limit", limit)
+ sys.tracebacklimit = limit
+ ftop()
+
+
+# test deleting the attribute
+print(hasattr(sys, "tracebacklimit"))
+del sys.tracebacklimit
+print(hasattr(sys, "tracebacklimit"))
diff --git a/tests/run-tests.py b/tests/run-tests.py
index edd20b9bd5..dfe0a8e55f 100755
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -504,6 +504,7 @@ def run_tests(pyb, tests, args, result_dir, num_threads=1):
skip_tests.add("basics/del_local.py") # requires checking for unbound local
skip_tests.add("basics/exception_chain.py") # raise from is not supported
skip_tests.add("basics/scope_implicit.py") # requires checking for unbound local
+ skip_tests.add("basics/sys_tracebacklimit.py") # requires traceback info
skip_tests.add("basics/try_finally_return2.py") # requires raise_varargs
skip_tests.add("basics/unboundlocal.py") # requires checking for unbound local
skip_tests.add("extmod/uasyncio_event.py") # unknown issue
diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp
index 1a5a2cde85..67d299bca1 100644
--- a/tests/unix/extra_coverage.py.exp
+++ b/tests/unix/extra_coverage.py.exp
@@ -45,8 +45,8 @@ utime utimeq
argv atexit byteorder exc_info
exit getsizeof implementation maxsize
modules path platform print_exception
-stderr stdin stdout version
-version_info
+stderr stdin stdout tracebacklimit
+version version_info
ementation
# attrtuple
(start=1, stop=2, step=3)