Print traceback without an exception
Hello, in CPython, there is a way to print a traceback outside of an exception, through traceback.print_stack() function. What would it take to add similar functionality to micropython?
py/modsys.c: Add sys._exc_traceback.
This new function makes it easier to provide custom formatting of exception stack traces, for example to make them fit on a tiny 16x8-character display. (Without this, I think you'd have to call sys.print_exception, somehow get the output as a string, and then scrape information from that string.)
I'm thinking of this as an experimental function that exposes internal details of MicroPython and therefore might change in the future. That's why it has the underscore in the name.
Here is an example of its output:
['test.py', 4, 'foo', 'test.py', 7, '<module>']
The regular output from sys.print_exception for the same exception is:
Traceback (most recent call last):
File "test.py", line 7, in <module>
File "test.py", line 4, in foo
ZeroDivisionError: divide by zero
Both outputs were generated by the following script:
import sys
def foo():
1/0
try:
foo()
except Exception as e:
sys.print_exception(e)
print(sys._exc_traceback(e))
Is this the right approach for getting info about exception stack traces? I'd be happy to change the name, change the output format, add tests, or add documentation.