summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-12 02:44:47 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-12 02:55:18 +0300
commit2ff3d9d0b2f53fe4a9e2fcb1ab2cff045211d78b (patch)
treec731619ee85eb42d92f535ff8a980904101e1be0
parent69f1867da59c812776db1fa9ef7bb13ba2c9674a (diff)
downloadmicropython-2ff3d9d0b2f53fe4a9e2fcb1ab2cff045211d78b.tar.gz
micropython-2ff3d9d0b2f53fe4a9e2fcb1ab2cff045211d78b.zip
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".
-rw-r--r--py/builtinimport.c2
-rw-r--r--tests/basics/import-pkg4.py2
-rw-r--r--tests/basics/pkg2/__init__.py1
-rw-r--r--tests/basics/pkg2/mod1.py1
-rw-r--r--tests/basics/pkg2/mod2.py1
5 files changed, 6 insertions, 1 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c
index 501ced7644..9eede65cd9 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -218,6 +218,7 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
if (stat == MP_IMPORT_STAT_DIR) {
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_str(&path, "__init__.py");
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
// https://docs.python.org/3.3/reference/import.html
// "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
do_load(module_obj, &path);
// TODO: We cannot just break here, at the very least, we must execute
diff --git a/tests/basics/import-pkg4.py b/tests/basics/import-pkg4.py
new file mode 100644
index 0000000000..90b6f2e0ee
--- /dev/null
+++ b/tests/basics/import-pkg4.py
@@ -0,0 +1,2 @@
+# Testing that "recursive" imports (pkg2/__init__.py imports from pkg2) work
+import pkg2
diff --git a/tests/basics/pkg2/__init__.py b/tests/basics/pkg2/__init__.py
new file mode 100644
index 0000000000..101ac7d400
--- /dev/null
+++ b/tests/basics/pkg2/__init__.py
@@ -0,0 +1 @@
+from pkg2 import mod1
diff --git a/tests/basics/pkg2/mod1.py b/tests/basics/pkg2/mod1.py
new file mode 100644
index 0000000000..03754a45f6
--- /dev/null
+++ b/tests/basics/pkg2/mod1.py
@@ -0,0 +1 @@
+from pkg2 import mod2
diff --git a/tests/basics/pkg2/mod2.py b/tests/basics/pkg2/mod2.py
new file mode 100644
index 0000000000..97dadcde46
--- /dev/null
+++ b/tests/basics/pkg2/mod2.py
@@ -0,0 +1 @@
+print("in mod2")