summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-22 23:59:20 +0000
committerDamien George <damien.p.george@gmail.com>2014-01-22 23:59:20 +0000
commit0d028743aa12ef935b27fdbaf0a5ad3ca62d7628 (patch)
treefef0f48e45badbc8bdab625721f314b01c77288d
parentf64086f80f1493c7f5009e99f4bd1fba47332cd3 (diff)
downloadmicropython-0d028743aa12ef935b27fdbaf0a5ad3ca62d7628.tar.gz
micropython-0d028743aa12ef935b27fdbaf0a5ad3ca62d7628.zip
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.
-rw-r--r--py/objmodule.c10
-rw-r--r--py/runtime.c8
-rw-r--r--py/runtime.h1
3 files changed, 11 insertions, 8 deletions
diff --git a/py/objmodule.c b/py/objmodule.c
index 749d345bcf..f8a8d2620d 100644
--- a/py/objmodule.c
+++ b/py/objmodule.c
@@ -17,9 +17,6 @@ typedef struct _mp_obj_module_t {
mp_map_t *globals;
} mp_obj_module_t;
-// TODO: expose as sys.modules
-static mp_map_t *loaded_modules;
-
static void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
mp_obj_module_t *self = self_in;
print(env, "<module '%s' from '-unknown-file-'>", qstr_str(self->name));
@@ -49,10 +46,7 @@ const mp_obj_type_t module_type = {
};
mp_obj_t mp_obj_new_module(qstr module_name) {
- if (loaded_modules == NULL) {
- loaded_modules = mp_map_new(1);
- }
- mp_map_elem_t *el = mp_map_lookup(loaded_modules, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
+ 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);
// We could error out if module already exists, but let C extensions
// add new members to existing modules.
if (el->value != MP_OBJ_NULL) {
@@ -69,7 +63,7 @@ mp_obj_t mp_obj_new_module(qstr module_name) {
}
mp_obj_t mp_obj_module_get(qstr module_name) {
- mp_map_elem_t *el = mp_map_lookup(loaded_modules, MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
+ mp_map_elem_t *el = mp_map_lookup(rt_loaded_modules_get(), MP_OBJ_NEW_QSTR(module_name), MP_MAP_LOOKUP);
if (el == NULL) {
return NULL;
}
diff --git a/py/runtime.c b/py/runtime.c
index 7937a65b83..d6b2bf7826 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -34,6 +34,7 @@
static mp_map_t *map_locals;
static mp_map_t *map_globals;
static mp_map_t map_builtins;
+static mp_map_t map_loaded_modules; // TODO: expose as sys.modules
typedef enum {
MP_CODE_NONE,
@@ -83,6 +84,9 @@ void rt_init(void) {
// init built-in hash table
mp_map_init(&map_builtins, 3);
+ // init loaded modules table
+ mp_map_init(&map_loaded_modules, 3);
+
// built-in exceptions (TODO, make these proper classes)
mp_map_add_qstr(&map_builtins, MP_QSTR_AttributeError, mp_obj_new_exception(MP_QSTR_AttributeError));
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) {
map_globals = m;
}
+mp_map_t *rt_loaded_modules_get(void) {
+ return &map_loaded_modules;
+}
+
// these must correspond to the respective enum
void *const rt_fun_table[RT_F_NUMBER_OF] = {
rt_load_const_dec,
diff --git a/py/runtime.h b/py/runtime.h
index 770cac8a65..d62d1f86d6 100644
--- a/py/runtime.h
+++ b/py/runtime.h
@@ -43,3 +43,4 @@ struct _mp_map_t *rt_locals_get(void);
void rt_locals_set(struct _mp_map_t *m);
struct _mp_map_t *rt_globals_get(void);
void rt_globals_set(struct _mp_map_t *m);
+struct _mp_map_t *rt_loaded_modules_get(void);