From 0d028743aa12ef935b27fdbaf0a5ad3ca62d7628 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Wed, 22 Jan 2014 23:59:20 +0000
Subject: [PATCH] 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.
---
 py/objmodule.c | 10 ++--------
 py/runtime.c   |  8 ++++++++
 py/runtime.h   |  1 +
 3 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/py/objmodule.c b/py/objmodule.c
index 749d345bc..f8a8d2620 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 7937a65b8..d6b2bf782 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 770cac8a6..d62d1f86d 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);
-- 
GitLab