From a0d0b27faf0bc76ede0fbc595f641fdceb04ac56 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 1 Sep 2017 16:12:26 -0400 Subject: Fix issue #207, esp8266 file operations problems (#222) The frozen module `_boot.py` was not being loaded on restart because `pyexec_frozen_module()` did not know about the new `.frozen` pseudo-directory. Updated lower-level routine to look in the right place. Also made ".frozen" and related values be `#define`s. --- py/builtinimport.c | 6 ++++-- py/frozenmod.c | 11 ++++++++--- py/frozenmod.h | 10 ++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) (limited to 'py') diff --git a/py/builtinimport.c b/py/builtinimport.c index a1ad6b0d5..3edf10512 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -57,8 +57,10 @@ bool mp_obj_is_package(mp_obj_t module) { // (whatever is available, if at all). STATIC mp_import_stat_t mp_import_stat_any(const char *path) { #if MICROPY_MODULE_FROZEN - if (strlen(path) > 8 && strncmp(".frozen/", path, 8) == 0) { - mp_import_stat_t st = mp_frozen_stat(path + 8); + if (strncmp(MP_FROZEN_FAKE_DIR_SLASH, + path, + MP_FROZEN_FAKE_DIR_SLASH_LENGTH) == 0) { + mp_import_stat_t st = mp_frozen_stat(path + MP_FROZEN_FAKE_DIR_SLASH_LENGTH); if (st != MP_IMPORT_STAT_NO_EXIST) { return st; } diff --git a/py/frozenmod.c b/py/frozenmod.c index 7158f6ea8..5464d0af9 100644 --- a/py/frozenmod.c +++ b/py/frozenmod.c @@ -135,16 +135,21 @@ mp_import_stat_t mp_frozen_stat(const char *str) { } int mp_find_frozen_module(const char *str, size_t len, void **data) { - // The +8/-8 account for the .frozen/ path prefix used on frozen modules. + // If the frozen module pseudo dir (e.g., ".frozen/") is a prefix of str, remove it. + if (strncmp(str, MP_FROZEN_FAKE_DIR_SLASH, MP_FROZEN_FAKE_DIR_SLASH_LENGTH) == 0) { + str = str + MP_FROZEN_FAKE_DIR_SLASH_LENGTH; + len = len - MP_FROZEN_FAKE_DIR_SLASH_LENGTH; + } + #if MICROPY_MODULE_FROZEN_STR - mp_lexer_t *lex = mp_lexer_frozen_str(str + 8, len - 8); + mp_lexer_t *lex = mp_lexer_frozen_str(str, len); if (lex != NULL) { *data = lex; return MP_FROZEN_STR; } #endif #if MICROPY_MODULE_FROZEN_MPY - const mp_raw_code_t *rc = mp_find_frozen_mpy(str + 8, len - 8); + const mp_raw_code_t *rc = mp_find_frozen_mpy(str, len); if (rc != NULL) { *data = (void*)rc; return MP_FROZEN_MPY; diff --git a/py/frozenmod.h b/py/frozenmod.h index a1fd622c4..1e50f3807 100644 --- a/py/frozenmod.h +++ b/py/frozenmod.h @@ -34,6 +34,16 @@ enum { MP_FROZEN_MPY, }; +// Frozen modules are in a pseudo-directory, so sys.path can control how they're found. +#define MP_FROZEN_FAKE_DIR ".frozen" +#define MP_FROZEN_FAKE_DIR_LENGTH (sizeof(MP_FROZEN_FAKE_DIR)-1) + +#define MP_FROZEN_FAKE_DIR_SLASH (MP_FROZEN_FAKE_DIR "/") +#define MP_FROZEN_FAKE_DIR_SLASH_LENGTH (sizeof(MP_FROZEN_FAKE_DIR_SLASH)-1) + +// This should match MP_FROZEN_FAKE_DIR. +#define MP_FROZEN_FAKE_DIR_QSTR MP_QSTR__dot_frozen + int mp_find_frozen_module(const char *str, size_t len, void **data); const char *mp_find_frozen_str(const char *str, size_t str_len, size_t *len); mp_import_stat_t mp_frozen_stat(const char *str); -- cgit v1.2.3