Skip to content

Commit 274952a

Browse files
committed
py: Allow to stat and import frozen mpy files using new frozen "VFS".
Freezing mpy files using mpy-tool.py now works again.
1 parent 3e33aeb commit 274952a

2 files changed

Lines changed: 73 additions & 38 deletions

File tree

py/builtinimport.c

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ bool mp_obj_is_package(mp_obj_t module) {
6363
// Stat either frozen or normal module by a given path
6464
// (whatever is available, if at all).
6565
STATIC mp_import_stat_t mp_import_stat_any(const char *path) {
66-
#if MICROPY_MODULE_FROZEN_STR
66+
#if MICROPY_MODULE_FROZEN
6767
mp_import_stat_t st = mp_frozen_stat(path);
6868
if (st != MP_IMPORT_STAT_NO_EXIST) {
6969
return st;
@@ -194,10 +194,37 @@ STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) {
194194
#endif
195195

196196
STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
197-
#if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_ENABLE_COMPILER
197+
#if MICROPY_MODULE_FROZEN || MICROPY_PERSISTENT_CODE_LOAD || MICROPY_ENABLE_COMPILER
198198
char *file_str = vstr_null_terminated_str(file);
199199
#endif
200200

201+
// If we support frozen modules (either as str or mpy) then try to find the
202+
// requested filename in the list of frozen module filenames.
203+
#if MICROPY_MODULE_FROZEN
204+
void *modref;
205+
int frozen_type = mp_find_frozen_module(file_str, file->len, &modref);
206+
#endif
207+
208+
// If we support frozen str modules and the compiler is enabled, and we
209+
// found the filename in the list of frozen files, then load and execute it.
210+
#if MICROPY_MODULE_FROZEN_STR
211+
if (frozen_type == MP_FROZEN_STR) {
212+
do_load_from_lexer(module_obj, modref, file_str);
213+
return;
214+
}
215+
#endif
216+
217+
// If we support frozen mpy modules and we found a corresponding file (and
218+
// its data) in the list of frozen files, execute it.
219+
#if MICROPY_MODULE_FROZEN_MPY
220+
if (frozen_type == MP_FROZEN_MPY) {
221+
do_execute_raw_code(module_obj, modref);
222+
return;
223+
}
224+
#endif
225+
226+
// If we support loading .mpy files then check if the file extension is of
227+
// the correct format and, if so, load and execute the file.
201228
#if MICROPY_PERSISTENT_CODE_LOAD
202229
if (file_str[file->len - 3] == 'm') {
203230
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) {
206233
}
207234
#endif
208235

236+
// If we can compile scripts then load the file and compile and execute it.
209237
#if MICROPY_ENABLE_COMPILER
210238
{
211-
void *modref;
212-
#if MICROPY_MODULE_FROZEN
213-
int frozen_type = mp_find_frozen_module(file_str, file->len, &modref);
214-
#else
215-
int frozen_type = MP_FROZEN_NONE;
216-
#endif
217-
#if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_MODULE_FROZEN_MPY
218-
if (frozen_type == MP_FROZEN_MPY) {
219-
do_execute_raw_code(module_obj, modref);
220-
return;
221-
}
222-
#endif
223-
if (frozen_type == MP_FROZEN_NONE) {
224-
modref = mp_lexer_new_from_file(file_str);
225-
}
226-
do_load_from_lexer(module_obj, modref, file_str);
239+
mp_lexer_t *lex = mp_lexer_new_from_file(file_str);
240+
do_load_from_lexer(module_obj, lex, file_str);
241+
return;
227242
}
228-
#else
243+
#endif
244+
245+
// If we get here then the file was not frozen and we can't compile scripts.
229246
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
230247
"script compilation not supported"));
231-
#endif
232248
}
233249

234250
STATIC void chop_component(const char *start, const char **end) {

py/frozenmod.c

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,6 @@ extern const char mp_frozen_str_names[];
4343
extern const uint32_t mp_frozen_str_sizes[];
4444
extern const char mp_frozen_str_content[];
4545

46-
mp_import_stat_t mp_frozen_stat(const char *str) {
47-
size_t len = strlen(str);
48-
const char *name = mp_frozen_str_names;
49-
50-
for (int i = 0; *name != 0; i++) {
51-
size_t l = strlen(name);
52-
if (l >= len && !memcmp(str, name, len)) {
53-
if (name[len] == 0) {
54-
return MP_IMPORT_STAT_FILE;
55-
} else if (name[len] == '/') {
56-
return MP_IMPORT_STAT_DIR;
57-
}
58-
}
59-
name += l + 1;
60-
}
61-
return MP_IMPORT_STAT_NO_EXIST;
62-
}
63-
6446
STATIC mp_lexer_t *mp_find_frozen_str(const char *str, size_t len) {
6547
const char *name = mp_frozen_str_names;
6648

@@ -103,6 +85,43 @@ STATIC const mp_raw_code_t *mp_find_frozen_mpy(const char *str, size_t len) {
10385

10486
#if MICROPY_MODULE_FROZEN
10587

88+
STATIC mp_import_stat_t mp_frozen_stat_helper(const char *name, const char *str) {
89+
size_t len = strlen(str);
90+
91+
for (int i = 0; *name != 0; i++) {
92+
size_t l = strlen(name);
93+
if (l >= len && !memcmp(str, name, len)) {
94+
if (name[len] == 0) {
95+
return MP_IMPORT_STAT_FILE;
96+
} else if (name[len] == '/') {
97+
return MP_IMPORT_STAT_DIR;
98+
}
99+
}
100+
name += l + 1;
101+
}
102+
return MP_IMPORT_STAT_NO_EXIST;
103+
}
104+
105+
mp_import_stat_t mp_frozen_stat(const char *str) {
106+
mp_import_stat_t stat;
107+
108+
#if MICROPY_MODULE_FROZEN_STR
109+
stat = mp_frozen_stat_helper(mp_frozen_str_names, str);
110+
if (stat != MP_IMPORT_STAT_NO_EXIST) {
111+
return stat;
112+
}
113+
#endif
114+
115+
#if MICROPY_MODULE_FROZEN_MPY
116+
stat = mp_frozen_stat_helper(mp_frozen_mpy_names, str);
117+
if (stat != MP_IMPORT_STAT_NO_EXIST) {
118+
return stat;
119+
}
120+
#endif
121+
122+
return MP_IMPORT_STAT_NO_EXIST;
123+
}
124+
106125
int mp_find_frozen_module(const char *str, size_t len, void **data) {
107126
#if MICROPY_MODULE_FROZEN_STR
108127
mp_lexer_t *lex = mp_find_frozen_str(str, len);

0 commit comments

Comments
 (0)