py: Add execfile function (from Python 2); enable in stmhal port.
Adds just 60 bytes to stmhal binary. Addresses issue #362.
This commit is contained in:
parent
8427c5b76c
commit
2a3e2b9033
@ -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_divmod_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_eval_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_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_getattr_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_globals_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_globals_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hasattr_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hasattr_obj);
|
||||||
|
@ -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);
|
const char *str = mp_obj_str_get_data(args[0], &str_len);
|
||||||
|
|
||||||
// create the lexer
|
// 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);
|
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);
|
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);
|
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
|
||||||
|
@ -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_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_eval), (mp_obj_t)&mp_builtin_eval_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_exec), (mp_obj_t)&mp_builtin_exec_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_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_globals), (mp_obj_t)&mp_builtin_globals_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_hasattr), (mp_obj_t)&mp_builtin_hasattr_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_hasattr), (mp_obj_t)&mp_builtin_hasattr_obj },
|
||||||
|
@ -340,6 +340,11 @@ typedef double mp_float_t;
|
|||||||
#define MICROPY_PY_BUILTINS_COMPILE (0)
|
#define MICROPY_PY_BUILTINS_COMPILE (0)
|
||||||
#endif
|
#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
|
// Whether to set __file__ for imported modules
|
||||||
#ifndef MICROPY_PY___FILE__
|
#ifndef MICROPY_PY___FILE__
|
||||||
#define MICROPY_PY___FILE__ (1)
|
#define MICROPY_PY___FILE__ (1)
|
||||||
|
@ -167,6 +167,9 @@ Q(divmod)
|
|||||||
Q(enumerate)
|
Q(enumerate)
|
||||||
Q(eval)
|
Q(eval)
|
||||||
Q(exec)
|
Q(exec)
|
||||||
|
#if MICROPY_PY_BUILTINS_EXECFILE
|
||||||
|
Q(execfile)
|
||||||
|
#endif
|
||||||
Q(filter)
|
Q(filter)
|
||||||
#if MICROPY_PY_BUILTINS_FLOAT
|
#if MICROPY_PY_BUILTINS_FLOAT
|
||||||
Q(float)
|
Q(float)
|
||||||
|
@ -54,6 +54,7 @@
|
|||||||
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
|
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
|
||||||
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
|
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
|
||||||
#define MICROPY_PY_BUILTINS_FROZENSET (1)
|
#define MICROPY_PY_BUILTINS_FROZENSET (1)
|
||||||
|
#define MICROPY_PY_BUILTINS_EXECFILE (1)
|
||||||
#define MICROPY_PY_SYS_EXIT (1)
|
#define MICROPY_PY_SYS_EXIT (1)
|
||||||
#define MICROPY_PY_SYS_STDFILES (1)
|
#define MICROPY_PY_SYS_STDFILES (1)
|
||||||
#define MICROPY_PY_CMATH (1)
|
#define MICROPY_PY_CMATH (1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user