summaryrefslogtreecommitdiff
path: root/py/builtinimport.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/builtinimport.c')
-rw-r--r--py/builtinimport.c50
1 files changed, 29 insertions, 21 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c
index 755ce779a..3e336633d 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -50,6 +50,9 @@
// Must be a string of one byte.
#define PATH_SEP_CHAR "/"
+// Virtual sys.path entry that maps to the frozen modules.
+#define MP_FROZEN_PATH_PREFIX ".frozen/"
+
bool mp_obj_is_package(mp_obj_t module) {
mp_obj_t dest[2];
mp_load_method_maybe(module, MP_QSTR___path__, dest);
@@ -62,9 +65,10 @@ bool mp_obj_is_package(mp_obj_t module) {
// will return whether the path is a file, directory, or doesn't exist.
STATIC mp_import_stat_t stat_path_or_frozen(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;
+ // Only try and load as a frozen module if it starts with .frozen/.
+ const int frozen_path_prefix_len = strlen(MP_FROZEN_PATH_PREFIX);
+ if (strncmp(path, MP_FROZEN_PATH_PREFIX, frozen_path_prefix_len) == 0) {
+ return mp_find_frozen_module(path + frozen_path_prefix_len, NULL, NULL);
}
#endif
return mp_import_stat(path);
@@ -193,32 +197,36 @@ STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code, co
STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
#if MICROPY_MODULE_FROZEN || MICROPY_ENABLE_COMPILER || (MICROPY_PERSISTENT_CODE_LOAD && MICROPY_HAS_FILE_READER)
- char *file_str = vstr_null_terminated_str(file);
+ const 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);
-
- // 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
+ int frozen_type;
+ const int frozen_path_prefix_len = strlen(MP_FROZEN_PATH_PREFIX);
+ if (strncmp(file_str, MP_FROZEN_PATH_PREFIX, frozen_path_prefix_len) == 0) {
+ mp_find_frozen_module(file_str + frozen_path_prefix_len, &frozen_type, &modref);
+
+ // 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, file_str);
- return;
+ // 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, file_str + frozen_path_prefix_len);
+ return;
+ }
+ #endif
}
- #endif
#endif // MICROPY_MODULE_FROZEN