Skip to content

Commit f6a7025

Browse files
committed
py: Pretend frozen files are stored under .frozen rather than the empty path.
This makes it clear when frozen modules are loaded as opposed to the empty path which represents the current working directory. Furthermore, by splitting the two apart this allows one to control in what order frozen modules are loaded. This is a prerequisite for adafruit#56.
1 parent 5c32a5a commit f6a7025

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

py/builtinimport.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ bool mp_obj_is_package(mp_obj_t module) {
5757
// (whatever is available, if at all).
5858
STATIC mp_import_stat_t mp_import_stat_any(const char *path) {
5959
#if MICROPY_MODULE_FROZEN
60-
mp_import_stat_t st = mp_frozen_stat(path);
61-
if (st != MP_IMPORT_STAT_NO_EXIST) {
62-
return st;
60+
if (strlen(path) > 8 && strncmp(".frozen/", path, 8) == 0) {
61+
mp_import_stat_t st = mp_frozen_stat(path + 8);
62+
if (st != MP_IMPORT_STAT_NO_EXIST) {
63+
return st;
64+
}
6365
}
6466
#endif
6567
return mp_import_stat(path);

py/frozenmod.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ extern const uint32_t mp_frozen_str_sizes[];
4444
extern const char mp_frozen_str_content[];
4545

4646
// On input, *len contains size of name, on output - size of content
47-
const char *mp_find_frozen_str(const char *str, size_t *len) {
47+
const char *mp_find_frozen_str(const char *str, size_t str_len, size_t *len) {
4848
const char *name = mp_frozen_str_names;
4949

5050
size_t offset = 0;
5151
for (int i = 0; *name != 0; i++) {
5252
size_t l = strlen(name);
53-
if (l == *len && !memcmp(str, name, l)) {
53+
if (l == str_len && !memcmp(str, name, l)) {
5454
*len = mp_frozen_str_sizes[i];
5555
return mp_frozen_str_content + offset;
5656
}
@@ -60,16 +60,16 @@ const char *mp_find_frozen_str(const char *str, size_t *len) {
6060
return NULL;
6161
}
6262

63-
STATIC mp_lexer_t *mp_lexer_frozen_str(const char *str, size_t len) {
64-
size_t name_len = len;
65-
const char *content = mp_find_frozen_str(str, &len);
63+
STATIC mp_lexer_t *mp_lexer_frozen_str(const char *str, size_t str_len) {
64+
size_t file_len;
65+
const char *content = mp_find_frozen_str(str, str_len, &file_len);
6666

6767
if (content == NULL) {
6868
return NULL;
6969
}
7070

71-
qstr source = qstr_from_strn(str, name_len);
72-
mp_lexer_t *lex = MICROPY_MODULE_FROZEN_LEXER(source, content, len, 0);
71+
qstr source = qstr_from_strn(str, str_len);
72+
mp_lexer_t *lex = MICROPY_MODULE_FROZEN_LEXER(source, content, file_len, 0);
7373
return lex;
7474
}
7575

@@ -82,11 +82,11 @@ STATIC mp_lexer_t *mp_lexer_frozen_str(const char *str, size_t len) {
8282
extern const char mp_frozen_mpy_names[];
8383
extern const mp_raw_code_t *const mp_frozen_mpy_content[];
8484

85-
STATIC const mp_raw_code_t *mp_find_frozen_mpy(const char *str, size_t len) {
85+
STATIC const mp_raw_code_t *mp_find_frozen_mpy(const char *str, size_t str_len) {
8686
const char *name = mp_frozen_mpy_names;
8787
for (size_t i = 0; *name != 0; i++) {
8888
size_t l = strlen(name);
89-
if (l == len && !memcmp(str, name, l)) {
89+
if (l == str_len && !memcmp(str, name, l)) {
9090
return mp_frozen_mpy_content[i];
9191
}
9292
name += l + 1;
@@ -136,15 +136,16 @@ mp_import_stat_t mp_frozen_stat(const char *str) {
136136
}
137137

138138
int mp_find_frozen_module(const char *str, size_t len, void **data) {
139+
// The +8/-8 account for the .frozen/ path prefix used on frozen modules.
139140
#if MICROPY_MODULE_FROZEN_STR
140-
mp_lexer_t *lex = mp_lexer_frozen_str(str, len);
141+
mp_lexer_t *lex = mp_lexer_frozen_str(str + 8, len - 8);
141142
if (lex != NULL) {
142143
*data = lex;
143144
return MP_FROZEN_STR;
144145
}
145146
#endif
146147
#if MICROPY_MODULE_FROZEN_MPY
147-
const mp_raw_code_t *rc = mp_find_frozen_mpy(str, len);
148+
const mp_raw_code_t *rc = mp_find_frozen_mpy(str + 8, len - 8);
148149
if (rc != NULL) {
149150
*data = (void*)rc;
150151
return MP_FROZEN_MPY;

py/frozenmod.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ enum {
3333
};
3434

3535
int mp_find_frozen_module(const char *str, size_t len, void **data);
36-
const char *mp_find_frozen_str(const char *str, size_t *len);
36+
const char *mp_find_frozen_str(const char *str, size_t str_len, size_t *len);
3737
mp_import_stat_t mp_frozen_stat(const char *str);

py/modio.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,14 @@ STATIC mp_obj_t resource_stream(mp_obj_t package_in, mp_obj_t path_in) {
164164
const char *path = mp_obj_str_get_data(path_in, &len);
165165
vstr_add_strn(&path_buf, path, len);
166166

167-
len = path_buf.len;
168-
const char *data = mp_find_frozen_str(path_buf.buf, &len);
167+
size_t file_len;
168+
const char *data = mp_find_frozen_str(path_buf.buf, path_buf.len, &file_len);
169169
if (data != NULL) {
170170
mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t);
171171
o->base.type = &mp_type_bytesio;
172172
o->vstr = m_new_obj(vstr_t);
173-
vstr_init_fixed_buf(o->vstr, len + 1, (char*)data);
174-
o->vstr->len = len;
173+
vstr_init_fixed_buf(o->vstr, file_len + 1, (char*)data);
174+
o->vstr->len = file_len;
175175
o->pos = 0;
176176
return MP_OBJ_FROM_PTR(o);
177177
}

unix/main.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,9 @@ MP_NOINLINE int main_(int argc, char **argv) {
453453
path = "~/.micropython/lib:/usr/lib/micropython";
454454
#endif
455455
}
456-
size_t path_num = 1; // [0] is for current dir (or base dir of the script)
456+
size_t path_num = 2; // [0] is for current dir (or base dir of the script)
457+
// [1] is for frozen files.
458+
size_t builtin_path_count = path_num;
457459
if (*path == ':') {
458460
path_num++;
459461
}
@@ -467,9 +469,10 @@ MP_NOINLINE int main_(int argc, char **argv) {
467469
mp_obj_t *path_items;
468470
mp_obj_list_get(mp_sys_path, &path_num, &path_items);
469471
path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
472+
path_items[1] = MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen);
470473
{
471474
char *p = path;
472-
for (mp_uint_t i = 1; i < path_num; i++) {
475+
for (mp_uint_t i = builtin_path_count; i < path_num; i++) {
473476
char *p1 = strchr(p, PATHLIST_SEP_CHAR);
474477
if (p1 == NULL) {
475478
p1 = p + strlen(p);

0 commit comments

Comments
 (0)