8000 all: Move frozen modules to virtual directory. · wnienhaus/micropython@450d6d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 450d6d3

Browse files
Wilko Nienhauswnienhaus
authored andcommitted
all: Move frozen modules to virtual directory.
This change "moves" frozen modules into a virtual directory called ".frozen". This allows controlling the search order for frozen modules, relative to other directories. Before this change, frozen modules were found via the current directory (i.e. the "" entry in sys.path). However, when a file is executed from the command line the first entry in sys.path (which is normally the empty string ""), is replaced by the base path of the executing file (correct behaviour as per CPython). This made loading frozen modules fail. With this change, frozen modules have their own entry in sys.path, which also allows the user to control the order of search when looking for frozen modules. By default, the virtual ".frozen" directory is the last entry in sys.path (i.e. it gets searched last). Fixes issue micropython#2322.
1 parent bbbdef4 commit 450d6d3

File tree

14 files changed

+52
-12
lines changed

14 files changed

+52
-12
lines changed

ports/esp32/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ void mp_task(void *pvParameter) {
143143
mp_obj_list_init(mp_sys_path, 0);
144144
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_));
145145
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
146+
#if MICROPY_MODULE_FROZEN
147+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
148+
#endif
146149
mp_obj_list_init(mp_sys_argv, 0);
147150
readline_init0();
148151

ports/esp8266/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ STATIC void mp_reset(void) {
5656
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
5757
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
5858
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_));
59+
#if MICROPY_MODULE_FROZEN
60+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
61+
#endif
5962
mp_obj_list_init(mp_sys_argv, 0);
6063
#if MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA
6164
extern void esp_native_code_init(void);

ports/mimxrt/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ int main(void) {
5757

5858
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0);
5959
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_));
60+
#if MICROPY_MODULE_FROZEN
61+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
62+
#endif
6063
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);
6164

6265
// Initialise sub-systems.

ports/nrf/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ int main(int argc, char **argv) {
252252
}
253253
#endif
254254

255+
#if MICROPY_MODULE_FROZEN
256+
// add frozen virtual directory to path
257+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
258+
#endif
259+
255260
// Main script is finished, so now go into REPL mode.
256261
// The REPL mode can change, or it can request a soft reset.
257262
int ret_code = 0;

ports/rp2/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ int main(int argc, char **argv) {
100100
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0);
101101
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_));
102102
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
103+
#if MICROPY_MODULE_FROZEN
104+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
105+
#endif
103106
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);
104107

105108
// Initialise sub-systems.

ports/stm32/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,11 @@ void stm32_main(uint32_t reset_mode) {
558558
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib));
559559
}
560560

561+
#if MICROPY_MODULE_FROZEN
562+
// add frozen virtual directory to path
563+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
564+
#endif
565+
561566
// reset config variables; they should be set by boot.py
562567
MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL;
563568

ports/teensy/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ int main(void) {
271271
mp_init();
272272
mp_obj_list_init(mp_sys_path, 0);
273273
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
274+
#if MICROPY_MODULE_FROZEN
275+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
276+
#endif
274277
mp_obj_list_init(mp_sys_argv, 0);
275278

276279
readline_init0();

ports/unix/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
498498
#ifdef MICROPY_PY_SYS_PATH_DEFAULT
499499
path = MICROPY_PY_SYS_PATH_DEFAULT;
500500
#else
501-
path = "~/.micropython/lib:/usr/lib/micropython";
501+
path = "~/.micropython/lib:/usr/lib/micropython:.frozen";
502502
#endif
503503
}
504504
size_t path_num = 1; // [0] is for current dir (or base dir of the script)

ports/zephyr/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ int real_main(void) {
141141
mp_init();
142142
mp_obj_list_init(mp_sys_path, 0);
143143
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
144+
#if MICROPY_MODULE_FROZEN
145+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
146+
#endif
144147
mp_obj_list_init(mp_sys_argv, 0);
145148

146149
#ifdef CONFIG_USB

py/builtinimport.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ bool mp_obj_is_package(mp_obj_t module) {
5858
// (whatever is available, if at all).
5959
STATIC mp_import_stat_t mp_import_stat_any(const char *path) {
6060
#if MICROPY_MODULE_FROZEN
61-
mp_import_stat_t st = mp_frozen_stat(path);
62-
if (st != MP_IMPORT_STAT_NO_EXIST) {
63-
return st;
61+
if (strncmp(path, ".frozen/", 8) == 0) {
62+
mp_import_stat_t st = mp_frozen_stat(path + 8); // +8 skips .frozen/
63+
if (st != MP_IMPORT_STAT_NO_EXIST) {
64+
return st;
65+
}
6466
}
6567
#endif
6668
return mp_import_stat(path);
@@ -188,13 +190,17 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
188190
// requested filename in the list of frozen module filenames.
189191
#if MICROPY_MODULE_FROZEN
190192
void *modref;
191-
int frozen_type = mp_find_frozen_module(file_str, file->len, &modref);
193+
int frozen_type = MP_FROZEN_NONE;
194+
if (strncmp(file_str, ".frozen/", 8) == 0) {
195+
frozen_type = mp_find_frozen_module(file_str + 8, file->len - 8, &modref); // +8 skips .frozen/
196+
}
192197
#endif
193198

194199
// If we support frozen str modules and the compiler is enabled, and we
195200
// found the filename in the list of frozen files, then load and execute it.
196201
#if MICROPY_MODULE_FROZEN_STR
197202
if (frozen_type == MP_FROZEN_STR) {
203+
((mp_lexer_t *)modref)->source_name = qstr_from_str(file_str);
198204
do_load_from_lexer(module_obj, modref);
199205
return;
200206
}

0 commit comments

Comments
 (0)
0