← index #4667PR #660
Off-topic · high · value 5.168
QUERY · ISSUE

Print traceback without an exception

openby mzakharocscopened 2019-04-02updated 2019-05-14

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?

CANDIDATE · PULL REQUEST

stdlib/logging: Add full support for logging exception tracebacks.

mergedby andrewleechopened 2023-05-18updated 2024-09-06

I noticed recently that the current log.exception() function doesn't always print the tracebacks of the exception.

The current handling requires your build to be compiled with MICROPY_PY_SYS_EXC_INFO which is not always on by default.

This PR allows you to pass an exception object in as the exc_info kwarg (cpython allows this) so the following works regarless of the above build setting.

try:
    run_func()
except Exception as ex:
  log.exception("run_func failed", exc_info=ex)

Separately to that, currently even when sys.exc_info() is enabled, it's only printing the traceback to _stream = sys.stderr - not to the configured logging handlers. This means for instance if you've got a file log handler, it misses out on the tracebacks.
This PR also addresses this.

1 comment
dpgeorge · 2023-07-21

This looks good, thanks. And it's CPython compatible (CPython also allows exc_info to be a 3-tuple, but we don't need to support that).

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied