Skip to content

Commit

Permalink
py/vm: Don't add traceback info for exceptions that are re-raised.
Browse files Browse the repository at this point in the history
With this patch exceptions that are re-raised have improved tracebacks
(less confusing, match CPython), and it makes re-raise slightly more
efficient (in time and RAM) because they no longer need to add a traceback.
Also general VM performance is not measurably affected.

Partially fixes issue #2928.
  • Loading branch information
dpgeorge committed Aug 28, 2019
1 parent 16f6169 commit 08c1fe5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion py/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1362,8 +1362,10 @@ unwind_jump:;
// 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_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
12 changes: 12 additions & 0 deletions tests/misc/print_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ def g():
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

0 comments on commit 08c1fe5

Please sign in to comment.