diff options
| author | Jeff Epler <jeff@adafruit.com> | 2020-03-05 14:28:46 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-05 14:28:46 -0600 |
| commit | 4fa90261a37f0f729b52289cf54492ec7108e16b (patch) | |
| tree | 6af84444989778e70b8d327d636d88df04c40304 /py | |
| parent | e81930fb9e9cc9df55e7fad7726dcedbf7dcdeff (diff) | |
| parent | 274cb597b0cd2a12b82d6efe2c40fb76402f8644 (diff) | |
Merge pull request #2657 from tannewt/builtin_package
Support importing native modules in native packages.
Diffstat (limited to 'py')
| -rw-r--r-- | py/builtinimport.c | 30 | ||||
| -rwxr-xr-x | py/gc_long_lived.c | 2 | ||||
| -rw-r--r-- | py/objmodule.c | 7 |
3 files changed, 28 insertions, 11 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c index 6ed0a7594..2be779c6c 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -400,21 +400,31 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { DEBUG_printf("Current path: %.*s\n", vstr_len(&path), vstr_str(&path)); if (stat == MP_IMPORT_STAT_NO_EXIST) { - #if MICROPY_MODULE_WEAK_LINKS - // check if there is a weak link to this module - if (i == mod_len) { - mp_map_elem_t *el = mp_map_lookup((mp_map_t*)&mp_builtin_module_weak_links_map, MP_OBJ_NEW_QSTR(mod_name), MP_MAP_LOOKUP); + // This is just the module name after the previous . + qstr current_module_name = qstr_from_strn(mod_str + last, i - last); + mp_map_elem_t *el = NULL; + if (outer_module_obj == MP_OBJ_NULL) { + el = mp_map_lookup((mp_map_t*)&mp_builtin_module_map, + MP_OBJ_NEW_QSTR(current_module_name), + MP_MAP_LOOKUP); + #if MICROPY_MODULE_WEAK_LINKS + // check if there is a weak link to this module if (el == NULL) { - goto no_exist; + el = mp_map_lookup((mp_map_t*)&mp_builtin_module_weak_links_map, + MP_OBJ_NEW_QSTR(current_module_name), + MP_MAP_LOOKUP); } - // found weak linked module + #endif + } else { + el = mp_map_lookup(&((mp_obj_module_t*) outer_module_obj)->globals->map, + MP_OBJ_NEW_QSTR(current_module_name), + MP_MAP_LOOKUP); + } + + if (el != NULL && MP_OBJ_IS_TYPE(el->value, &mp_type_module)) { module_obj = el->value; mp_module_call_init(mod_name, module_obj); } else { - no_exist: - #else - { - #endif // couldn't find the file, so fail if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_raise_ImportError(translate("module not found")); diff --git a/py/gc_long_lived.c b/py/gc_long_lived.c index 01c22a7af..0e94390e9 100755 --- a/py/gc_long_lived.c +++ b/py/gc_long_lived.c @@ -89,7 +89,7 @@ mp_obj_dict_t *make_dict_long_lived(mp_obj_dict_t *dict, uint8_t max_depth) { #ifndef MICROPY_ENABLE_GC return dict; #endif - if (dict == NULL || max_depth == 0 || dict == &MP_STATE_VM(dict_main)) { + if (dict == NULL || max_depth == 0 || dict == &MP_STATE_VM(dict_main) || dict->map.is_fixed) { return dict; } // Don't recurse unnecessarily. Return immediately if we've already seen this dict. diff --git a/py/objmodule.c b/py/objmodule.c index c7b080300..7405a6919 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -69,6 +69,13 @@ STATIC void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { // delete/store attribute mp_obj_dict_t *dict = self->globals; if (dict->map.is_fixed) { + mp_map_elem_t *elem = mp_map_lookup(&dict->map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP); + // Return success if the given value is already in the dictionary. This is the case for + // native packages with native submodules. + if (elem != NULL && elem->value == dest[1]) { + dest[0] = MP_OBJ_NULL; // indicate success + return; + } else #if MICROPY_CAN_OVERRIDE_BUILTINS if (dict == &mp_module_builtins_globals) { if (MP_STATE_VM(mp_module_builtins_override_dict) == NULL) { |
