Skip to content

all: Move frozen modules to virtual directory. #8000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ports/esp32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ void mp_task(void *pvParameter) {
mp_obj_list_init(mp_sys_path, 0);
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));
#if MICROPY_MODULE_FROZEN
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__pipe_frozen));
#endif
mp_obj_list_init(mp_sys_argv, 0);
readline_init0();

Expand Down
3 changes: 3 additions & 0 deletions ports/esp8266/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ STATIC void mp_reset(void) {
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_));
#if MICROPY_MODULE_FROZEN
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__pipe_frozen));
#endif
mp_obj_list_init(mp_sys_argv, 0);
#if MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA
extern void esp_native_code_init(void);
Expand Down
3 changes: 3 additions & 0 deletions ports/mimxrt/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ int main(void) {

mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0);
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_));
#if MICROPY_MODULE_FROZEN
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__pipe_frozen));
#endif
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);
#if MICROPY_PY_NETWORK
mod_network_init();
Expand Down
5 changes: 5 additions & 0 deletions ports/nrf/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ int main(int argc, char **argv) {
}
#endif

#if MICROPY_MODULE_FROZEN
// add frozen virtual directory to path
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__pipe_frozen));
#endif

// Main script is finished, so now go into REPL mode.
// The REPL mode can change, or it can request a soft reset.
int ret_code = 0;
Expand Down
3 changes: 3 additions & 0 deletions ports/rp2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ int main(int argc, char **argv) {
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0);
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));
#if MICROPY_MODULE_FROZEN
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__pipe_frozen));
#endif
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);

// Initialise sub-systems.
Expand Down
5 changes: 5 additions & 0 deletions ports/stm32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,11 @@ void stm32_main(uint32_t reset_mode) {
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib));
}

#if MICROPY_MODULE_FROZEN
// add frozen virtual directory to path
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__pipe_frozen));
#endif

// reset config variables; they should be set by boot.py
MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL;

Expand Down
3 changes: 3 additions & 0 deletions ports/teensy/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ int main(void) {
mp_init();
mp_obj_list_init(mp_sys_path, 0);
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__pipe_frozen));
#endif
mp_obj_list_init(mp_sys_argv, 0);

readline_init0();
Expand Down
2 changes: 1 addition & 1 deletion ports/unix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
#ifdef MICROPY_PY_SYS_PATH_DEFAULT
path = MICROPY_PY_SYS_PATH_DEFAULT;
#else
path = "~/.micropython/lib:/usr/lib/micropython";
path = "~/.micropython/lib:/usr/lib/micropython:|frozen";
#endif
}
size_t path_num = 1; // [0] is for current dir (or base dir of the script)
Expand Down
2 changes: 1 addition & 1 deletion ports/windows/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
#define MICROPY_PY_SYS_ATEXIT (1)
#define MICROPY_PY_SYS_PLATFORM "win32"
#ifndef MICROPY_PY_SYS_PATH_DEFAULT
#define MICROPY_PY_SYS_PATH_DEFAULT "~/.micropython/lib"
#define MICROPY_PY_SYS_PATH_DEFAULT "~/.micropython/lib;|frozen"
#endif
#define MICROPY_PY_SYS_MAXSIZE (1)
#define MICROPY_PY_SYS_STDFILES (1)
Expand Down
3 changes: 3 additions & 0 deletions ports/zephyr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ int real_main(void) {
mp_init();
mp_obj_list_init(mp_sys_path, 0);
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__pipe_frozen));
#endif
mp_obj_list_init(mp_sys_argv, 0);

#ifdef CONFIG_USB
Expand Down
15 changes: 11 additions & 4 deletions py/builtinimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#if MICROPY_ENABLE_EXTERNAL_IMPORT

#define PATH_SEP_CHAR '/'
#define PATH_FROZEN "|frozen/"

