unix: Enable exit code handling for sys.exit().

Enable `MICROPY_PYEXEC_ENABLE_EXIT_CODE_HANDLING` to propagate `sys.exit()`
exit codes properly. Update `convert_pyexec_result()` to handle return
values where pyexec returns the exit code with `PYEXEC_FORCED_EXIT` flag
set for `SystemExit`. Extract the exit code from the lower 8 bits when the
flag is set, otherwise return as-is (0 for success, 1 for exception).

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
This commit is contained in:
Andrew Leech
2025-11-05 16:35:31 +11:00
committed by Damien George
parent fd1ddc3f12
commit 5f815b8a2d
4 changed files with 19 additions and 0 deletions

View File

@@ -165,6 +165,7 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input
#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
// None is an exit value of 0; an int is its value; anything else is 1
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)) {