Skip to content

do not chain exceptions to themselves #7415

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

Merged
merged 1 commit into from
Jan 4, 2023
Merged
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
6 changes: 3 additions & 3 deletions py/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ unwind_jump:;
mp_obj_t obj = mp_make_raise_obj(TOP());
#if MICROPY_CPYTHON_EXCEPTION_CHAIN
mp_obj_t active_exception = get_active_exception(exc_sp, exc_stack);
if (active_exception != MP_OBJ_NULL) {
if (active_exception != MP_OBJ_NULL && active_exception != obj) {
mp_store_attr(obj, MP_QSTR___context__, active_exception);
}
#endif
Expand All @@ -1164,7 +1164,7 @@ unwind_jump:;
#if MICROPY_CPYTHON_EXCEPTION_CHAIN
// search for the inner-most previous exception, to chain it
mp_obj_t active_exception = get_active_exception(exc_sp, exc_stack);
if (active_exception != MP_OBJ_NULL) {
if (active_exception != MP_OBJ_NULL && active_exception != obj) {
mp_store_attr(obj, MP_QSTR___context__, active_exception);
}
mp_store_attr(obj, MP_QSTR___cause__, cause);
Expand Down Expand Up @@ -1463,7 +1463,7 @@ unwind_jump:;
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) {
if (active_exception != MP_OBJ_NULL && active_exception != obj) {
mp_store_attr(obj, MP_QSTR___context__, active_exception);
}
#endif
Expand Down
10 changes: 10 additions & 0 deletions tests/circuitpython/traceback_test_chained.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,13 @@ def print_exc_info(e, chain=True):
1 / 0
except Exception as e:
print_exc_info(e)

try:
try:
1 / 0
except Exception as inner:
raise inner
except Exception as e:
print_exc_info(e, chain=False)
print_exc_info(e)
print()
15 changes: 15 additions & 0 deletions tests/circuitpython/traceback_test_chained.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,18 @@ Traceback (most recent call last):
ZeroDivisionError: division by zero
------------------------------------------------------------------------

------------------------------------------------------------------------
Traceback (most recent call last):
File "circuitpython/traceback_test_chained.py", line 70, in <module>
File "circuitpython/traceback_test_chained.py", line 68, in <module>
ZeroDivisionError: division by zero
------------------------------------------------------------------------

------------------------------------------------------------------------
Traceback (most recent call last):
File "circuitpython/traceback_test_chained.py", line 70, in <module>
File "circuitpython/traceback_test_chained.py", line 68, in <module>
ZeroDivisionError: division by zero
------------------------------------------------------------------------