Skip to content

Commit e40c35d

Browse files
committed
py/objexcept: Add uncatchable SystemAbort exception.
This adds an exception type that forces MicroPython to exit even if the user MicroPython script catches all exceptions. This exception can be raised by C code to force MicroPython to exit for safety reasons, such as very low battery or very high temperatures.
1 parent f44246c commit e40c35d

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

py/mpconfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,12 @@
658658
#define MICROPY_KBD_EXCEPTION (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
659659
#endif
660660

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+
661667
// Prefer to raise KeyboardInterrupt asynchronously (from signal or interrupt
662668
// handler) - if supported by a particular port.
663669
#ifndef MICROPY_ASYNC_KBD_INTR

py/obj.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,9 @@ extern const mp_obj_type_t mp_type_UnicodeError;
860860
extern const mp_obj_type_t mp_type_ValueError;
861861
extern const mp_obj_type_t mp_type_ViperTypeError;
862862
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
863866

864867
// Constant objects, globally accessible: None, False, True
865868
// These should always be accessed via the below macros.

py/objexcept.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,12 @@ MP_DEFINE_EXCEPTION(Exception, BaseException)
376376
MP_DEFINE_EXCEPTION(ResourceWarning, Warning)
377377
*/
378378

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+
379385
// *FORMAT-ON*
380386

381387
mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {

py/vm.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,8 +1441,12 @@ unwind_jump:;
14411441
POP_EXC_BLOCK();
14421442
}
14431443

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
14461450
code_state->ip = exc_sp->handler;
14471451
mp_obj_t *sp = MP_TAGPTR_PTR(exc_sp->val_sp);
14481452
// save this exception in the stack so it can be used in a reraise, if needed

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy