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 <jim.mussared@gmail.com>
This commit is contained in:
Jim Mussared 2021-12-11 23:04:01 +11:00 committed by Damien George
parent d6d4a5819b
commit 86ce442607
10 changed files with 33 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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