From 6baacf46b746b897cc8fea0f32df4379370b2c04 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 5 Sep 2017 19:01:17 -0700 Subject: py: Only load frozen modules when the filename has the prefix. (#235) * py: Only load frozen modules when the filename has the prefix. This allows one to override a built-in module by loading a newer version onto the file system. * Unbreak mpys --- py/builtinimport.c | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/py/builtinimport.c b/py/builtinimport.c index 3edf10512..5894309ff 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -190,30 +190,37 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { 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 MICROPY_MODULE_FROZEN || MICROPY_MODULE_FROZEN_MPY + if (strncmp(MP_FROZEN_FAKE_DIR_SLASH, + file_str, + MP_FROZEN_FAKE_DIR_SLASH_LENGTH) == 0) { + // 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 + MP_FROZEN_FAKE_DIR_SLASH_LENGTH, file->len - MP_FROZEN_FAKE_DIR_SLASH_LENGTH, &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); - return; - } - #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); + 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 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 + #endif // MICROPY_MODULE_FROZEN || MICROPY_MODULE_FROZEN_MPY // 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. -- cgit v1.2.3