diff options
| author | Jim Mussared <jim.mussared@gmail.com> | 2023-06-05 16:52:29 +1000 |
|---|---|---|
| committer | Jim Mussared <jim.mussared@gmail.com> | 2023-06-08 17:54:24 +1000 |
| commit | 5e50975a6dd9466afafbcd012c00078093fe1f57 (patch) | |
| tree | 92e29c7c944b69ed94c3f161c6c8519d5e7bed15 /py | |
| parent | 7d2ee8aed0cc5ba1a0041ac4cc7631898aaf252f (diff) | |
py/modsys: Allow sys.path to be assigned to.
Previously sys.path could be modified by append/pop or slice assignment.
This allows `sys.path = [...]`, which can be simpler in many cases, but
also improves CPython compatibility.
It also allows sys.path to be set to a tuple which means that you can
clear sys.path (e.g. temporarily) with no allocations.
This also makes sys.path (and sys.argv for consistency) able to be disabled
via mpconfig. The unix port (and upytesthelper) require them, so they
explicitly verify that they're enabled.
This work was funded through GitHub Sponsors.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'py')
| -rw-r--r-- | py/builtinimport.c | 4 | ||||
| -rw-r--r-- | py/modsys.c | 18 | ||||
| -rw-r--r-- | py/mpconfig.h | 19 | ||||
| -rw-r--r-- | py/mpstate.h | 3 | ||||
| -rw-r--r-- | py/runtime.c | 6 | ||||
| -rw-r--r-- | py/runtime.h | 7 |
6 files changed, 48 insertions, 9 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c index 8a125fc53..4fee04b8f 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -118,7 +118,7 @@ STATIC mp_import_stat_t stat_top_level(qstr mod_name, vstr_t *dest) { #if MICROPY_PY_SYS size_t path_num; mp_obj_t *path_items; - mp_obj_list_get(mp_sys_path, &path_num, &path_items); + mp_obj_get_array(mp_sys_path, &path_num, &path_items); // go through each sys.path entry, trying to import "<entry>/<mod_name>". for (size_t i = 0; i < path_num; i++) { @@ -365,7 +365,7 @@ STATIC mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name, // which may have come from the filesystem. size_t path_num; mp_obj_t *path_items; - mp_obj_list_get(mp_sys_path, &path_num, &path_items); + mp_obj_get_array(mp_sys_path, &path_num, &path_items); if (path_num) #endif { diff --git a/py/modsys.c b/py/modsys.c index 72817ce00..9b3a2bc16 100644 --- a/py/modsys.c +++ b/py/modsys.c @@ -195,6 +195,9 @@ STATIC mp_obj_t mp_sys_settrace(mp_obj_t obj) { MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_settrace_obj, mp_sys_settrace); #endif // MICROPY_PY_SYS_SETTRACE +#if MICROPY_PY_SYS_PATH && !MICROPY_PY_SYS_ATTR_DELEGATION +#error "MICROPY_PY_SYS_PATH requires MICROPY_PY_SYS_ATTR_DELEGATION" +#endif #if MICROPY_PY_SYS_PS1_PS2 && !MICROPY_PY_SYS_ATTR_DELEGATION #error "MICROPY_PY_SYS_PS1_PS2 requires MICROPY_PY_SYS_ATTR_DELEGATION" @@ -211,6 +214,11 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_settrace_obj, mp_sys_settrace); #if MICROPY_PY_SYS_ATTR_DELEGATION // Must be kept in sync with the enum at the top of mpstate.h. STATIC const uint16_t sys_mutable_keys[] = { + #if MICROPY_PY_SYS_PATH + // Code should access this (as an mp_obj_t) for use with e.g. + // mp_obj_list_append by using the `mp_sys_path` macro defined in runtime.h. + MP_QSTR_path, + #endif #if MICROPY_PY_SYS_PS1_PS2 MP_QSTR_ps1, MP_QSTR_ps2, @@ -231,8 +239,9 @@ void mp_module_sys_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) }, - { MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&MP_STATE_VM(mp_sys_path_obj)) }, + #if MICROPY_PY_SYS_ARGV { MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) }, + #endif { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&mp_sys_version_obj) }, { MP_ROM_QSTR(MP_QSTR_version_info), MP_ROM_PTR(&mp_sys_version_info_obj) }, { MP_ROM_QSTR(MP_QSTR_implementation), MP_ROM_PTR(&mp_sys_implementation_obj) }, @@ -308,10 +317,11 @@ const mp_obj_module_t mp_module_sys = { // available. MP_REGISTER_MODULE(MP_QSTR_sys, mp_module_sys); -// If MICROPY_PY_SYS_PATH_ARGV_DEFAULTS is not enabled then these two lists -// must be initialised after the call to mp_init. -MP_REGISTER_ROOT_POINTER(mp_obj_list_t mp_sys_path_obj); +#if MICROPY_PY_SYS_ARGV +// Code should access this (as an mp_obj_t) for use with e.g. +// mp_obj_list_append by using the `mp_sys_argv` macro defined in runtime.h. MP_REGISTER_ROOT_POINTER(mp_obj_list_t mp_sys_argv_obj); +#endif #if MICROPY_PY_SYS_EXC_INFO // current exception being handled, for sys.exc_info() diff --git a/py/mpconfig.h b/py/mpconfig.h index b6f883866..eb23c5965 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1421,6 +1421,23 @@ typedef double mp_float_t; #define MICROPY_PY_SYS_ATEXIT (0) #endif +// Whether to provide the "sys.path" attribute (which forces module delegation +// and mutable sys attributes to be enabled). +// If MICROPY_PY_SYS_PATH_ARGV_DEFAULTS is enabled, this is initialised in +// mp_init to an empty list. Otherwise the port must initialise it using +// `mp_sys_path = mp_obj_new_list(...)`. +#ifndef MICROPY_PY_SYS_PATH +#define MICROPY_PY_SYS_PATH (1) +#endif + +// Whether to provide the "sys.argv" attribute. +// If MICROPY_PY_SYS_PATH_ARGV_DEFAULTS is enabled, this is initialised in +// mp_init to an empty list. Otherwise the port must initialise it using +// `mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), ...);` +#ifndef MICROPY_PY_SYS_ARGV +#define MICROPY_PY_SYS_ARGV (1) +#endif + // Whether to provide sys.{ps1,ps2} mutable attributes, to control REPL prompts #ifndef MICROPY_PY_SYS_PS1_PS2 #define MICROPY_PY_SYS_PS1_PS2 (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES) @@ -1455,7 +1472,7 @@ typedef double mp_float_t; // Whether the sys module supports attribute delegation // This is enabled automatically when needed by other features #ifndef MICROPY_PY_SYS_ATTR_DELEGATION -#define MICROPY_PY_SYS_ATTR_DELEGATION (MICROPY_PY_SYS_PS1_PS2 || MICROPY_PY_SYS_TRACEBACKLIMIT) +#define MICROPY_PY_SYS_ATTR_DELEGATION (MICROPY_PY_SYS_PATH || MICROPY_PY_SYS_PS1_PS2 || MICROPY_PY_SYS_TRACEBACKLIMIT) #endif // Whether to provide "errno" module diff --git a/py/mpstate.h b/py/mpstate.h index 3786131de..080dc1380 100644 --- a/py/mpstate.h +++ b/py/mpstate.h @@ -43,6 +43,9 @@ #if MICROPY_PY_SYS_ATTR_DELEGATION // Must be kept in sync with sys_mutable_keys in modsys.c. enum { + #if MICROPY_PY_SYS_PATH + MP_SYS_MUTABLE_PATH, + #endif #if MICROPY_PY_SYS_PS1_PS2 MP_SYS_MUTABLE_PS1, MP_SYS_MUTABLE_PS2, diff --git a/py/runtime.c b/py/runtime.c index 2326dfb3c..f5d219728 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -136,13 +136,17 @@ void mp_init(void) { #endif #if MICROPY_PY_SYS_PATH_ARGV_DEFAULTS - mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0); + #if MICROPY_PY_SYS_PATH + mp_sys_path = mp_obj_new_list(0, NULL); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script) #if MICROPY_MODULE_FROZEN mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen)); #endif + #endif + #if MICROPY_PY_SYS_ARGV mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0); #endif + #endif // MICROPY_PY_SYS_PATH_ARGV_DEFAULTS #if MICROPY_PY_SYS_ATEXIT MP_STATE_VM(sys_exitfunc) = mp_const_none; diff --git a/py/runtime.h b/py/runtime.h index 78194973d..c033c77b4 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -227,8 +227,13 @@ int mp_native_type_from_qstr(qstr qst); mp_uint_t mp_native_from_obj(mp_obj_t obj, mp_uint_t type); mp_obj_t mp_native_to_obj(mp_uint_t val, mp_uint_t type); -#define mp_sys_path (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_path_obj))) +#if MICROPY_PY_SYS_PATH +#define mp_sys_path (MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_PATH])) +#endif + +#if MICROPY_PY_SYS_ARGV #define mp_sys_argv (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_argv_obj))) +#endif #if MICROPY_WARNINGS #ifndef mp_warning |
