Merge pull request #7415 from jepler/fix-exception-chain-self

do not chain exceptions to themselves
This commit is contained in:
Dan Halbert 2023-01-04 11:39:16 -05:00 committed by GitHub
commit e7475153c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 3 deletions

View File

@ -1150,7 +1150,7 @@ unwind_return:
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
@ -1164,7 +1164,7 @@ unwind_return:
#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);
@ -1463,7 +1463,7 @@ unwind_loop:
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

View File

@ -62,3 +62,13 @@ try:
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()

View File

@ -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
------------------------------------------------------------------------