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 /minimal | |
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 'minimal')
-rw-r--r-- | minimal/main.c | 26 |
1 files changed, 4 insertions, 22 deletions
diff --git a/minimal/main.c b/minimal/main.c index d25577d62d..c6c8080ade 100644 --- a/minimal/main.c +++ b/minimal/main.c @@ -3,7 +3,6 @@ #include <string.h> #include "py/nlr.h" -#include "py/parsehelper.h" #include "py/compile.h" #include "py/runtime.h" #include "py/repl.h" @@ -15,32 +14,15 @@ void do_str(const char *src) { mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0); if (lex == NULL) { - return; - } - - mp_parse_error_kind_t parse_error_kind; - mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT, &parse_error_kind); - - if (pn == MP_PARSE_NODE_NULL) { - // parse error - mp_parse_show_exception(lex, parse_error_kind); - mp_lexer_free(lex); - return; - } - - // parse okay - qstr source_name = lex->source_name; - mp_lexer_free(lex); - mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true); - - if (mp_obj_is_exception_instance(module_fun)) { - // compile error - mp_obj_print_exception(printf_wrapper, NULL, module_fun); + printf("MemoryError: lexer could not allocate memory\n"); return; } nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { + qstr source_name = lex->source_name; + mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT); + mp_obj_t module_fun = mp_compile(pn, source_name, MP_EMIT_OPT_NONE, true); mp_call_function_0(module_fun); nlr_pop(); } else { |