From 86ce4426079b1b368881c22f46d80045e2f720b0 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sat, 11 Dec 2021 23:04:01 +1100 Subject: [PATCH] ports: Add '.frozen' as the first entry in sys.path. Frozen modules will be searched preferentially, but gives the user the ability to override this behavior. This matches the previous behavior where "" was implicitly the frozen search path, but the frozen list was checked before the filesystem. Signed-off-by: Jim Mussared --- ports/esp32/main.c | 3 +++ ports/esp8266/main.c | 3 +++ ports/mimxrt/main.c | 3 +++ ports/nrf/main.c | 3 +++ ports/rp2/main.c | 3 +++ ports/stm32/main.c | 3 +++ ports/unix/main.c | 15 ++++++--------- ports/unix/mpconfigport.h | 3 +++ ports/unix/variants/minimal/mpconfigvariant.h | 3 +++ shared/upytesthelper/upytesthelper.c | 3 +++ 10 files changed, 33 insertions(+), 9 deletions(-) diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 4920180b23..e95930ed07 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -141,6 +141,9 @@ soft_reset: gc_init(mp_task_heap, mp_task_heap + mp_task_heap_size); mp_init(); mp_obj_list_init(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_)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib)); mp_obj_list_init(mp_sys_argv, 0); diff --git a/ports/esp8266/main.c b/ports/esp8266/main.c index 404188346c..14863a35b0 100644 --- a/ports/esp8266/main.c +++ b/ports/esp8266/main.c @@ -53,6 +53,9 @@ STATIC void mp_reset(void) { gc_init(heap, heap + sizeof(heap)); mp_init(); mp_obj_list_init(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_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_)); diff --git a/ports/mimxrt/main.c b/ports/mimxrt/main.c index 4d603913fd..28ee30d990 100644 --- a/ports/mimxrt/main.c +++ b/ports/mimxrt/main.c @@ -76,6 +76,9 @@ int main(void) { mp_init(); 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_)); mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0); #if MICROPY_PY_NETWORK diff --git a/ports/nrf/main.c b/ports/nrf/main.c index 3768b8f404..fc67bd657d 100644 --- a/ports/nrf/main.c +++ b/ports/nrf/main.c @@ -130,6 +130,9 @@ soft_reset: mp_init(); mp_obj_list_init(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_sys_argv, 0); diff --git a/ports/rp2/main.c b/ports/rp2/main.c index 793444c9a9..6a5bb8867d 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -101,6 +101,9 @@ int main(int argc, char **argv) { // Initialise MicroPython runtime. mp_init(); 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_)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib)); mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0); diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 7a7f80467b..51459682fd 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -495,6 +495,9 @@ soft_reset: // MicroPython init mp_init(); 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); diff --git a/ports/unix/main.c b/ports/unix/main.c index 031bdd75d1..cecb806283 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -495,13 +495,9 @@ MP_NOINLINE int main_(int argc, char **argv) { char *home = getenv("HOME"); char *path = getenv("MICROPYPATH"); if (path == NULL) { - #ifdef MICROPY_PY_SYS_PATH_DEFAULT path = MICROPY_PY_SYS_PATH_DEFAULT; - #else - path = "~/.micropython/lib:/usr/lib/micropython"; - #endif } - size_t path_num = 1; // [0] is for current dir (or base dir of the script) + size_t path_num = 2; // [0] is frozen, [1] is for current dir (or base dir of the script) if (*path == PATHLIST_SEP_CHAR) { path_num++; } @@ -514,10 +510,11 @@ MP_NOINLINE int main_(int argc, char **argv) { mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), path_num); mp_obj_t *path_items; mp_obj_list_get(mp_sys_path, &path_num, &path_items); - path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_); + path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen); + path_items[1] = MP_OBJ_NEW_QSTR(MP_QSTR_); { char *p = path; - for (mp_uint_t i = 1; i < path_num; i++) { + for (mp_uint_t i = 2; i < path_num; i++) { char *p1 = strchr(p, PATHLIST_SEP_CHAR); if (p1 == NULL) { p1 = p + strlen(p); @@ -658,9 +655,9 @@ MP_NOINLINE int main_(int argc, char **argv) { break; } - // Set base dir of the script as first entry in sys.path + // Set base dir of the script as second entry in sys.path. char *p = strrchr(basedir, '/'); - path_items[0] = mp_obj_new_str_via_qstr(basedir, p - basedir); + path_items[1] = mp_obj_new_str_via_qstr(basedir, p - basedir); free(pathbuf); set_sys_argv(argv, argc, a); diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 93d790ac36..68d4d8d33f 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -123,6 +123,9 @@ #define MICROPY_PY_SYS_PLATFORM "linux" #endif #endif +#ifndef MICROPY_PY_SYS_PATH_DEFAULT +#define MICROPY_PY_SYS_PATH_DEFAULT "~/.micropython/lib:/usr/lib/micropython" +#endif #define MICROPY_PY_SYS_MAXSIZE (1) #define MICROPY_PY_SYS_STDFILES (1) #define MICROPY_PY_SYS_EXC_INFO (1) diff --git a/ports/unix/variants/minimal/mpconfigvariant.h b/ports/unix/variants/minimal/mpconfigvariant.h index 973febd572..4766b8e895 100644 --- a/ports/unix/variants/minimal/mpconfigvariant.h +++ b/ports/unix/variants/minimal/mpconfigvariant.h @@ -88,6 +88,9 @@ #define MICROPY_PY_SYS (1) #define MICROPY_PY_SYS_EXIT (0) #define MICROPY_PY_SYS_PLATFORM "linux" +#ifndef MICROPY_PY_SYS_PATH_DEFAULT +#define MICROPY_PY_SYS_PATH_DEFAULT "~/.micropython/lib:/usr/lib/micropython" +#endif #define MICROPY_PY_SYS_MAXSIZE (0) #define MICROPY_PY_SYS_STDFILES (0) #define MICROPY_PY_CMATH (0) diff --git a/shared/upytesthelper/upytesthelper.c b/shared/upytesthelper/upytesthelper.c index 326172be65..ce60732424 100644 --- a/shared/upytesthelper/upytesthelper.c +++ b/shared/upytesthelper/upytesthelper.c @@ -94,6 +94,9 @@ void upytest_execute_test(const char *src) { gc_init(heap_start, heap_end); mp_init(); mp_obj_list_init(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_init(mp_sys_argv, 0); nlr_buf_t nlr;