File tree Expand file tree Collapse file tree 4 files changed +21
-2
lines changed Expand file tree Collapse file tree 4 files changed +21
-2
lines changed Original file line number Diff line number Diff line change 658
658
#define MICROPY_KBD_EXCEPTION (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
659
659
#endif
660
660
661
+ // Whether to provide the SystemAbort exception, used to exit MicroPython
662
+ // even if user code catches all exceptions.
663
+ #ifndef MICROPY_ENABLE_SYSTEM_ABORT
664
+ #define MICROPY_ENABLE_SYSTEM_ABORT (0)
665
+ #endif
666
+
661
667
// Prefer to raise KeyboardInterrupt asynchronously (from signal or interrupt
662
668
// handler) - if supported by a particular port.
663
669
#ifndef MICROPY_ASYNC_KBD_INTR
Original file line number Diff line number Diff line change @@ -860,6 +860,9 @@ extern const mp_obj_type_t mp_type_UnicodeError;
860
860
extern const mp_obj_type_t mp_type_ValueError ;
861
861
extern const mp_obj_type_t mp_type_ViperTypeError ;
862
862
extern const mp_obj_type_t mp_type_ZeroDivisionError ;
863
+ #if MICROPY_ENABLE_SYSTEM_ABORT
864
+ extern const mp_obj_type_t mp_type_SystemAbort ;
865
+ #endif
863
866
864
867
// Constant objects, globally accessible: None, False, True
865
868
// These should always be accessed via the below macros.
Original file line number Diff line number Diff line change @@ -376,6 +376,12 @@ MP_DEFINE_EXCEPTION(Exception, BaseException)
376
376
MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
377
377
*/
378
378
379
+ #if MICROPY_ENABLE_SYSTEM_ABORT
380
+ // Exception that can't be raised or caught by user code, not even with a
381
+ // bare except. Can be used to force MicroPython to exit from C code.
382
+ MP_DEFINE_EXCEPTION (SystemAbort , BaseException )
383
+ #endif
384
+
379
385
// *FORMAT-ON*
380
386
381
387
mp_obj_t mp_obj_new_exception (const mp_obj_type_t * exc_type ) {
Original file line number Diff line number Diff line change @@ -1441,8 +1441,12 @@ unwind_jump:;
1441
1441
POP_EXC_BLOCK ();
1442
1442
}
1443
1443
1444
- if (exc_sp >= exc_stack ) {
1445
- // catch exception and pass to byte code
1444
+ if (exc_sp >= exc_stack
1445
+ #if MICROPY_ENABLE_SYSTEM_ABORT
1446
+ && !mp_obj_exception_match (MP_OBJ_FROM_PTR (nlr .ret_val ), MP_OBJ_FROM_PTR (& mp_type_SystemAbort ))
1447
+ #endif
1448
+ ) {
1449
+ // catch exception (but not SystemAbort) and pass to byte code
1446
1450
code_state -> ip = exc_sp -> handler ;
1447
1451
mp_obj_t * sp = MP_TAGPTR_PTR (exc_sp -> val_sp );
1448
1452
// save this exception in the stack so it can be used in a reraise, if needed
You can’t perform that action at this time.
0 commit comments