summaryrefslogtreecommitdiffstatshomepage
path: root/tests/import
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-04-17 16:21:43 +0100
committerDamien George <damien.p.george@gmail.com>2014-04-17 16:21:43 +0100
commit5cd0b2227f1c36584129cdce86dd20952b45581c (patch)
treebef451353a1a0f0a5050fbbd0df8eb550b728278 /tests/import
parentd7a4b6903931f9e42686f4c4f3c3c8e9485c143f (diff)
downloadmicropython-5cd0b2227f1c36584129cdce86dd20952b45581c.tar.gz
micropython-5cd0b2227f1c36584129cdce86dd20952b45581c.zip
tests: Split out those tests requiring float and import.
Tests in basics (which should probably be renamed to core) should not rely on float, or import any non-built-in files. This way these tests can be run when those features are not available. All test in basics now pass on the pyboard using stmhal port, except for string-repr which has some issues with character hex printing.
Diffstat (limited to 'tests/import')
-rw-r--r--tests/import/import-pkg1.py11
-rw-r--r--tests/import/import-pkg2.py18
-rw-r--r--tests/import/import-pkg3.py6
-rw-r--r--tests/import/import-pkg4.py2
-rw-r--r--tests/import/import-pkg5.py6
-rw-r--r--tests/import/import1a.py2
-rw-r--r--tests/import/import1b.py4
-rw-r--r--tests/import/import2a.py2
-rw-r--r--tests/import/import3a.py2
-rw-r--r--tests/import/pkg/__init__.py0
-rw-r--r--tests/import/pkg/mod.py2
-rw-r--r--tests/import/pkg2/__init__.py1
-rw-r--r--tests/import/pkg2/mod1.py1
-rw-r--r--tests/import/pkg2/mod2.py1
-rw-r--r--tests/import/pkg3/__init__.py1
-rw-r--r--tests/import/pkg3/mod1.py2
-rw-r--r--tests/import/pkg3/mod2.py5
-rw-r--r--tests/import/pkg3/subpkg1/__init__.py1
-rw-r--r--tests/import/pkg3/subpkg1/mod1.py2
-rw-r--r--tests/import/try-module.py15
20 files changed, 84 insertions, 0 deletions
diff --git a/tests/import/import-pkg1.py b/tests/import/import-pkg1.py
new file mode 100644
index 0000000000..8cd77af79d
--- /dev/null
+++ b/tests/import/import-pkg1.py
@@ -0,0 +1,11 @@
+import pkg.mod
+
+print(pkg.__name__)
+print(pkg.mod.__name__)
+print(pkg.mod.foo())
+
+# Import 2nd time, must be same module objects
+pkg_ = __import__("pkg.mod")
+print(pkg_ is not pkg.mod)
+print(pkg_ is pkg)
+print(pkg_.mod is pkg.mod)
diff --git a/tests/import/import-pkg2.py b/tests/import/import-pkg2.py
new file mode 100644
index 0000000000..2e9f34121b
--- /dev/null
+++ b/tests/import/import-pkg2.py
@@ -0,0 +1,18 @@
+from pkg.mod import foo
+
+try:
+ pkg
+except NameError:
+ print("NameError")
+try:
+ pkg.mod
+except NameError:
+ print("NameError")
+print(foo())
+
+# Import few times, must be same module objects
+mod_1 = __import__("pkg.mod", None, None, ("foo",))
+mod_2 = __import__("pkg.mod", None, None, ("foo",))
+print(mod_1 is mod_2)
+print(mod_1.foo is mod_2.foo)
+print(foo is mod_1.foo)
diff --git a/tests/import/import-pkg3.py b/tests/import/import-pkg3.py
new file mode 100644
index 0000000000..0ee885b220
--- /dev/null
+++ b/tests/import/import-pkg3.py
@@ -0,0 +1,6 @@
+from pkg import mod
+
+print(mod.foo())
+
+import pkg.mod
+print(mod is pkg.mod)
diff --git a/tests/import/import-pkg4.py b/tests/import/import-pkg4.py
new file mode 100644
index 0000000000..90b6f2e0ee
--- /dev/null
+++ b/tests/import/import-pkg4.py
@@ -0,0 +1,2 @@
+# Testing that "recursive" imports (pkg2/__init__.py imports from pkg2) work
+import pkg2
diff --git a/tests/import/import-pkg5.py b/tests/import/import-pkg5.py
new file mode 100644
index 0000000000..aa74bb45f0
--- /dev/null
+++ b/tests/import/import-pkg5.py
@@ -0,0 +1,6 @@
+# This tests relative imports as used in pkg3
+import pkg3
+import pkg3.mod1
+import pkg3.subpkg1.mod1
+
+pkg3.subpkg1.mod1.foo()
diff --git a/tests/import/import1a.py b/tests/import/import1a.py
new file mode 100644
index 0000000000..16b2d4d30f
--- /dev/null
+++ b/tests/import/import1a.py
@@ -0,0 +1,2 @@
+import import1b
+print(import1b.var)
diff --git a/tests/import/import1b.py b/tests/import/import1b.py
new file mode 100644
index 0000000000..be74eca094
--- /dev/null
+++ b/tests/import/import1b.py
@@ -0,0 +1,4 @@
+var = 123
+
+def throw():
+ raise ValueError
diff --git a/tests/import/import2a.py b/tests/import/import2a.py
new file mode 100644
index 0000000000..ce32b10b1b
--- /dev/null
+++ b/tests/import/import2a.py
@@ -0,0 +1,2 @@
+from import1b import var
+print(var)
diff --git a/tests/import/import3a.py b/tests/import/import3a.py
new file mode 100644
index 0000000000..2e9d41f71d
--- /dev/null
+++ b/tests/import/import3a.py
@@ -0,0 +1,2 @@
+from import1b import *
+print(var)
diff --git a/tests/import/pkg/__init__.py b/tests/import/pkg/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/import/pkg/__init__.py
diff --git a/tests/import/pkg/mod.py b/tests/import/pkg/mod.py
new file mode 100644
index 0000000000..9e67bdd291
--- /dev/null
+++ b/tests/import/pkg/mod.py
@@ -0,0 +1,2 @@
+def foo():
+ return 42
diff --git a/tests/import/pkg2/__init__.py b/tests/import/pkg2/__init__.py
new file mode 100644
index 0000000000..101ac7d400
--- /dev/null
+++ b/tests/import/pkg2/__init__.py
@@ -0,0 +1 @@
+from pkg2 import mod1
diff --git a/tests/import/pkg2/mod1.py b/tests/import/pkg2/mod1.py
new file mode 100644
index 0000000000..03754a45f6
--- /dev/null
+++ b/tests/import/pkg2/mod1.py
@@ -0,0 +1 @@
+from pkg2 import mod2
diff --git a/tests/import/pkg2/mod2.py b/tests/import/pkg2/mod2.py
new file mode 100644
index 0000000000..97dadcde46
--- /dev/null
+++ b/tests/import/pkg2/mod2.py
@@ -0,0 +1 @@
+print("in mod2")
diff --git a/tests/import/pkg3/__init__.py b/tests/import/pkg3/__init__.py
new file mode 100644
index 0000000000..8b92fa9967
--- /dev/null
+++ b/tests/import/pkg3/__init__.py
@@ -0,0 +1 @@
+print("pkg __name__:", __name__)
diff --git a/tests/import/pkg3/mod1.py b/tests/import/pkg3/mod1.py
new file mode 100644
index 0000000000..28a0f5bf10
--- /dev/null
+++ b/tests/import/pkg3/mod1.py
@@ -0,0 +1,2 @@
+print("mod1 __name__:", __name__)
+from . import mod2
diff --git a/tests/import/pkg3/mod2.py b/tests/import/pkg3/mod2.py
new file mode 100644
index 0000000000..67f43bad52
--- /dev/null
+++ b/tests/import/pkg3/mod2.py
@@ -0,0 +1,5 @@
+print("mod2 __name__:", __name__)
+print("in mod2")
+
+def foo():
+ print("mod2.foo()")
diff --git a/tests/import/pkg3/subpkg1/__init__.py b/tests/import/pkg3/subpkg1/__init__.py
new file mode 100644
index 0000000000..72b5423958
--- /dev/null
+++ b/tests/import/pkg3/subpkg1/__init__.py
@@ -0,0 +1 @@
+print("subpkg1 __name__:", __name__)
diff --git a/tests/import/pkg3/subpkg1/mod1.py b/tests/import/pkg3/subpkg1/mod1.py
new file mode 100644
index 0000000000..7a2ae44b54
--- /dev/null
+++ b/tests/import/pkg3/subpkg1/mod1.py
@@ -0,0 +1,2 @@
+print("subpkg1.mod1 __name__:", __name__)
+from ..mod2 import foo
diff --git a/tests/import/try-module.py b/tests/import/try-module.py
new file mode 100644
index 0000000000..03a9db15b5
--- /dev/null
+++ b/tests/import/try-module.py
@@ -0,0 +1,15 @@
+# Regression test for #290 - throwing exception in another module led to
+# its namespace stick and namespace of current module not coming back.
+import import1b
+
+def func1():
+ print('func1')
+
+def func2():
+ try:
+ import1b.throw()
+ except ValueError:
+ pass
+ func1()
+
+func2()