summaryrefslogtreecommitdiff
path: root/shared/runtime/pyexec.c
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2024-07-18 13:20:31 +1000
committerDamien George <damien@micropython.org>2024-07-20 12:13:14 +1000
commit69c25ea8653566ec97690b5121bd10b753c89426 (patch)
treef7994c218eebafb78ecd2e0e05a8c7e341523068 /shared/runtime/pyexec.c
parenta734ee9057a760b0316eca110d71db1524142fec (diff)
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 <damien@micropython.org>
Diffstat (limited to 'shared/runtime/pyexec.c')
-rw-r--r--shared/runtime/pyexec.c6
1 files changed, 1 insertions, 5 deletions
diff --git a/shared/runtime/pyexec.c b/shared/runtime/pyexec.c
index a305e6a5d..9dc4446ed 100644
--- a/shared/runtime/pyexec.c
+++ b/shared/runtime/pyexec.c
@@ -44,7 +44,6 @@
#include "genhdr/mpversion.h"
pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
-int pyexec_system_exit = 0;
#if MICROPY_REPL_INFO
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
MICROPY_BOARD_BEFORE_PYTHON_EXEC(input_kind, exec_flags);
#endif
- // by default a SystemExit exception returns 0
- pyexec_system_exit = 0;
-
nlr_buf_t nlr;
nlr.ret_val = NULL;
if (nlr_push(&nlr) == 0) {
@@ -146,7 +142,7 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input
// check for SystemExit
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))) {
// at the moment, the value of SystemExit is unused
- ret = pyexec_system_exit;
+ ret = PYEXEC_FORCED_EXIT;
} else {
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
ret = 0;