Skip to content

Commit 4c1d286

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 0e2f79a commit 4c1d286

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

ports/unix/main.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ static int handle_uncaught_exception(mp_obj_base_t *exc) {
110110
}
111111

112112
#define LEX_SRC_STR (1)
113-
#define LEX_SRC_VSTR (2)
114-
#define LEX_SRC_FILENAME (3)
115113
#define LEX_SRC_STDIN (4)
116114

117115
// Returns standard error codes: 0 for success, 1 for all other errors,
@@ -127,12 +125,6 @@ static int execute_from_lexer(int source_kind, const void *source, mp_parse_inpu
127125
if (source_kind == LEX_SRC_STR) {
128126
const char *line = source;
129127
lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line, strlen(line), false);
130-
} else if (source_kind == LEX_SRC_VSTR) {
131-
const vstr_t *vstr = source;
132-
lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr->buf, vstr->len, false);
133-
} else if (source_kind == LEX_SRC_FILENAME) {
134-
const char *filename = (const char *)source;
135-
lex = mp_lexer_new_from_file(qstr_from_str(filename));
136128
} else { // LEX_SRC_STDIN
137129
lex = mp_lexer_new_from_fd(MP_QSTR__lt_stdin_gt_, 0, false);
138130
}
@@ -259,7 +251,18 @@ static int do_file(const char *file) {
259251
}
260252

261253
static int do_str(const char *str) {
262-
return execute_from_lexer(LEX_SRC_STR, str, MP_PARSE_FILE_INPUT, false);
254+
vstr_t vstr;
255+
vstr_init(&vstr, strlen(str));
256+
vstr_add_strn(&vstr, str, strlen(str));
257+
int ret = pyexec_vstr(&vstr, false);
258+
vstr_clear(&vstr);
259+
if (ret == 1) {
260+
return 0; // success
261+
} else if (ret & PYEXEC_FORCED_EXIT) {
262+
return ret; // SystemExit with exit value in lower 8 bits
263+
} else {
264+
return 1; // exception
265+
}
263266
}
264267

265268
static void print_help(char **argv) {

0 commit comments

Comments
 (0)