diff options
| author | Glenn Ruben Bakke <glennbakke@gmail.com> | 2017-03-16 22:50:59 +0100 |
|---|---|---|
| committer | Glenn Ruben Bakke <glennbakke@gmail.com> | 2017-03-16 22:50:59 +0100 |
| commit | 23163154b9ed5814d039faf7d69f8557df8b08ad (patch) | |
| tree | 6c9aa52b6287be47e317dd712aeec87263c5301d /lib | |
| parent | 327754a953a87cd0efb6c0749025189faa5a7111 (diff) | |
| parent | eeff0c352845649a3ae7b2e325361744d2db114b (diff) | |
Merge branch 'master' into nrf5_no_sdk
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/memzip/lexermemzip.c | 4 | ||||
| -rw-r--r-- | lib/utils/pyexec.c | 67 |
2 files changed, 28 insertions, 43 deletions
diff --git a/lib/memzip/lexermemzip.c b/lib/memzip/lexermemzip.c index 72fe6b1c6..6b26961bd 100644 --- a/lib/memzip/lexermemzip.c +++ b/lib/memzip/lexermemzip.c @@ -1,6 +1,8 @@ #include <stdlib.h> #include "py/lexer.h" +#include "py/runtime.h" +#include "py/mperrno.h" #include "memzip.h" mp_lexer_t *mp_lexer_new_from_file(const char *filename) @@ -9,7 +11,7 @@ mp_lexer_t *mp_lexer_new_from_file(const char *filename) size_t len; if (memzip_locate(filename, &data, &len) != MZ_OK) { - return NULL; + mp_raise_OSError(MP_ENOENT); } return mp_lexer_new_from_str_len(qstr_from_str(filename), (const char *)data, (mp_uint_t)len, 0); diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c index 0d20c1917..112cfe592 100644 --- a/lib/utils/pyexec.c +++ b/lib/utils/pyexec.c @@ -52,13 +52,15 @@ STATIC bool repl_display_debugging_info = 0; #define EXEC_FLAG_ALLOW_DEBUGGING (2) #define EXEC_FLAG_IS_REPL (4) #define EXEC_FLAG_SOURCE_IS_RAW_CODE (8) +#define EXEC_FLAG_SOURCE_IS_VSTR (16) +#define EXEC_FLAG_SOURCE_IS_FILENAME (32) // parses, compiles and executes the code in the lexer // frees the lexer before returning // EXEC_FLAG_PRINT_EOF prints 2 EOF chars: 1 after normal output, 1 after exception output // EXEC_FLAG_ALLOW_DEBUGGING allows debugging info to be printed after executing the code // EXEC_FLAG_IS_REPL is used for REPL inputs (flag passed on to mp_compile) -STATIC int parse_compile_execute(void *source, mp_parse_input_kind_t input_kind, int exec_flags) { +STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input_kind, int exec_flags) { int ret = 0; uint32_t start = 0; @@ -76,8 +78,16 @@ STATIC int parse_compile_execute(void *source, mp_parse_input_kind_t input_kind, #endif { #if MICROPY_ENABLE_COMPILER + mp_lexer_t *lex; + if (exec_flags & EXEC_FLAG_SOURCE_IS_VSTR) { + const vstr_t *vstr = source; + lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr->buf, vstr->len, 0); + } else if (exec_flags & EXEC_FLAG_SOURCE_IS_FILENAME) { + lex = mp_lexer_new_from_file(source); + } else { + lex = (mp_lexer_t*)source; + } // source is a lexer, parse and compile the script - mp_lexer_t *lex = source; qstr source_name = lex->source_name; mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); 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) { return PYEXEC_FORCED_EXIT; } - 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); - if (lex == NULL) { - mp_hal_stdout_tx_str("\x04MemoryError\r\n\x04"); - } else { - int ret = parse_compile_execute(lex, MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF); - if (ret & PYEXEC_FORCED_EXIT) { - return ret; - } + int ret = parse_compile_execute(MP_STATE_VM(repl_line), MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF | EXEC_FLAG_SOURCE_IS_VSTR); + if (ret & PYEXEC_FORCED_EXIT) { + return ret; } reset: @@ -285,14 +290,9 @@ STATIC int pyexec_friendly_repl_process_char(int c) { } exec: ; - 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); - if (lex == NULL) { - printf("MemoryError\n"); - } else { - int ret = parse_compile_execute(lex, MP_PARSE_SINGLE_INPUT, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL); - if (ret & PYEXEC_FORCED_EXIT) { - return ret; - } + 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); + if (ret & PYEXEC_FORCED_EXIT) { + return ret; } input_restart: @@ -361,14 +361,9 @@ raw_repl_reset: return PYEXEC_FORCED_EXIT; } - mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line.buf, line.len, 0); - if (lex == NULL) { - printf("\x04MemoryError\n\x04"); - } else { - int ret = parse_compile_execute(lex, MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF); - if (ret & PYEXEC_FORCED_EXIT) { - return ret; - } + int ret = parse_compile_execute(&line, MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF | EXEC_FLAG_SOURCE_IS_VSTR); + if (ret & PYEXEC_FORCED_EXIT) { + return ret; } } } @@ -489,14 +484,9 @@ friendly_repl_reset: } } - mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(&line), vstr_len(&line), 0); - if (lex == NULL) { - printf("MemoryError\n"); - } else { - ret = parse_compile_execute(lex, parse_input_kind, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL); - if (ret & PYEXEC_FORCED_EXIT) { - return ret; - } + ret = parse_compile_execute(&line, parse_input_kind, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL | EXEC_FLAG_SOURCE_IS_VSTR); + if (ret & PYEXEC_FORCED_EXIT) { + return ret; } } } @@ -505,14 +495,7 @@ friendly_repl_reset: #endif // MICROPY_ENABLE_COMPILER int pyexec_file(const char *filename) { - mp_lexer_t *lex = mp_lexer_new_from_file(filename); - - if (lex == NULL) { - printf("could not open file '%s' for reading\n", filename); - return false; - } - - return parse_compile_execute(lex, MP_PARSE_FILE_INPUT, 0); + return parse_compile_execute(filename, MP_PARSE_FILE_INPUT, EXEC_FLAG_SOURCE_IS_FILENAME); } #if MICROPY_MODULE_FROZEN |
