diff options
author | Damien George <damien.p.george@gmail.com> | 2017-03-06 16:46:34 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-12-13 14:48:53 +1100 |
commit | f1c9e7760d91cb141b0f03ee348b3fe36d2cf450 (patch) | |
tree | ac45d76a8e50b29f9d04371152136d81d2bf09ab /py/builtinimport.c | |
parent | 479392a56e6edd8449e594c0e5c1e8f5176411ab (diff) |
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.
Diffstat (limited to 'py/builtinimport.c')
-rw-r--r-- | py/builtinimport.c | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c index 2157902c9..97c1789f8 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -389,6 +389,19 @@ 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); + } + } } else { no_exist: #else |