Skip to content

Commit 07802c2

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. Signed-off-by: Laurens Valk <laurens@pybricks.com>
1 parent e35bcb0 commit 07802c2

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ extern const mp_obj_type_t mp_type_RuntimeError;
854854
extern const mp_obj_type_t mp_type_StopAsyncIteration;
855855
extern const mp_obj_type_t mp_type_StopIteration;
856856
extern const mp_obj_type_t mp_type_SyntaxError;
857+
extern const mp_obj_type_t mp_type_SystemAbort;
857858
extern const mp_obj_type_t mp_type_SystemExit;
858859
extern const mp_obj_type_t mp_type_TypeError;
859860
extern const mp_obj_type_t mp_type_UnicodeError;

py/objexcept.c

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

379+
// Exception that can't be raised or caught by user code, not even with a
380+
// bare except. Can be used to force MicroPython to exit from C code.
381+
MP_DEFINE_EXCEPTION(SystemAbort, BaseException)
382+
379383
// *FORMAT-ON*
380384

381385
mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) {

py/vm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,8 +1441,8 @@ 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 && !mp_obj_is_type(MP_OBJ_FROM_PTR(nlr.ret_val), &mp_type_SystemAbort)) {
1445+
// catch exception (but not SystemAbort) and pass to byte code
14461446
code_state->ip = exc_sp->handler;
14471447
mp_obj_t *sp = MP_TAGPTR_PTR(exc_sp->val_sp);
14481448
// 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