8000 py/builtinimport: Bypass filesystem for u-prefix modules. · micropython/micropython@32a4260 · GitHub
[go: up one dir, main page]

Skip to content

Commit 32a4260

Browse files
committed
py/builtinimport: Bypass filesystem for u-prefix modules.
This restores the old behavior for u-prefixed builtin modules, so that it never attempts to find them on the filesystem. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent 6b88693 commit 32a4260

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

py/builtinimport.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,29 +372,41 @@ STATIC mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name,
372372
if (outer_module_obj == MP_OBJ_NULL) {
373373
DEBUG_printf("Searching for top-level module\n");
374374

375+
// Always prefer an exact match for any u-prefixed built-in.
376+
// e.g. `import utime` bypasses the filesystem.
377+
const char *level_mod_name_str = qstr_str(level_mod_name);
378+
if (level_mod_name_str[0] == 'u') {
379+
module_obj = mp_module_get_builtin(level_mod_name);
380+
if (module_obj != MP_OBJ_NULL) {
381+
return module_obj;
382+
}
383+
}
384+
375385
// First module in the dotted-name; search for a directory or file
376386
// relative to all the locations in sys.path.
377-
stat = stat_top_level(full_mod_name, &path);
387+
stat = stat_top_level(level_mod_name, &path);
378388

379389
// There's always an implicit "builtin modules" at the end of sys.path.
380390
if (stat == MP_IMPORT_STAT_NO_EXIST) {
381391
// Direct match of module name for builtin.
382-
module_obj = mp_module_get_builtin(full_mod_name);
392+
// e.g. `import pyb` will use builtin unless pyb exists on the filesystem.
393+
module_obj = mp_module_get_builtin(level_mod_name);
383394
if (module_obj != MP_OBJ_NULL) {
384395
return module_obj;
385396
}
386397

387398
#if MICROPY_MODULE_WEAK_LINKS
388399
// If "foo" was requested, then try "ufoo" as a built-in.
389400
// (This feature was formerly known as "weak links").
390-
qstr umodule_name = make_weak_link_name(&path, full_mod_name);
401+
// e.g. `import time` will use builtin time, unless time exists on the filesystem.
402+
qstr umodule_name = make_weak_link_name(&path, level_mod_name);
391403
module_obj = mp_module_get_builtin(umodule_name);
392404
if (module_obj != MP_OBJ_NULL) {
393405
return module_obj;
394406
}
395407
#elif MICROPY_PY_SYS
396408
// Special handling to make `import sys` work even if weak links aren't enabled.
397-
if (full_mod_name == MP_QSTR_sys) {
409+
if (level_mod_name == MP_QSTR_sys) {
398410
return MP_OBJ_FROM_PTR(&mp_module_sys);
399411
}
400412
#endif

0 commit comments

Comments
 (0)
0