Skip to content

Commit 56b2383

Browse files
committed
lib/utils/pyexec: Refactor to put lexer constructors all in one place.
The lexer can now raise an exception on construction so it must go within an nlr handler block.
1 parent 1831034 commit 56b2383

1 file changed

Lines changed: 25 additions & 42 deletions

File tree

lib/utils/pyexec.c

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ STATIC bool repl_display_debugging_info = 0;
5252
#define EXEC_FLAG_ALLOW_DEBUGGING (2)
5353
#define EXEC_FLAG_IS_REPL (4)
5454
#define EXEC_FLAG_SOURCE_IS_RAW_CODE (8)
55+
#define EXEC_FLAG_SOURCE_IS_VSTR (16)
56+
#define EXEC_FLAG_SOURCE_IS_FILENAME (32)
5557

5658
// parses, compiles and executes the code in the lexer
5759
// frees the lexer before returning
5860
// EXEC_FLAG_PRINT_EOF prints 2 EOF chars: 1 after normal output, 1 after exception output
5961
// EXEC_FLAG_ALLOW_DEBUGGING allows debugging info to be printed after executing the code
6062
// EXEC_FLAG_IS_REPL is used for REPL inputs (flag passed on to mp_compile)
61-
STATIC int parse_compile_execute(void *source, mp_parse_input_kind_t input_kind, int exec_flags) {
63+
STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input_kind, int exec_flags) {
6264
int ret = 0;
6365
uint32_t start = 0;
6466

@@ -76,8 +78,16 @@ STATIC int parse_compile_execute(void *source, mp_parse_input_kind_t input_kind,
7678
#endif
7779
{
7880
#if MICROPY_ENABLE_COMPILER
81+
mp_lexer_t *lex;
82+
if (exec_flags & EXEC_FLAG_SOURCE_IS_VSTR) {
83+
const vstr_t *vstr = source;
84+
lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr->buf, vstr->len, 0);
85+
} else if (exec_flags & EXEC_FLAG_SOURCE_IS_FILENAME) {
86+
lex = mp_lexer_new_from_file(source);
87+
} else {
88+
lex = (mp_lexer_t*)source;
89+
}
7990
// source is a lexer, parse and compile the script
80-
mp_lexer_t *lex = source;
8191
qstr source_name = lex->source_name;
8292
mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
8393
module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, exec_flags & EXEC_FLAG_IS_REPL);
@@ -202,14 +212,9 @@ STATIC int pyexec_raw_repl_process_char(int c) {
202212
return PYEXEC_FORCED_EXIT;
203213
}
204214

205-
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, MP_STATE_VM(repl_line)->buf, MP_STATE_VM(repl_line)->len, 0);
206-
if (lex == NULL) {
207-
mp_hal_stdout_tx_str("\x04MemoryError\r\n\x04");
208-
} else {
209-
int ret = parse_compile_execute(lex, MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF);
210-
if (ret & PYEXEC_FORCED_EXIT) {
211-
return ret;
212-
}
215+
int ret = parse_compile_execute(MP_STATE_VM(repl_line), MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF | EXEC_FLAG_SOURCE_IS_VSTR);
216+
if (ret & PYEXEC_FORCED_EXIT) {
217+
return ret;
213218
}
214219

215220
reset:
@@ -285,14 +290,9 @@ STATIC int pyexec_friendly_repl_process_char(int c) {
285290
}
286291

287292
exec: ;
288-
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(MP_STATE_VM(repl_line)), vstr_len(MP_STATE_VM(repl_line)), 0);
289-
if (lex == NULL) {
290-
printf("MemoryError\n");
291-
} else {
292-
int ret = parse_compile_execute(lex, MP_PARSE_SINGLE_INPUT, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL);
293-
if (ret & PYEXEC_FORCED_EXIT) {
294-
return ret;
295-
}
293+
int ret = parse_compile_execute(MP_STATE_VM(repl_line), MP_PARSE_SINGLE_INPUT, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL | EXEC_FLAG_SOURCE_IS_VSTR);
294+
if (ret & PYEXEC_FORCED_EXIT) {
295+
return ret;
296296
}
297297

298298
input_restart:
@@ -361,14 +361,9 @@ int pyexec_raw_repl(void) {
361361
return PYEXEC_FORCED_EXIT;
362362
}
363363

364-
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line.buf, line.len, 0);
365-
if (lex == NULL) {
366-
printf("\x04MemoryError\n\x04");
367-
} else {
368-
int ret = parse_compile_execute(lex, MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF);
369-
if (ret & PYEXEC_FORCED_EXIT) {
370-
return ret;
371-
}
364+
int ret = parse_compile_execute(&line, MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF | EXEC_FLAG_SOURCE_IS_VSTR);
365+
if (ret & PYEXEC_FORCED_EXIT) {
366+
return ret;
372367
}
373368
}
374369
}
@@ -489,14 +484,9 @@ int pyexec_friendly_repl(void) {
489484
}
490485
}
491486

492-
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(&line), vstr_len(&line), 0);
493-
if (lex == NULL) {
494-
printf("MemoryError\n");
495-
} else {
496-
ret = parse_compile_execute(lex, parse_input_kind, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL);
497-
if (ret & PYEXEC_FORCED_EXIT) {
498-
return ret;
499-
}
487+
ret = parse_compile_execute(&line, parse_input_kind, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL | EXEC_FLAG_SOURCE_IS_VSTR);
488+
if (ret & PYEXEC_FORCED_EXIT) {
489+
return ret;
500490
}
501491
}
502492
}
@@ -505,14 +495,7 @@ int pyexec_friendly_repl(void) {
505495
#endif // MICROPY_ENABLE_COMPILER
506496

507497
int pyexec_file(const char *filename) {
508-
mp_lexer_t *lex = mp_lexer_new_from_file(filename);
509-
510-
if (lex == NULL) {
511-
printf("could not open file '%s' for reading\n", filename);
512-
return false;
513-
}
514-
515-
return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, 0);
498+
return parse_compile_execute(filename, MP_PARSE_FILE_INPUT, 0);
516499
}
517500

518501
#if MICROPY_MODULE_FROZEN

0 commit comments

Comments
 (0)