Skip to content

Commit 412edc4

Browse files
committed
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 <[email protected]>
1 parent 66c748c commit 412edc4

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

ports/unix/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,17 @@ static int do_repl(void) {
236236
}
237237

238238
static inline int convert_pyexec_result(int ret) {
239+
#if MICROPY_PYEXEC_ENABLE_EXIT_CODE_HANDLING
240+
// With exit code handling enabled:
241+
// pyexec returns exit code with PYEXEC_FORCED_EXIT flag set for SystemExit
242+
// Unix port expects: 0 for success, non-zero for error/exit
243+
if (ret & PYEXEC_FORCED_EXIT) {
244+
// SystemExit: extract exit code from lower bits
245+
return ret & 0xFF;
246+
}
247+
// Normal execution or exception: return as-is (0 for success, 1 for exception)
248+
return ret;
249+
#else
239250
// pyexec returns 1 for success, 0 for exception, PYEXEC_FORCED_EXIT for SystemExit
240251
// Convert to unix port's expected codes: 0 for success, 1 for exception, FORCED_EXIT|val for SystemExit
241252
if (ret == 1) {
@@ -245,6 +256,7 @@ static inline int convert_pyexec_result(int ret) {
245256
} else {
246257
return 1; // exception
247258
}
259+
#endif
248260
}
249261

250262
static int do_file(const char *file) {

ports/unix/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ typedef long mp_off_t;
154154
// Enable support for compile-only mode.
155155
#define MICROPY_PYEXEC_COMPILE_ONLY (1)
156156

157+
// Enable handling of sys.exit() exit codes.
158+
#define MICROPY_PYEXEC_ENABLE_EXIT_CODE_HANDLING (1)
159+
157160
#define MICROPY_PY_SOCKET_LISTEN_BACKLOG_DEFAULT (SOMAXCONN < 128 ? SOMAXCONN : 128)
158161

159162
// Bare-metal ports don't have stderr. Printing debug to stderr may give tests

0 commit comments

Comments
 (0)