Skip to content

Commit c7830f6

Browse files
committed
py/builtinimport: Call __init__ an modules imported via a weak link.
This is a bit of a hack and should be reworked.
1 parent 8a80644 commit c7830f6

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
@@ -415,6 +415,19 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
415415
}
416416
// found weak linked module
417417
module_obj = el->value;
418+
if (MICROPY_MODULE_BUILTIN_INIT) {
419+
// look for __init__ and call it if it exists
420+
// Note: this code doesn't work fully correctly because it allows the
421+
// __init__ function to be called twice if the module is imported by its
422+
// non-weak-link name. Also, this code is duplicated in objmodule.c.
423+
mp_obj_t dest[2];
424+
mp_load_method_maybe(el->value, MP_QSTR___init__, dest);
425+
if (dest[0] != MP_OBJ_NULL) {
426+
mp_call_method_n_kw(0, 0, dest);
427+
// register module so __init__ is not called again
428+
mp_module_register(mod_name, el->value);
429+
}
430+
}
418431
} else {
419432
no_exist:
420433
#else

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy