Skip to content

Commit 0d02874

Browse files
committed
py: Initialise loaded_module map in rt_init.
STM port crashes without this re-init. There should not be any state in the core py/ code that relies on pre-initialised data.
1 parent f64086f commit 0d02874

3 files changed

Lines changed: 11 additions & 8 deletions

File tree

py/objmodule.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ typedef struct _mp_obj_module_t {
1717
mp_map_t *globals;
1818
} mp_obj_module_t;
1919

20-
// TODO: expose as sys.modules
21-
static mp_map_t *loaded_modules;
22-
2320
static void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
2421
mp_obj_module_t *self = self_in;
2522
print(env, "<module '%s' from '-unknown-file-'>", qstr_str(self->name));
@@ -49,10 +46,7 @@ const mp_obj_type_t module_type = {
4946
};
5047

5148
mp_obj_t mp_obj_new_module(qstr module_name) {
52-
if (loaded_modules == NULL) {
53-
loaded_modules = mp_map_new(1);
54-
}
55-
mp_map_elem_t *el = mp_map_lookup(loaded_modules, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
49+
mp_map_elem_t *el = mp_map_lookup(rt_loaded_modules_get(), MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
5650
// We could error out if module already exists, but let C extensions
5751
// add new members to existing modules.
5852
if (el->value != MP_OBJ_NULL) {
@@ -69,7 +63,7 @@ mp_obj_t mp_obj_new_module(qstr module_name) {
6963
}
7064

7165
mp_obj_t mp_obj_module_get(qstr module_name) {
72-
mp_map_elem_t *el = mp_map_lookup(loaded_modules, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
66+
mp_map_elem_t *el = mp_map_lookup(rt_loaded_modules_get(), MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
7367
if (el == NULL) {
7468
return NULL;
7569
}

py/runtime.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
static mp_map_t *map_locals;
3535
static mp_map_t *map_globals;
3636
static mp_map_t map_builtins;
37+
static mp_map_t map_loaded_modules; // TODO: expose as sys.modules
3738

3839
typedef enum {
3940
MP_CODE_NONE,
@@ -83,6 +84,9 @@ void rt_init(void) {
8384
// init built-in hash table
8485
mp_map_init(&map_builtins, 3);
8586

87+
// init loaded modules table
88+
mp_map_init(&map_loaded_modules, 3);
89+
8690
// built-in exceptions (TODO, make these proper classes)
8791
mp_map_add_qstr(&map_builtins, MP_QSTR_AttributeError, mp_obj_new_exception(MP_QSTR_AttributeError));
8892
mp_map_add_qstr(&map_builtins, MP_QSTR_IndexError, mp_obj_new_exception(MP_QSTR_IndexError));
@@ -953,6 +957,10 @@ void rt_globals_set(mp_map_t *m) {
953957
map_globals = m;
954958
}
955959

960+
mp_map_t *rt_loaded_modules_get(void) {
961+
return &map_loaded_modules;
962+
}
963+
956964
// these must correspond to the respective enum
957965
void *const rt_fun_table[RT_F_NUMBER_OF] = {
958966
rt_load_const_dec,

py/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ struct _mp_map_t *rt_locals_get(void);
4343
void rt_locals_set(struct _mp_map_t *m);
4444
struct _mp_map_t *rt_globals_get(void);
4545
void rt_globals_set(struct _mp_map_t *m);
46+
struct _mp_map_t *rt_loaded_modules_get(void);

0 commit comments

Comments
 (0)