8000 Fix issue #207, esp8266 file operations problems (#222) · sparkfun/circuitpython@a0d0b27 · GitHub
[go: up one dir, main page]

Skip to content

Commit a0d0b27

Browse files
authored
Fix issue adafruit#207, esp8266 file operations problems (adafruit#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.
1 parent c01bf67 commit a0d0b27

File tree

6 files changed

+31
-10
lines changed

6 files changed

+31
-10
lines changed

atmel-samd/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "py/nlr.h"
3131
#include "py/compile.h"
32+
#include "py/frozenmod.h"
3233
#include "py/mphal.h"
3334
#include "py/runtime.h"
3435
#include "py/repl.h"
@@ -165,8 +166,8 @@ void reset_mp(void) {
165166
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
166167
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_));
167168
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
168-
// Frozen modules are in their own pseudo-dir, ".frozen".
169-
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
169+
// Frozen modules are in their own pseudo-dir, e.g., ".frozen".
170+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_FROZEN_FAKE_DIR_QSTR));
170171

171172
mp_obj_list_init(mp_sys_argv, 0);
172173
}

esp8266/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "py/nlr.h"
3131
#include "py/compile.h"
32+
#include "py/frozenmod.h"
3233
#include "py/runtime0.h"
3334
#include "py/runtime.h"
3435
#include "py/stackctrl.h"
@@ -98,8 +99,8 @@ STATIC void mp_reset(void) {
9899
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
99100
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
100101
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_));
101-
// Frozen modules are in their own pseudo-dir, ".frozen".
102-
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
102+
// Frozen modules are in their own pseudo-dir, e.g., ".frozen".
103+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_FROZEN_FAKE_DIR_QSTR));
103104

104105
mp_obj_list_init(mp_sys_argv, 0);
105106
MP_STATE_PORT(term_obj) = MP_OBJ_NULL;

py/builtinimport.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ 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-
if (strlen(path) > 8 && strncmp(".frozen/", path, 8) == 0) {
61-
mp_import_stat_t st = mp_frozen_stat(path + 8);
60+
if (strncmp(MP_FROZEN_FAKE_DIR_SLASH,
61+
path,
62+
MP_FROZEN_FAKE_DIR_SLASH_LENGTH) == 0) {
63+
mp_import_stat_t st = mp_frozen_stat(path + MP_FROZEN_FAKE_DIR_SLASH_LENGTH);
6264
if (st != MP_IMPORT_STAT_NO_EXIST) {
6365
return st;
6466
}

py/frozenmod.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,21 @@ mp_import_stat_t mp_frozen_stat(const char *str) {
135135
}
136136

137137
int mp_find_frozen_module(const char *str, size_t len, void **data) {
138-
// The +8/-8 account for the .frozen/ path prefix used on frozen modules.
138+
// If the frozen module pseudo dir (e.g., ".frozen/") is a prefix of str, remove it.
139+
if (strncmp(str, MP_FROZEN_FAKE_DIR_SLASH, MP_FROZEN_FAKE_DIR_SLASH_LENGTH) == 0) {
140+
str = str + MP_FROZEN_FAKE_DIR_SLASH_LENGTH;
141+
len = len - MP_FROZEN_FAKE_DIR_SLASH_LENGTH;
142+
}
143+
139144
#if MICROPY_MODULE_FROZEN_STR
140-
mp_lexer_t *lex = mp_lexer_frozen_str(str + 8, len - 8);
145+
mp_lexer_t *lex = mp_lexer_frozen_str(str, len);
141146
if (lex != NULL) {
142147
*data = lex;
143148
return MP_FROZEN_STR;
144149
}
145150
#en 67F4 dif
146151
#if MICROPY_MODULE_FROZEN_MPY
147-
const mp_raw_code_t *rc = mp_find_frozen_mpy(str + 8, len - 8);
152+
const mp_raw_code_t *rc = mp_find_frozen_mpy(str, len);
148153
if (rc != NULL) {
149154
*data = (void*)rc;
150155
return MP_FROZEN_MPY;

py/frozenmod.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ enum {
3434
MP_FROZEN_MPY,
3535
};
3636

37+
// Frozen modules are in a pseudo-directory, so sys.path can control how they're found.
38+
#define MP_FROZEN_FAKE_DIR ".frozen"
39+
#define MP_FROZEN_FAKE_DIR_LENGTH (sizeof(MP_FROZEN_FAKE_DIR)-1)
40+
41+
#define MP_FROZEN_FAKE_DIR_SLASH (MP_FROZEN_FAKE_DIR "/")
42+
#define MP_FROZEN_FAKE_DIR_SLASH_LENGTH (sizeof(MP_FROZEN_FAKE_DIR_SLASH)-1)
43+
44+
// This should match MP_FROZEN_FAKE_DIR.
45+
#define MP_FROZEN_FAKE_DIR_QSTR MP_QSTR__dot_frozen
46+
3747
int mp_find_frozen_module(const char *str, size_t len, void **data);
3848
const char *mp_find_frozen_str(const char *str, size_t str_len, size_t *len);
3949
mp_import_stat_t mp_frozen_stat(const char *str);

unix/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "py/mpstate.h"
4141
#include "py/nlr.h"
4242
#include "py/compile.h"
43+
#include "py/frozenmod.h"
4344
#include "py/runtime.h"
4445
#include "py/builtin.h"
4546
#include "py/repl.h"
@@ -469,7 +470,8 @@ MP_NOINLINE int main_(int argc, char **argv) {
469470
mp_obj_t *path_items;
470471
mp_obj_list_get(mp_sys_path, &path_num, &path_items);
471472
path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
472-
path_items[1] = MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen);
473+
// Frozen modules are in their own pseudo-dir, e.g., ".frozen".
474+
path_items[1] = MP_OBJ_NEW_QSTR(MP_FROZEN_FAKE_DIR_QSTR);
473475
{
474476
char *p = path;
475477
for (mp_uint_t i = builtin_path_count; i < path_num; i++) {

0 commit comments

Comments
 (0)
0