Skip to content

Commit 938d7f2

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 the new return value semantics where pyexec returns 0 for success, 1 for exception, or the actual exit code for SystemExit. Signed-off-by: Andrew Leech <[email protected]>
1 parent 66c748c commit 938d7f2

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

ports/unix/main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ 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 0 for success, 1 for exception, or actual exit value for SystemExit
242+
// Unix port expects: 0 for success, non-zero for error/exit
243+
return ret;
244+
#else
239245
// pyexec returns 1 for success, 0 for exception, PYEXEC_FORCED_EXIT for SystemExit
240246
// Convert to unix port's expected codes: 0 for success, 1 for exception, FORCED_EXIT|val for SystemExit
241247
if (ret == 1) {
@@ -245,6 +251,7 @@ static inline int convert_pyexec_result(int ret) {
245251
} else {
246252
return 1; // exception
247253
}
254+
#endif
248255
}
249256

250257
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)