summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/builtinimport.c14
-rw-r--r--py/objmodule.c28
-rw-r--r--py/objmodule.h9
3 files changed, 27 insertions, 24 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c
index 97c1789f8..19bc9fc95 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -389,19 +389,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
}
// found weak linked module
module_obj = el->value;
- if (MICROPY_MODULE_BUILTIN_INIT) {
- // look for __init__ and call it if it exists
- // Note: this code doesn't work fully correctly because it allows the
- // __init__ function to be called twice if the module is imported by its
- // non-weak-link name. Also, this code is duplicated in objmodule.c.
- mp_obj_t dest[2];
- mp_load_method_maybe(el->value, MP_QSTR___init__, dest);
- if (dest[0] != MP_OBJ_NULL) {
- mp_call_method_n_kw(0, 0, dest);
- // register module so __init__ is not called again
- mp_module_register(mod_name, el->value);
- }
- }
+ mp_module_call_init(mod_name, module_obj);
} else {
no_exist:
#else
diff --git a/py/objmodule.c b/py/objmodule.c
index f9363e379..c4aba3a7b 100644
--- a/py/objmodule.c
+++ b/py/objmodule.c
@@ -247,17 +247,7 @@ mp_obj_t mp_module_get(qstr module_name) {
if (el == NULL) {
return MP_OBJ_NULL;
}
-
- if (MICROPY_MODULE_BUILTIN_INIT) {
- // look for __init__ and call it if it exists
- mp_obj_t dest[2];
- mp_load_method_maybe(el->value, MP_QSTR___init__, dest);
- if (dest[0] != MP_OBJ_NULL) {
- mp_call_method_n_kw(0, 0, dest);
- // register module so __init__ is not called again
- mp_module_register(module_name, el->value);
- }
- }
+ mp_module_call_init(module_name, el->value);
}
// module found, return it
@@ -268,3 +258,19 @@ void mp_module_register(qstr qst, mp_obj_t module) {
mp_map_t *mp_loaded_modules_map = &MP_STATE_VM(mp_loaded_modules_dict).map;
mp_map_lookup(mp_loaded_modules_map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = module;
}
+
+#if MICROPY_MODULE_BUILTIN_INIT
+void mp_module_call_init(qstr module_name, mp_obj_t module_obj) {
+ // Look for __init__ and call it if it exists
+ mp_obj_t dest[2];
+ mp_load_method_maybe(module_obj, MP_QSTR___init__, dest);
+ if (dest[0] != MP_OBJ_NULL) {
+ mp_call_method_n_kw(0, 0, dest);
+ // Register module so __init__ is not called again.
+ // If a module can be referenced by more than one name (eg due to weak links)
+ // then __init__ will still be called for each distinct import, and it's then
+ // up to the particular module to make sure it's __init__ code only runs once.
+ mp_module_register(module_name, module_obj);
+ }
+}
+#endif
diff --git a/py/objmodule.h b/py/objmodule.h
index b5c07dc33..b7702ec50 100644
--- a/py/objmodule.h
+++ b/py/objmodule.h
@@ -34,4 +34,13 @@ extern const mp_map_t mp_builtin_module_weak_links_map;
mp_obj_t mp_module_get(qstr module_name);
void mp_module_register(qstr qstr, mp_obj_t module);
+#if MICROPY_MODULE_BUILTIN_INIT
+void mp_module_call_init(qstr module_name, mp_obj_t module_obj);
+#else
+static inline void mp_module_call_init(qstr module_name, mp_obj_t module_obj) {
+ (void)module_name;
+ (void)module_obj;
+}
+#endif
+
#endif // MICROPY_INCLUDED_PY_OBJMODULE_H