diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2018-07-30 12:33:44 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-07-30 12:33:44 -0700 |
| commit | a6d94b68453b8535398ff0b61cd6c4a0c7f77f8b (patch) | |
| tree | 8defd6392975b8145dc11f0cce9c39a4e440b32a /py/objmodule.c | |
| parent | 433a29bb70d51abb84c77a911ced43c6ff14706e (diff) | |
| parent | b1006170f1dcfa3a9499ef31c19c8f20736b136a (diff) | |
Merge pull request #1068 from dhalbert/micropython-25ae98f-merge
Micropython 25ae98f merge
Diffstat (limited to 'py/objmodule.c')
| -rw-r--r-- | py/objmodule.c | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/py/objmodule.c b/py/objmodule.c index e3464ecfa..828f556f3 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -213,13 +213,13 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = { #if MICROPY_PY_USSL { MP_ROM_QSTR(MP_QSTR_ussl), MP_ROM_PTR(&mp_module_ussl) }, #endif -#ifdef MICROPY_PY_LWIP +#if MICROPY_PY_LWIP { MP_ROM_QSTR(MP_QSTR_lwip), MP_ROM_PTR(&mp_module_lwip) }, #endif #if MICROPY_PY_WEBSOCKET { MP_ROM_QSTR(MP_QSTR_websocket), MP_ROM_PTR(&mp_module_websocket) }, #endif -#ifdef MICROPY_PY_WEBREPL +#if MICROPY_PY_WEBREPL { MP_ROM_QSTR(MP_QSTR__webrepl), MP_ROM_PTR(&mp_module_webrepl) }, #endif #if MICROPY_PY_FRAMEBUF @@ -259,17 +259,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 @@ -280,3 +270,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 |
