mirror of
https://github.com/micropython/micropython.git
synced 2026-01-06 12:10:13 +01:00
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>
This commit is contained in:
committed by
Damien George
parent
e067d96c8b
commit
e06ac9ce08
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user