Skip to content

Commit e06ac9c

Browse files
pi-anldpgeorge
authored andcommitted
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>
1 parent e067d96 commit e06ac9c

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

ports/unix/main.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ static int handle_uncaught_exception(mp_obj_base_t *exc) {
111111
}
112112

113113
#define LEX_SRC_STR (1)
114-
#define LEX_SRC_VSTR (2)
115-
#define LEX_SRC_FILENAME (3)
116114
#define LEX_SRC_STDIN (4)
117115

118116
// 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
128126
if (source_kind == LEX_SRC_STR) {
129127
const char *line = source;
130128
lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line, strlen(line), false);
131-
} else if (source_kind == LEX_SRC_VSTR) {
132-
const vstr_t *vstr = source;
133-
lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr->buf, vstr->len, false);
134-
} else if (source_kind == LEX_SRC_FILENAME) {
135-
const char *filename = (const char *)source;
136-
lex = mp_lexer_new_from_file(qstr_from_str(filename));
137129
} else { // LEX_SRC_STDIN
138130
lex = mp_lexer_new_from_fd(MP_QSTR__lt_stdin_gt_, 0, false);
139131
}
@@ -251,12 +243,28 @@ static int do_repl(void) {
251243
#endif
252244
}
253245

246+
static inline int convert_pyexec_result(int ret) {
247+
// pyexec returns 1 for success, 0 for exception, PYEXEC_FORCED_EXIT for SystemExit
248+
// Convert to unix port's expected codes: 0 for success, 1 for exception, FORCED_EXIT|val for SystemExit
249+
if (ret == 1) {
250+
return 0; // success
251+
} else if (ret & PYEXEC_FORCED_EXIT) {
252+
return ret; // SystemExit with exit value in lower 8 bits
253+
} else {
254+
return 1; // exception
255+
}
256+
}
257+
254258
static int do_file(const char *file) {
255-
return execute_from_lexer(LEX_SRC_FILENAME, file, MP_PARSE_FILE_INPUT, false);
259+
return convert_pyexec_result(pyexec_file(file));
256260
}
257261

258262
static int do_str(const char *str) {
259-
return execute_from_lexer(LEX_SRC_STR, str, MP_PARSE_FILE_INPUT, false);
263+
vstr_t vstr;
264+
vstr.buf = (char *)str;
265+
vstr.len = strlen(str);
266+
int ret = pyexec_vstr(&vstr, true);
267+
return convert_pyexec_result(ret);
260268
}
261269

262270
static void print_help(char **argv) {

0 commit comments

Comments
 (0)