bool mp_obj_is_package(mp_obj_t module) {
mp_obj_t dest[2];
Expand All @@ -58,9 +59,11 @@ bool mp_obj_is_package(mp_obj_t module) {
// (whatever is available, if at all).
STATIC mp_import_stat_t mp_import_stat_any(const char *path) {
#if MICROPY_MODULE_FROZEN
mp_import_stat_t st = mp_frozen_stat(path);
if (st != MP_IMPORT_STAT_NO_EXIST) {
return st;
if (strncmp(path, PATH_FROZEN, strlen(PATH_FROZEN)) == 0) {
mp_import_stat_t st = mp_frozen_stat(path + strlen(PATH_FROZEN));
if (st != MP_IMPORT_STAT_NO_EXIST) {
return st;
}
}
#endif
return mp_import_stat(path);
Expand Down Expand Up @@ -188,13 +191,17 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
// requested filename in the list of frozen module filenames.
#if MICROPY_MODULE_FROZEN
void *modref;
int frozen_type = mp_find_frozen_module(file_str, file->len, &modref);
int frozen_type = MP_FROZEN_NONE;
if (strncmp(file_str, PATH_FROZEN, strlen(PATH_FROZEN)) == 0) {
frozen_type = mp_find_frozen_module(file_str + strlen(PATH_FROZEN), file->len - strlen(PATH_FROZEN), &modref);
}
#endif

// If we support frozen str modules and the compiler is enabled, and we
// found the filename in the list of frozen files, then load and execute it.
#if MICROPY_MODULE_FROZEN_STR
if (frozen_type == MP_FROZEN_STR) {
((mp_lexer_t *)modref)->source_name = qstr_from_str(file_str);
do_load_from_lexer(module_obj, modref);
return;
}
Expand Down
7 changes: 5 additions & 2 deletions py/modio.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,11 @@ STATIC mp_obj_t resource_stream(mp_obj_t package_in, mp_obj_t path_in) {
const char *path = mp_obj_str_get_data(path_in, &len);
vstr_add_strn(&path_buf, path, len);

len = path_buf.len;
const char *data = mp_find_frozen_str(path_buf.buf, &len);
const char *data = NULL;
if (strncmp(path_buf.buf, "|frozen/", 8) == 0) {
len = path_buf.len - 8;
data = mp_find_frozen_str(path_buf.buf + 8, &len); // +8 skips |frozen/
}
if (data != NULL) {
mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t);
o->base.type = &mp_type_bytesio;
Expand Down
3 changes: 3 additions & 0 deletions py/qstrdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ Q({:#o})
Q({:#x})
#endif
Q({:#b})
#if MICROPY_MODULE_FROZEN
Q(|frozen)
#endif
Q( )
Q(\n)
Q(maximum recursion depth exceeded)
Expand Down
3 changes: 3 additions & 0 deletions shared/upytesthelper/upytesthelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__pipe_frozen));
#endif
mp_obj_list_init(mp_sys_argv, 0);

nlr_buf_t nlr;
Expand Down
2 changes: 1 addition & 1 deletion tests/run-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ def main():

if not args.keep_path:
# clear search path to make sure tests use only builtin modules and those in extmod
os.environ["MICROPYPATH"] = os.pathsep + base_path("../extmod")
os.environ["MICROPYPATH"] = os.pathsep + base_path("../extmod") + os.pathsep + "|frozen"

try:
os.makedirs(args.result_dir, exist_ok=True)
Expand Down
8 changes: 4 additions & 4 deletions tests/unix/extra_coverage.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ cpp None
5
(3, 'hellocpp')
frzstr1
frzstr1.py
|frozen/frzstr1.py
frzmpy1
frzmpy1.py
|frozen/frzmpy1.py
frzstr_pkg1.__init__
frzstr_pkg1/__init__.py 1
|frozen/frzstr_pkg1/__init__.py 1
frzmpy_pkg1.__init__
frzmpy_pkg1/__init__.py 1
|frozen/frzmpy_pkg1/__init__.py 1
frzstr_pkg2.mod
1
frzmpy_pkg2.mod
Expand Down