diff options
author | Damien George <damien.p.george@gmail.com> | 2015-02-07 18:33:58 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-02-07 18:33:58 +0000 |
commit | 0bfc7638baa4c5a4a2351364ab770a188dcab302 (patch) | |
tree | 0127fcea13a875d37dd9cfa07dc6921bf387c578 /stmhal/pyexec.c | |
parent | e1e359ff59d6bbf09441cc1f3965be63f1046182 (diff) | |
download | micropython-0bfc7638baa4c5a4a2351364ab770a188dcab302.tar.gz micropython-0bfc7638baa4c5a4a2351364ab770a188dcab302.zip |
py: Protect mp_parse and mp_compile with nlr push/pop block.
To enable parsing constants more efficiently, mp_parse should be allowed
to raise an exception, and mp_compile can already raise a MemoryError.
So these functions need to be protected by an nlr push/pop block.
This patch adds that feature in all places. This allows to simplify how
mp_parse and mp_compile are called: they now raise an exception if they
have an error and so explicit checking is not needed anymore.
Diffstat (limited to 'stmhal/pyexec.c')
-rw-r--r-- | stmhal/pyexec.c | 39 |
1 files changed, 8 insertions, 31 deletions
diff --git a/stmhal/pyexec.c b/stmhal/pyexec.c index eaad6b5d72..a59df74d6d 100644 --- a/stmhal/pyexec.c +++ b/stmhal/pyexec.c @@ -29,7 +29,6 @@ #include <stdint.h> #include "py/nlr.h" -#include "py/parsehelper.h" #include "py/compile.h" #include "py/runtime.h" #include "py/repl.h" @@ -57,39 +56,18 @@ STATIC bool repl_display_debugging_info = 0; // EXEC_FLAG_IS_REPL is used for REPL inputs (flag passed on to mp_compile) STATIC int parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, int exec_flags) { int ret = 0; + uint32_t start = 0; - mp_parse_error_kind_t parse_error_kind; - mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind); - qstr source_name = lex->source_name; - - // check for parse error - if (pn == MP_PARSE_NODE_NULL) { - if (exec_flags & EXEC_FLAG_PRINT_EOF) { - stdout_tx_strn("\x04", 1); - } - mp_parse_show_exception(lex, parse_error_kind); - mp_lexer_free(lex); - goto finish; - } - - mp_lexer_free(lex); - - mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, exec_flags & EXEC_FLAG_IS_REPL); - - // check for compile error - if (mp_obj_is_exception_instance(module_fun)) { - if (exec_flags & EXEC_FLAG_PRINT_EOF) { - stdout_tx_strn("\x04", 1); - } - mp_obj_print_exception(printf_wrapper, NULL, module_fun); - goto finish; - } - - // execute code nlr_buf_t nlr; - uint32_t start = HAL_GetTick(); if (nlr_push(&nlr) == 0) { + // parse and compile the script + qstr source_name = lex->source_name; + mp_parse_node_t pn = mp_parse(lex, input_kind); + mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, exec_flags & EXEC_FLAG_IS_REPL); + + // execute code mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us + start = HAL_GetTick(); mp_call_function_0(module_fun); mp_hal_set_interrupt_char(-1); // disable interrupt nlr_pop(); @@ -131,7 +109,6 @@ STATIC int parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_ki gc_dump_info(); } -finish: if (exec_flags & EXEC_FLAG_PRINT_EOF) { stdout_tx_strn("\x04", 1); } |