diff options
Diffstat (limited to 'py/builtinimport.c')
-rw-r--r-- | py/builtinimport.c | 56 |
1 files changed, 36 insertions, 20 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c index dc9bf75f07..d3670858e5 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -63,7 +63,7 @@ bool mp_obj_is_package(mp_obj_t module) { // Stat either frozen or normal module by a given path // (whatever is available, if at all). STATIC mp_import_stat_t mp_import_stat_any(const char *path) { - #if MICROPY_MODULE_FROZEN_STR + #if MICROPY_MODULE_FROZEN mp_import_stat_t st = mp_frozen_stat(path); if (st != MP_IMPORT_STAT_NO_EXIST) { return st; @@ -194,10 +194,37 @@ STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) { #endif STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { - #if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_ENABLE_COMPILER + #if MICROPY_MODULE_FROZEN || MICROPY_PERSISTENT_CODE_LOAD || MICROPY_ENABLE_COMPILER char *file_str = vstr_null_terminated_str(file); #endif + // If we support frozen modules (either as str or mpy) then try to find the + // 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); + #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) { + do_load_from_lexer(module_obj, modref, file_str); + return; + } + #endif + + // If we support frozen mpy modules and we found a corresponding file (and + // its data) in the list of frozen files, execute it. + #if MICROPY_MODULE_FROZEN_MPY + if (frozen_type == MP_FROZEN_MPY) { + do_execute_raw_code(module_obj, modref); + return; + } + #endif + + // If we support loading .mpy files then check if the file extension is of + // the correct format and, if so, load and execute the file. #if MICROPY_PERSISTENT_CODE_LOAD if (file_str[file->len - 3] == 'm') { mp_raw_code_t *raw_code = mp_raw_code_load_file(file_str); @@ -206,29 +233,18 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { } #endif + // If we can compile scripts then load the file and compile and execute it. #if MICROPY_ENABLE_COMPILER { - void *modref; - #if MICROPY_MODULE_FROZEN - int frozen_type = mp_find_frozen_module(file_str, file->len, &modref); - #else - int frozen_type = MP_FROZEN_NONE; - #endif - #if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_MODULE_FROZEN_MPY - if (frozen_type == MP_FROZEN_MPY) { - do_execute_raw_code(module_obj, modref); - return; - } - #endif - if (frozen_type == MP_FROZEN_NONE) { - modref = mp_lexer_new_from_file(file_str); - } - do_load_from_lexer(module_obj, modref, file_str); + mp_lexer_t *lex = mp_lexer_new_from_file(file_str); + do_load_from_lexer(module_obj, lex, file_str); + return; } - #else + #endif + + // If we get here then the file was not frozen and we can't compile scripts. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "script compilation not supported")); - #endif } STATIC void chop_component(const char *start, const char **end) { |