Skip to content

Commit 69c25ea

Browse files
committed
shared/runtime/pyexec: Make a raised SystemExit always do a forced exit.
The current situation with SystemExit and soft reset is the following: - `sys.exit()` follows CPython and just raises `SystemExit`. - On the unix port, raising `SystemExit` quits the application/MicroPython, whether at the REPL or in code (this follows CPython behaviour). - On bare-metal ports, raising `SystemExit` at the REPL does nothing, raising it in code will stop the code and drop into the REPL. - `machine.soft_reset()` raises `SystemExit` but with a special flag set, and bare-metal targets check this flag when it propagates to the top-level and do a soft reset when they receive it. The original idea here was that a bare-metal target can't "quit" like the unix port can, and so dropping to the REPL was considered the same as "quit". But this bare-metal behaviour is arguably inconsistent with unix, and "quit" should mean terminate everything, including REPL access. This commit changes the behaviour to the following, which is more consistent: - Raising `SystemExit` on a bare-metal port will do a soft reset (unless the exception is caught by the application). - `machine.soft_reset()` is now equivalent to `sys.exit()`. - unix port behaviour remains unchanged. Tested running the test suite on an stm32 board and everything still passes, in particular tests that skip by raising `SystemExit` still correctly skip. Signed-off-by: Damien George <[email protected]>
1 parent a734ee9 commit 69c25ea

File tree

5 files changed

+1
-17
lines changed

5 files changed

+1
-17
lines changed

extmod/modmachine.c

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ NORETURN static void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args);
6262
#endif
6363

6464
static mp_obj_t machine_soft_reset(void) {
65-
pyexec_system_exit = PYEXEC_FORCED_EXIT;
6665
mp_raise_type(&mp_type_SystemExit);
6766
}
6867
static MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);

ports/qemu-arm/modmachine.c

-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
// This file is never compiled standalone, it's included directly from
2828
// extmod/modmachine.c via MICROPY_PY_MACHINE_INCLUDEFILE.
2929

30-
// This variable is needed for machine.soft_reset(), but the variable is otherwise unused.
31-
int pyexec_system_exit = 0;
32-
3330
static void mp_machine_idle(void) {
3431
// Do nothing.
3532
}

ports/unix/modmachine.c

-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
#define MICROPY_PAGE_MASK (MICROPY_PAGE_SIZE - 1)
3737
#endif
3838

39-
// This variable is needed for machine.soft_reset(), but the variable is otherwise unused.
40-
int pyexec_system_exit = 0;
41-
4239
uintptr_t mod_machine_mem_get_addr(mp_obj_t addr_o, uint align) {
4340
uintptr_t addr = mp_obj_get_int_truncated(addr_o);
4441
if ((addr & (align - 1)) != 0) {

shared/runtime/pyexec.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#include "genhdr/mpversion.h"
4545

4646
pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
47-
int pyexec_system_exit = 0;
4847

4948
#if MICROPY_REPL_INFO
5049
static bool repl_display_debugging_info = 0;
@@ -74,9 +73,6 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input
7473
MICROPY_BOARD_BEFORE_PYTHON_EXEC(input_kind, exec_flags);
7574
#endif
7675

77-
// by default a SystemExit exception returns 0
78-
pyexec_system_exit = 0;
79-
8076
nlr_buf_t nlr;
8177
nlr.ret_val = NULL;
8278
if (nlr_push(&nlr) == 0) {
@@ -146,7 +142,7 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input
146142
// check for SystemExit
147143
if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t *)nlr.ret_val)->type), MP_OBJ_FROM_PTR(&mp_type_SystemExit))) {
148144
// at the moment, the value of SystemExit is unused
149-
ret = pyexec_system_exit;
145+
ret = PYEXEC_FORCED_EXIT;
150146
} else {
151147
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
152148
ret = 0;

shared/runtime/pyexec.h

-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ typedef enum {
3535

3636
extern pyexec_mode_kind_t pyexec_mode_kind;
3737

38-
// Set this to the value (eg PYEXEC_FORCED_EXIT) that will be propagated through
39-
// the pyexec functions if a SystemExit exception is raised by the running code.
40-
// It will reset to 0 at the start of each execution (eg each REPL entry).
41-
extern int pyexec_system_exit;
42-
4338
#define PYEXEC_FORCED_EXIT (0x100)
4439

4540
int pyexec_raw_repl(void);

0 commit comments

Comments
 (0)