diff options
| author | Damien George <damien.p.george@gmail.com> | 2014-12-19 13:36:17 +0000 |
|---|---|---|
| committer | Damien George <damien.p.george@gmail.com> | 2014-12-19 13:36:17 +0000 |
| commit | 2a3e2b903394376fcf5722904453febd7472ee25 (patch) | |
| tree | 49da1c7af3fe1726a550c6a3ca189acdb5b838d6 /py/builtinevex.c | |
| parent | 8427c5b76c9326ab3bc060fb729c1f0dbe17facd (diff) | |
py: Add execfile function (from Python 2); enable in stmhal port.
Adds just 60 bytes to stmhal binary. Addresses issue #362.
Diffstat (limited to 'py/builtinevex.c')
| -rw-r--r-- | py/builtinevex.c | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/py/builtinevex.c b/py/builtinevex.c index 73f254c5c..bb15fd4d0 100644 --- a/py/builtinevex.c +++ b/py/builtinevex.c @@ -137,7 +137,14 @@ STATIC mp_obj_t eval_exec_helper(mp_uint_t n_args, const mp_obj_t *args, mp_pars const char *str = mp_obj_str_get_data(args[0], &str_len); // create the lexer - mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0); + // MP_PARSE_SINGLE_INPUT is used to indicate a file input + mp_lexer_t *lex; + if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) { + lex = mp_lexer_new_from_file(str); + parse_input_kind = MP_PARSE_FILE_INPUT; + } else { + lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0); + } return mp_parse_compile_execute(lex, parse_input_kind, globals, locals); } @@ -151,3 +158,11 @@ STATIC mp_obj_t mp_builtin_exec(mp_uint_t n_args, const mp_obj_t *args) { return eval_exec_helper(n_args, args, MP_PARSE_FILE_INPUT); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec); + +#if MICROPY_PY_BUILTINS_EXECFILE +STATIC mp_obj_t mp_builtin_execfile(mp_uint_t n_args, const mp_obj_t *args) { + // MP_PARSE_SINGLE_INPUT is used to indicate a file input + return eval_exec_helper(n_args, args, MP_PARSE_SINGLE_INPUT); +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_execfile_obj, 1, 3, mp_builtin_execfile); +#endif |
