From 1e5da2642cbb0b4c808323173b7a703c3a3cf24f Mon Sep 17 00:00:00 2001 From: John Smith Date: Thu, 29 Aug 2024 10:29:35 +0200 Subject: shared/runtime: Set exit code according to the SystemExit exception. 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 --- shared/runtime/pyexec.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'shared/runtime/pyexec.h') diff --git a/shared/runtime/pyexec.h b/shared/runtime/pyexec.h index 95f448162..fb52c09b1 100644 --- a/shared/runtime/pyexec.h +++ b/shared/runtime/pyexec.h @@ -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); -- cgit v1.2.3