diff options
author | Damien George <damien.p.george@gmail.com> | 2018-02-20 17:56:58 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2018-02-20 17:56:58 +1100 |
commit | 6e7819ee2ee20ec9f09feb40b68be5973797f874 (patch) | |
tree | 677628d5de061d30f30ba2337af435d9943e73f3 /py/objmodule.c | |
parent | 27fa9881a9294c6a6875856c44101e5b33d27a3b (diff) | |
download | micropython-6e7819ee2ee20ec9f09feb40b68be5973797f874.tar.gz micropython-6e7819ee2ee20ec9f09feb40b68be5973797f874.zip |
py/objmodule: Factor common code for calling __init__ on builtin module.
Diffstat (limited to 'py/objmodule.c')
-rw-r--r-- | py/objmodule.c | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/py/objmodule.c b/py/objmodule.c index f9363e379b..c4aba3a7b4 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 |