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>
This commit is contained in:
Jim Mussared
2023-06-05 16:52:29 +10:00
parent 7d2ee8aed0
commit 5e50975a6d
9 changed files with 93 additions and 42 deletions

View File

@@ -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;