Skip to content

Commit 2a3e2b9

Browse files
committed
py: Add execfile function (from Python 2); enable in stmhal port.
Adds just 60 bytes to stmhal binary. Addresses issue adafruit#362.
1 parent 8427c5b commit 2a3e2b9

6 files changed

Lines changed: 29 additions & 1 deletion

File tree

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_dir_obj);
4141
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_divmod_obj);
4242
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_eval_obj);
4343
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_exec_obj);
44+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_execfile_obj);
4445
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_getattr_obj);
4546
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_globals_obj);
4647
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hasattr_obj);

py/builtinevex.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,14 @@ STATIC mp_obj_t eval_exec_helper(mp_uint_t n_args, const mp_obj_t *args, mp_pars
137137
const char *str = mp_obj_str_get_data(args[0], &str_len);
138138

139139
// create the lexer
140-
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0);
140+
// MP_PARSE_SINGLE_INPUT is used to indicate a file input
141+
mp_lexer_t *lex;
142+
if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) {
143+
lex = mp_lexer_new_from_file(str);
144+
parse_input_kind = MP_PARSE_FILE_INPUT;
145+
} else {
146+
lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0);
147+
}
141148

142149
return mp_parse_compile_execute(lex, parse_input_kind, globals, locals);
143150
}
@@ -151,3 +158,11 @@ STATIC mp_obj_t mp_builtin_exec(mp_uint_t n_args, const mp_obj_t *args) {
151158
return eval_exec_helper(n_args, args, MP_PARSE_FILE_INPUT);
152159
}
153160
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
161+
162+
#if MICROPY_PY_BUILTINS_EXECFILE
163+
STATIC mp_obj_t mp_builtin_execfile(mp_uint_t n_args, const mp_obj_t *args) {
164+
// MP_PARSE_SINGLE_INPUT is used to indicate a file input
165+
return eval_exec_helper(n_args, args, MP_PARSE_SINGLE_INPUT);
166+
}
167+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_execfile_obj, 1, 3, mp_builtin_execfile);
168+
#endif

py/modbuiltins.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@ STATIC const mp_map_elem_t mp_module_builtins_globals_table[] = {
627627
{ MP_OBJ_NEW_QSTR(MP_QSTR_divmod), (mp_obj_t)&mp_builtin_divmod_obj },
628628
{ MP_OBJ_NEW_QSTR(MP_QSTR_eval), (mp_obj_t)&mp_builtin_eval_obj },
629629
{ MP_OBJ_NEW_QSTR(MP_QSTR_exec), (mp_obj_t)&mp_builtin_exec_obj },
630+
#if MICROPY_PY_BUILTINS_EXECFILE
631+
{ MP_OBJ_NEW_QSTR(MP_QSTR_execfile), (mp_obj_t)&mp_builtin_execfile_obj },
632+
#endif
630633
{ MP_OBJ_NEW_QSTR(MP_QSTR_getattr), (mp_obj_t)&mp_builtin_getattr_obj },
631634
{ MP_OBJ_NEW_QSTR(MP_QSTR_globals), (mp_obj_t)&mp_builtin_globals_obj },
632635
{ MP_OBJ_NEW_QSTR(MP_QSTR_hasattr), (mp_obj_t)&mp_builtin_hasattr_obj },

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ typedef double mp_float_t;
340340
#define MICROPY_PY_BUILTINS_COMPILE (0)
341341
#endif
342342

343+
// Whether to support the Python 2 execfile function
344+
#ifndef MICROPY_PY_BUILTINS_EXECFILE
345+
#define MICROPY_PY_BUILTINS_EXECFILE (0)
346+
#endif
347+
343348
// Whether to set __file__ for imported modules
344349
#ifndef MICROPY_PY___FILE__
345350
#define MICROPY_PY___FILE__ (1)

py/qstrdefs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ Q(divmod)
167167
Q(enumerate)
168168
Q(eval)
169169
Q(exec)
170+
#if MICROPY_PY_BUILTINS_EXECFILE
171+
Q(execfile)
172+
#endif
170173
Q(filter)
171174
#if MICROPY_PY_BUILTINS_FLOAT
172175
Q(float)

stmhal/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
5555
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
5656
#define MICROPY_PY_BUILTINS_FROZENSET (1)
57+
#define MICROPY_PY_BUILTINS_EXECFILE (1)
5758
#define MICROPY_PY_SYS_EXIT (1)
5859
#define MICROPY_PY_SYS_STDFILES (1)
5960
#define MICROPY_PY_CMATH (1)

0 commit comments

Comments
 (0)