Chain exceptions while unwinding

This commit is contained in:
Jeff Epler 2022-10-17 09:08:38 -05:00
parent b6f86e1e73
commit dd443bacb8
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
4 changed files with 39 additions and 2 deletions

11
py/vm.c
View File

@ -1456,10 +1456,19 @@ unwind_loop:
// catch exception and pass to byte code
code_state->ip = exc_sp->handler;
mp_obj_t *sp = MP_TAGPTR_PTR(exc_sp->val_sp);
#if MICROPY_CPYTHON_EXCEPTION_CHAIN
mp_obj_t active_exception = get_active_exception(exc_sp, exc_stack);
#endif
// save this exception in the stack so it can be used in a reraise, if needed
exc_sp->prev_exc = nlr.ret_val;
mp_obj_t obj = MP_OBJ_FROM_PTR(nlr.ret_val);
#if MICROPY_CPYTHON_EXCEPTION_CHAIN
if (active_exception != MP_OBJ_NULL) {
mp_store_attr(obj, MP_QSTR___context__, active_exception);
}
#endif
// push exception object so it can be handled by bytecode
PUSH(MP_OBJ_FROM_PTR(nlr.ret_val));
PUSH(obj);
code_state->sp = sp;
#if MICROPY_STACKLESS

View File

@ -5,7 +5,7 @@ except AttributeError:
raise SystemExit
def print_exc_info(e):
print("exception", type(e), repr(e))
print("exception", type(e), e.args)
print("context", type(e.__context__), e.__suppress_context__)
print("cause", type(e.__cause__))
@ -44,3 +44,11 @@ try:
raise RuntimeError() from None
except Exception as e:
print_exc_info(e)
try:
try:
raise RuntimeError()
except Exception as inner:
1/0
except Exception as e:
print_exc_info(e)

View File

@ -53,3 +53,11 @@ try:
raise RuntimeError() from None
except Exception as e:
print_exc_info(e)
try:
try:
raise RuntimeError()
except Exception as inner:
1 / 0
except Exception as e:
print_exc_info(e)

View File

@ -41,3 +41,15 @@ Traceback (most recent call last):
RuntimeError:
------------------------------------------------------------------------
------------------------------------------------------------------------
Traceback (most recent call last):
File "circuitpython/traceback_test_chained.py", line 59, in <module>
RuntimeError:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "circuitpython/traceback_test_chained.py", line 61, in <module>
ZeroDivisionError: division by zero
------------------------------------------------------------------------