Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 22bd901

Browse files
committed
py/builtinimport: Call __init__ for modules imported via a weak link.
This is a bit of a clumsy way of doing it but solves the issue of __init__ not running when a module is imported via its weak-link name. Ideally a better solution would be found.
1 parent 7d25a19 commit 22bd901

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

py/builtinimport.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,19 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
388388
}
389389
// found weak linked module
390390
module_obj = el->value;
391+
if (MICROPY_MODULE_BUILTIN_INIT) {
392+
// look for __init__ and call it if it exists
393+
// Note: this code doesn't work fully correctly because it allows the
394+
// __init__ function to be called twice if the module is imported by its
395+
// non-weak-link name. Also, this code is duplicated in objmodule.c.
396+
mp_obj_t dest[2];
397+
mp_load_method_maybe(el->value, MP_QSTR___init__, dest);
398+
if (dest[0] != MP_OBJ_NULL) {
399+
mp_call_method_n_kw(0, 0, dest);
400+
// register module so __init__ is not called again
401+
mp_module_register(mod_name, el->value);
402+
}
403+
}
391404
} else {
392405
no_exist:
393406
#else

0 commit comments

Comments
 (0)