traceback.format_exc failed due to sys.exc_info not exist
Is this expected? not design to work on MCU ports ?
>>> try:
... 1/0
... except:
... print(traceback.format_exc())
...
...
...
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
File "traceback.py", line 27, in format_exc
AttributeError: 'module' object has no attribute 'exc_info'
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.
I'm not sure, I haven't tried the traceback module from here it might be out of date.
Take a look at this, it might be what you want: https://docs.micropython.org/en/latest/library/sys.html#sys.print_exception
Thanks to @andrewleech, the above link worked!