Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion py/builtinimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,26 @@ static mp_import_stat_t stat_module(vstr_t *path) {
mp_import_stat_t stat = stat_path(path);
DEBUG_printf("stat %s: %d\n", vstr_str(path), stat);
if (stat == MP_IMPORT_STAT_DIR) {
return stat;
// CIRCUITPY-CHANGE: match CPython import precedence. A regular
// package (directory with __init__.py/.mpy) takes precedence, then a
// sibling .py/.mpy module, and only then a namespace package
// (directory without __init__). See
// https://docs.python.org/3/reference/import.html#regular-packages
size_t orig_len = path->len;
vstr_add_str(path, PATH_SEP_CHAR "__init__.py");
mp_import_stat_t init_stat = stat_file_py_or_mpy(path);
path->len = orig_len;
if (init_stat == MP_IMPORT_STAT_FILE) {
return MP_IMPORT_STAT_DIR;
}

vstr_add_str(path, ".py");
mp_import_stat_t file_stat = stat_file_py_or_mpy(path);
if (file_stat == MP_IMPORT_STAT_FILE) {
return file_stat;
}
path->len = orig_len;
return MP_IMPORT_STAT_DIR;
}

// Not a directory, add .py and try as a file.
Expand Down
7 changes: 7 additions & 0 deletions tests/import/import_shared_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://github.com/adafruit/circuitpython/issues/10614
# When a directory `shared_name/` (no __init__.py) and a module `shared_name.py`
# share a name, `import shared_name` must pick the .py module per PEP 420
# precedence: regular package > module > namespace package.
import shared_name

print("done")
1 change: 1 addition & 0 deletions tests/import/shared_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("hello shared_name.py")
Empty file.
Loading