Skip to content

Commit 1e46c4c

Browse files
committed
unix/main: Replace execute_from_lexer with pyexec_vstr in do_str.
Consolidates 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 <[email protected]>
1 parent 99f5b95 commit 1e46c4c

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

ports/unix/main.c

Lines changed: 11 additions & 12 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
}
@@ -245,9 +237,7 @@ static int do_repl(void) {
245237
return ret;
246238
}
247239

248-
249-
static int do_file(const char *file) {
250-
int ret = pyexec_file(file);
240+
static inline int convert_pyexec_result(int ret) {
251241
// pyexec returns 1 for success, 0 for exception, PYEXEC_FORCED_EXIT for SystemExit
252242
// Convert to unix port's expected codes: 0 for success, 1 for exception, FORCED_EXIT|val for SystemExit
253243
if (ret == 1) {
@@ -259,8 +249,17 @@ static int do_file(const char *file) {
259249
}
260250
}
261251

252+
static int do_file(const char *file) {
253+
return convert_pyexec_result(pyexec_file(file));
254+
}
255+
262256
static int do_str(const char *str) {
263-
return execute_from_lexer(LEX_SRC_STR, str, MP_PARSE_FILE_INPUT, false);
257+
vstr_t vstr;
258+
vstr_init(&vstr, strlen(str));
259+
vstr_add_strn(&vstr, str, strlen(str));
260+
int ret = pyexec_vstr(&vstr, false);
261+
vstr_clear(&vstr);
262+
return convert_pyexec_result(ret);
264263
}
265264

266265
static void print_help(char **argv) {

0 commit comments

Comments
 (0)