@@ -372,29 +372,41 @@ STATIC mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name,
372
372
if (outer_module_obj == MP_OBJ_NULL ) {
373
373
DEBUG_printf ("Searching for top-level module\n" );
374
374
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
+
375
385
// First module in the dotted-name; search for a directory or file
376
386
// 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 );
378
388
379
389
// There's always an implicit "builtin modules" at the end of sys.path.
380
390
if (stat == MP_IMPORT_STAT_NO_EXIST ) {
381
391
// 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 );
383
394
if (module_obj != MP_OBJ_NULL ) {
384
395
return module_obj ;
385
396
}
386
397
387
398
#if MICROPY_MODULE_WEAK_LINKS
388
399
// If "foo" was requested, then try "ufoo" as a built-in.
389
400
// (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 );
391
403
module_obj = mp_module_get_builtin (umodule_name );
392
404
if (module_obj != MP_OBJ_NULL ) {
393
405
return module_obj ;
394
406
}
395
407
#elif MICROPY_PY_SYS
396
408
// 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 ) {
398
410
return MP_OBJ_FROM_PTR (& mp_module_sys );
399
411
}
400
412
#endif
0 commit comments