diff options
| author | Damien George <damien@micropython.org> | 2021-12-17 23:35:32 +1100 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2021-12-18 00:08:07 +1100 |
| commit | de43b500bdff465539d89c5004d9e8cb3849eab1 (patch) | |
| tree | 00e10489fa3b8dd9d785b60999a4239ee9aa0016 /py | |
| parent | 86394f70fcd7b9fa66ec952711e5d64557d81a1e (diff) | |
py/runtime: Allow initialising sys.path/argv with defaults.
If MICROPY_PY_SYS_PATH_ARGV_DEFAULTS is enabled (which it is by default)
then sys.path and sys.argv will be initialised and populated with default
values. This keeps all bare-metal ports aligned.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py')
| -rw-r--r-- | py/mpconfig.h | 5 | ||||
| -rw-r--r-- | py/mpstate.h | 3 | ||||
| -rw-r--r-- | py/runtime.c | 9 |
3 files changed, 16 insertions, 1 deletions
diff --git a/py/mpconfig.h b/py/mpconfig.h index 86e3e0f34..f0d11961d 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1306,6 +1306,11 @@ typedef double mp_float_t; #define MICROPY_PY_SYS (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_CORE_FEATURES) #endif +// Whether to initialise "sys.path" and "sys.argv" to their defaults in mp_init() +#ifndef MICROPY_PY_SYS_PATH_ARGV_DEFAULTS +#define MICROPY_PY_SYS_PATH_ARGV_DEFAULTS (1) +#endif + // Whether to provide "sys.maxsize" constant #ifndef MICROPY_PY_SYS_MAXSIZE #define MICROPY_PY_SYS_MAXSIZE (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES) diff --git a/py/mpstate.h b/py/mpstate.h index 69360738c..69fee0654 100644 --- a/py/mpstate.h +++ b/py/mpstate.h @@ -153,7 +153,8 @@ typedef struct _mp_state_vm_t { // dictionary for the __main__ module mp_obj_dict_t dict_main; - // these two lists must be initialised per port, after the call to mp_init + // If MICROPY_PY_SYS_PATH_ARGV_DEFAULTS is not enabled then these two lists + // must be initialised after the call to mp_init. mp_obj_list_t mp_sys_path_obj; mp_obj_list_t mp_sys_argv_obj; diff --git a/py/runtime.c b/py/runtime.c index 0120b70d7..7607ffb19 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -122,6 +122,15 @@ void mp_init(void) { MP_STATE_VM(vfs_mount_table) = NULL; #endif + #if MICROPY_PY_SYS_PATH_ARGV_DEFAULTS + mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0); + #if MICROPY_MODULE_FROZEN + mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen)); + #endif + mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script) + mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0); + #endif + #if MICROPY_PY_SYS_ATEXIT MP_STATE_VM(sys_exitfunc) = mp_const_none; #endif |
