builtinimport: Set __path__ attribute ASAP as it's clear we have a package.
This helps with handling "recursive" imports in sane manner, for example when foo/__init__.py has something like "from foo import submod".
This commit is contained in:
parent
69f1867da5
commit
2ff3d9d0b2
|
@ -218,6 +218,7 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
|
||||||
|
|
||||||
if (stat == MP_IMPORT_STAT_DIR) {
|
if (stat == MP_IMPORT_STAT_DIR) {
|
||||||
DEBUG_printf("%s is dir\n", vstr_str(&path));
|
DEBUG_printf("%s is dir\n", vstr_str(&path));
|
||||||
|
mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str((byte*)vstr_str(&path), vstr_len(&path), false));
|
||||||
vstr_add_char(&path, PATH_SEP_CHAR);
|
vstr_add_char(&path, PATH_SEP_CHAR);
|
||||||
vstr_add_str(&path, "__init__.py");
|
vstr_add_str(&path, "__init__.py");
|
||||||
if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) {
|
if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) {
|
||||||
|
@ -230,7 +231,6 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
|
||||||
vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
|
vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
|
||||||
// https://docs.python.org/3.3/reference/import.html
|
// https://docs.python.org/3.3/reference/import.html
|
||||||
// "Specifically, any module that contains a __path__ attribute is considered a package."
|
// "Specifically, any module that contains a __path__ attribute is considered a package."
|
||||||
mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str((byte*)vstr_str(&path), vstr_len(&path), false));
|
|
||||||
} else { // MP_IMPORT_STAT_FILE
|
} else { // MP_IMPORT_STAT_FILE
|
||||||
do_load(module_obj, &path);
|
do_load(module_obj, &path);
|
||||||
// TODO: We cannot just break here, at the very least, we must execute
|
// TODO: We cannot just break here, at the very least, we must execute
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
# Testing that "recursive" imports (pkg2/__init__.py imports from pkg2) work
|
||||||
|
import pkg2
|
|
@ -0,0 +1 @@
|
||||||
|
from pkg2 import mod1
|
|
@ -0,0 +1 @@
|
||||||
|
from pkg2 import mod2
|
|
@ -0,0 +1 @@
|
||||||
|
print("in mod2")
|
Loading…
Reference in New Issue