File tree Expand file tree Collapse file tree 6 files changed +32
-3
lines changed Expand file tree Collapse file tree 6 files changed +32
-3
lines changed Original file line number Diff line number Diff line change 659
659
#endif
660
660
661
661
// Whether to provide the SystemAbort exception, used to exit MicroPython
662
- // even if user code catches all exceptions.
662
+ // even if user code catches all exceptions. This also provides
663
+ // mp_sched_system_exit_or_abort() to schedule the mp_system_exception object.
663
664
#ifndef MICROPY_ENABLE_SYSTEM_ABORT
664
665
#define MICROPY_ENABLE_SYSTEM_ABORT (0)
665
666
#endif
Original file line number Diff line number Diff line change @@ -165,6 +165,11 @@ typedef struct _mp_state_vm_t {
165
165
mp_obj_exception_t mp_kbd_exception ;
166
166
#endif
167
167
168
+ #if MICROPY_ENABLE_SYSTEM_ABORT
169
+ // exception object of type SystemExit or SystemAbort
170
+ mp_obj_exception_t mp_system_exception ;
171
+ #endif
172
+
168
173
// dictionary with loaded modules (may be exposed as sys.modules)
169
174
mp_obj_dict_t mp_loaded_modules_dict ;
170
175
Original file line number Diff line number Diff line change @@ -377,8 +377,9 @@ MP_DEFINE_EXCEPTION(Exception, BaseException)
377
377
*/
378
378
379
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.
380
+ // Exception that can't be raised or caught by user code, not even with a bare
381
+ // except. Do not raise directly but use mp_sched_system_exit_or_abort(true) to
382
+ // schedule it on the main thread.
382
383
MP_DEFINE_EXCEPTION (SystemAbort , BaseException )
383
384
#endif
384
385
Original file line number Diff line number Diff line change @@ -95,6 +95,14 @@ void mp_init(void) {
95
95
MP_STATE_VM (mp_kbd_exception ).args = (mp_obj_tuple_t * )& mp_const_empty_tuple_obj ;
96
96
#endif
97
97
98
+ #if MICROPY_ENABLE_SYSTEM_ABORT
99
+ // initialise the exception object for raising SystemExit or SystemAbort
100
+ MP_STATE_VM (mp_system_exception ).traceback_alloc = 0 ;
101
+ MP_STATE_VM (mp_system_exception ).traceback_len = 0 ;
102
+ MP_STATE_VM (mp_system_exception ).traceback_data = NULL ;
103
+ MP_STATE_VM (mp_system_exception ).args = (mp_obj_tuple_t * )& mp_const_empty_tuple_obj ;
104
+ #endif
105
+
98
106
#if MICROPY_ENABLE_COMPILER
99
107
// optimization disabled by default
100
108
MP_STATE_VM (mp_optimise_value ) = 0 ;
Original file line number Diff line number Diff line change @@ -75,6 +75,9 @@ void mp_deinit(void);
75
75
76
76
void mp_sched_exception (mp_obj_t exc );
77
77
void mp_sched_keyboard_interrupt (void );
78
+ #if MICROPY_ENABLE_SYSTEM_ABORT
79
+ void mp_sched_system_exit_or_abort (bool abort );
80
+ #endif
78
81
void mp_handle_pending (bool raise_exc );
79
82
80
83
#if MICROPY_ENABLE_SCHEDULER
Original file line number Diff line number Diff line change @@ -51,6 +51,17 @@ void MICROPY_WRAP_MP_SCHED_KEYBOARD_INTERRUPT(mp_sched_keyboard_interrupt)(void)
51
51
}
52
52
#endif
53
53
54
+ #if MICROPY_ENABLE_SYSTEM_ABORT
55
+ // Schedules SystemExit or SystemAbort on the main thread. Can be used by
56
+ // async sources to exit MicroPython. Use abort=true if the MicroPython script
57
+ // must not be able to catch it, not even with a bare except.
58
+ void MICROPY_WRAP_MP_SCHED_EXCEPTION (mp_sched_system_exit_or_abort )(bool abort ) {
59
+ MP_STATE_VM (mp_system_exception ).base .type = abort ?
60
+ & mp_type_SystemAbort : & mp_type_SystemExit ;
61
+ mp_sched_exception (MP_OBJ_FROM_PTR (& MP_STATE_VM (mp_system_exception )));
62
+ }
63
+ #endif
64
+
54
65
#if MICROPY_ENABLE_SCHEDULER
55
66
56
67
#define IDX_MASK (i ) ((i) & (MICROPY_SCHEDULER_DEPTH - 1))
You can’t perform that action at this time.
0 commit comments