Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py: don't add traceback info for finally or re-raised exceptions #5032

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions py/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1358,10 +1358,12 @@ unwind_jump:;
#if MICROPY_STACKLESS
unwind_loop:
#endif
// set file and line number that the exception occurred at
// TODO: don't set traceback for exceptions re-raised by END_FINALLY.
// But consider how to handle nested exceptions.
if (nlr.ret_val != &mp_const_GeneratorExit_obj) {
// Set traceback info (file and line number) where the exception occurred, but not for:
// - constant GeneratorExit object, because it's const
// - exceptions re-raised by END_FINALLY
// - exceptions re-raised explicitly by "raise"
if (nlr.ret_val != &mp_const_GeneratorExit_obj && *code_state->ip != MP_BC_END_FINALLY
&& !(*code_state->ip == MP_BC_RAISE_VARARGS && code_state->ip[1] == 0)) {
const byte *ip = code_state->fun_bc->bytecode;
ip = mp_decode_uint_skip(ip); // skip n_state
ip = mp_decode_uint_skip(ip); // skip n_exc_stack
Expand Down
22 changes: 22 additions & 0 deletions tests/misc/print_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ def g():
print('caught')
print_exc(e)

# Test that an exception propagated through a finally doesn't have a traceback added there
try:
try:
f()
finally:
print('finally')
except Exception as e:
print('caught')
print_exc(e)

# Test that re-raising an exception doesn't add traceback info
try:
try:
f()
except Exception as e:
print('reraise')
print_exc(e)
raise
except Exception as e:
print('caught')
print_exc(e)

# Here we have a function with lots of bytecode generated for a single source-line, and
# there is an error right at the end of the bytecode. It should report the correct line.
def f():
Expand Down