summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Leech <andrew.leech@planetinnovation.com.au>2025-07-22 15:28:28 +1000
committerDamien George <damien@micropython.org>2025-11-22 00:06:59 +1100
commite06ac9ce089c5eda9cbd9ec035cdf56fe75be0ab (patch)
tree6f91fed5fb043a4a4c44a669cb4ba49e0a39c16f
parente067d96c8b71b3fe258583fd6cddb1094cb4efe5 (diff)
unix/main: Replace execute_from_lexer with pyexec in do_file and do_str.
Consolidates file and string execution to use the standard pyexec interface for consistency with other ports. Simplify execute_from_lexer for remaining usage: Remove unused LEX_SRC_VSTR and LEX_SRC_FILENAME cases, keeping only LEX_SRC_STR for REPL and LEX_SRC_STDIN for stdin execution. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
-rw-r--r--ports/unix/main.c28
1 files changed, 18 insertions, 10 deletions
diff --git a/ports/unix/main.c b/ports/unix/main.c
index 934bd3252..dec3f90de 100644
--- a/ports/unix/main.c
+++ b/ports/unix/main.c
@@ -111,8 +111,6 @@ static int handle_uncaught_exception(mp_obj_base_t *exc) {
}
#define LEX_SRC_STR (1)
-#define LEX_SRC_VSTR (2)
-#define LEX_SRC_FILENAME (3)
#define LEX_SRC_STDIN (4)
// Returns standard error codes: 0 for success, 1 for all other errors,
@@ -128,12 +126,6 @@ static int execute_from_lexer(int source_kind, const void *source, mp_parse_inpu
if (source_kind == LEX_SRC_STR) {
const char *line = source;
lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line, strlen(line), false);
- } else if (source_kind == LEX_SRC_VSTR) {
- const vstr_t *vstr = source;
- lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr->buf, vstr->len, false);
- } else if (source_kind == LEX_SRC_FILENAME) {
- const char *filename = (const char *)source;
- lex = mp_lexer_new_from_file(qstr_from_str(filename));
} else { // LEX_SRC_STDIN
lex = mp_lexer_new_from_fd(MP_QSTR__lt_stdin_gt_, 0, false);
}
@@ -251,12 +243,28 @@ static int do_repl(void) {
#endif
}
+static inline int convert_pyexec_result(int ret) {
+ // pyexec returns 1 for success, 0 for exception, PYEXEC_FORCED_EXIT for SystemExit
+ // Convert to unix port's expected codes: 0 for success, 1 for exception, FORCED_EXIT|val for SystemExit
+ if (ret == 1) {
+ return 0; // success
+ } else if (ret & PYEXEC_FORCED_EXIT) {
+ return ret; // SystemExit with exit value in lower 8 bits
+ } else {
+ return 1; // exception
+ }
+}
+
static int do_file(const char *file) {
- return execute_from_lexer(LEX_SRC_FILENAME, file, MP_PARSE_FILE_INPUT, false);
+ return convert_pyexec_result(pyexec_file(file));
}
static int do_str(const char *str) {
- return execute_from_lexer(LEX_SRC_STR, str, MP_PARSE_FILE_INPUT, false);
+ vstr_t vstr;
+ vstr.buf = (char *)str;
+ vstr.len = strlen(str);
+ int ret = pyexec_vstr(&vstr, true);
+ return convert_pyexec_result(ret);
}
static void print_help(char **argv) {