diff options
author | Damien George <damien@micropython.org> | 2021-07-27 00:41:27 +1000 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2022-03-10 10:43:21 +1100 |
commit | cac939ddc3625da7e6cf1cf0309daba25fc1cedb (patch) | |
tree | 8e0417aab0ea5c9e22d6679edefb6a265cd31ab5 /tests/basics/sys_tracebacklimit.py | |
parent | bc181550a4790f4dcfaf10e7e61ecfcdf346d5a8 (diff) | |
download | micropython-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/basics/sys_tracebacklimit.py')
-rw-r--r-- | tests/basics/sys_tracebacklimit.py | 78 |
1 files changed, 78 insertions, 0 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")) |