diff options
| author | Damien George | 2014-12-19 13:36:17 +0000 |
|---|---|---|
| committer | Damien George | 2014-12-19 13:36:17 +0000 |
| commit | 2a3e2b903394376fcf5722904453febd7472ee25 (patch) | |
| tree | 49da1c7af3fe1726a550c6a3ca189acdb5b838d6 /py | |
| 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')
| -rw-r--r-- | py/builtin.h | 1 | ||||
| -rw-r--r-- | py/builtinevex.c | 17 | ||||
| -rw-r--r-- | py/modbuiltins.c | 3 | ||||
| -rw-r--r-- | py/mpconfig.h | 5 | ||||
| -rw-r--r-- | py/qstrdefs.h | 3 |
5 files changed, 28 insertions, 1 deletions
diff --git a/py/builtin.h b/py/builtin.h index 115d05991..70f163379 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -41,6 +41,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_dir_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_divmod_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_eval_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_exec_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_execfile_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_getattr_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_globals_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hasattr_obj); 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 diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 181b08eb1..c0b3b8a86 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -627,6 +627,9 @@ STATIC const mp_map_elem_t mp_module_builtins_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_divmod), (mp_obj_t)&mp_builtin_divmod_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_eval), (mp_obj_t)&mp_builtin_eval_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_exec), (mp_obj_t)&mp_builtin_exec_obj }, +#if MICROPY_PY_BUILTINS_EXECFILE + { MP_OBJ_NEW_QSTR(MP_QSTR_execfile), (mp_obj_t)&mp_builtin_execfile_obj }, +#endif { MP_OBJ_NEW_QSTR(MP_QSTR_getattr), (mp_obj_t)&mp_builtin_getattr_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_globals), (mp_obj_t)&mp_builtin_globals_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_hasattr), (mp_obj_t)&mp_builtin_hasattr_obj }, diff --git a/py/mpconfig.h b/py/mpconfig.h index 880e46987..170033240 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -340,6 +340,11 @@ typedef double mp_float_t; #define MICROPY_PY_BUILTINS_COMPILE (0) #endif +// Whether to support the Python 2 execfile function +#ifndef MICROPY_PY_BUILTINS_EXECFILE +#define MICROPY_PY_BUILTINS_EXECFILE (0) +#endif + // Whether to set __file__ for imported modules #ifndef MICROPY_PY___FILE__ #define MICROPY_PY___FILE__ (1) diff --git a/py/qstrdefs.h b/py/qstrdefs.h index f183c256f..e91d36070 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -167,6 +167,9 @@ Q(divmod) Q(enumerate) Q(eval) Q(exec) +#if MICROPY_PY_BUILTINS_EXECFILE +Q(execfile) +#endif Q(filter) #if MICROPY_PY_BUILTINS_FLOAT Q(float) |
