shared/runtime: Set exit code according to the SystemExit exception.
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_c2_c6) (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (bigendian) (push) Has been cancelled
qemu port / build_and_test_arm (sabrelite) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_hardfp) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_softfp) (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
qemu port / build_and_test_rv64 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / longlong (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / gil_enabled (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / repr_b (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
unix port / sanitize_address (push) Has been cancelled
unix port / sanitize_undefined (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled

Add abort setup code `nlr_set_abort` to the standard runtime executor.
This makes the standard runtime respond to abort signal without any further
modifications.

- When aborted, the program exits with 137 exit code (configurable, same as
  posix sig abort), to differentiate from a normal shutdown.

- When exited by exception/crash, the program will exit with exit code 1
  (configurable).

- When exited by exception KeyboardInterrupt, the program will exit with
  exit code 130 (configurable, same as posix sig int).

- When exited with a exit code (from Python environment), this code is
  propagated.  When a different object is passed, exit code is set to 1 and
  the value printed, to be consistent with Python docs:
  https://python.readthedocs.io/en/latest/library/exceptions.html#SystemExit

Signed-off-by: John Smith <jsmith@jsmith.cz>
This commit is contained in:
John Smith
2024-08-29 10:29:35 +02:00
committed by Damien George
parent 4f2f520dc2
commit 1e5da2642c
3 changed files with 57 additions and 6 deletions

View File

@@ -1180,6 +1180,16 @@ typedef time_t mp_timestamp_t;
#define MICROPY_ENABLE_VM_ABORT (0)
#endif
// Whether to handle abort behavior in pyexec code
#ifndef MICROPY_PYEXEC_ENABLE_VM_ABORT
#define MICROPY_PYEXEC_ENABLE_VM_ABORT (0)
#endif
// Whether to set exit codes according to the exit reason (keyboard interrupt, crash, normal exit, ...)
#ifndef MICROPY_PYEXEC_ENABLE_EXIT_CODE_HANDLING
#define MICROPY_PYEXEC_ENABLE_EXIT_CODE_HANDLING (0)
#endif
// Support for internal scheduler
#ifndef MICROPY_ENABLE_SCHEDULER
#define MICROPY_ENABLE_SCHEDULER (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)

View File

@@ -72,6 +72,10 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input
nlr_buf_t nlr;
nlr.ret_val = NULL;
if (nlr_push(&nlr) == 0) {
#if MICROPY_PYEXEC_ENABLE_VM_ABORT
nlr_set_abort(&nlr);
#endif
mp_obj_t module_fun;
#if MICROPY_MODULE_FROZEN_MPY
if (exec_flags & EXEC_FLAG_SOURCE_IS_RAW_CODE) {
@@ -116,7 +120,7 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input
mp_hal_set_interrupt_char(-1); // disable interrupt
mp_handle_pending(true); // handle any pending exceptions (and any callbacks)
nlr_pop();
ret = 1;
ret = PYEXEC_NORMAL_EXIT;
if (exec_flags & EXEC_FLAG_PRINT_EOF) {
mp_hal_stdout_tx_strn("\x04", 1);
}
@@ -135,15 +139,41 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input
mp_hal_stdout_tx_strn("\x04", 1);
}
// 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
#if MICROPY_PYEXEC_ENABLE_VM_ABORT
if (nlr.ret_val == NULL) { // abort
ret = PYEXEC_ABORT;
} else
#endif
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))) { // system exit
#if MICROPY_PYEXEC_ENABLE_EXIT_CODE_HANDLING
mp_obj_t val = mp_obj_exception_get_value(MP_OBJ_FROM_PTR(nlr.ret_val));
if (val != mp_const_none) {
if (mp_obj_is_int(val)) {
ret = (int)mp_obj_int_get_truncated(val);
} else {
mp_obj_print_helper(MICROPY_ERROR_PRINTER, val, PRINT_STR);
mp_print_str(MICROPY_ERROR_PRINTER, "\n");
ret = PYEXEC_UNHANDLED_EXCEPTION;
}
} else {
ret = PYEXEC_NORMAL_EXIT;
}
#else
ret = PYEXEC_FORCED_EXIT;
} else {
#endif
} else { // other exception
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
ret = 0;
ret = PYEXEC_UNHANDLED_EXCEPTION;
#if MICROPY_PYEXEC_ENABLE_EXIT_CODE_HANDLING
if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t *)nlr.ret_val)->type), MP_OBJ_FROM_PTR(&mp_type_KeyboardInterrupt))) { // keyboard interrupt
ret = PYEXEC_KEYBOARD_INTERRUPT;
}
#endif
}
}
#if MICROPY_PYEXEC_ENABLE_VM_ABORT
nlr_set_abort(NULL);
#endif
#if MICROPY_REPL_INFO
// display debugging info if wanted

View File

@@ -37,6 +37,17 @@ extern pyexec_mode_kind_t pyexec_mode_kind;
#define PYEXEC_FORCED_EXIT (0x100)
#if MICROPY_PYEXEC_ENABLE_EXIT_CODE_HANDLING
#define PYEXEC_NORMAL_EXIT (0)
#define PYEXEC_UNHANDLED_EXCEPTION (1)
#define PYEXEC_KEYBOARD_INTERRUPT (128 + 2) // same as SIG INT exit code
#define PYEXEC_ABORT (128 + 9) // same as SIG KILL exit code
#else
#define PYEXEC_NORMAL_EXIT (1)
#define PYEXEC_UNHANDLED_EXCEPTION (0)
#define PYEXEC_ABORT PYEXEC_FORCED_EXIT
#endif
int pyexec_raw_repl(void);
int pyexec_friendly_repl(void);
int pyexec_file(const char *filename);