micropython: add micropython component
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
# test calling builtin import function
|
||||
|
||||
# basic test
|
||||
__import__("builtins")
|
||||
|
||||
# first arg should be a string
|
||||
try:
|
||||
__import__(1)
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
|
||||
# module name should not be empty
|
||||
try:
|
||||
__import__("")
|
||||
except ValueError:
|
||||
print("ValueError")
|
||||
|
||||
# level argument should be non-negative
|
||||
try:
|
||||
__import__("xyz", None, None, None, -1)
|
||||
except ValueError:
|
||||
print("ValueError")
|
11
components/language/micropython/tests/import/gen_context.py
Normal file
11
components/language/micropython/tests/import/gen_context.py
Normal file
@@ -0,0 +1,11 @@
|
||||
import gen_context2
|
||||
|
||||
GLOBAL = "GLOBAL"
|
||||
|
||||
|
||||
def gen():
|
||||
print(GLOBAL)
|
||||
yield 1
|
||||
|
||||
|
||||
gen_context2.call(gen())
|
@@ -0,0 +1,2 @@
|
||||
def call(g):
|
||||
next(g)
|
3
components/language/micropython/tests/import/import1a.py
Normal file
3
components/language/micropython/tests/import/import1a.py
Normal file
@@ -0,0 +1,3 @@
|
||||
import import1b
|
||||
|
||||
print(import1b.var)
|
5
components/language/micropython/tests/import/import1b.py
Normal file
5
components/language/micropython/tests/import/import1b.py
Normal file
@@ -0,0 +1,5 @@
|
||||
var = 123
|
||||
|
||||
|
||||
def throw():
|
||||
raise ValueError
|
7
components/language/micropython/tests/import/import2a.py
Normal file
7
components/language/micropython/tests/import/import2a.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from import1b import var
|
||||
|
||||
print(var)
|
||||
|
||||
from import1b import var as var2
|
||||
|
||||
print(var2)
|
3
components/language/micropython/tests/import/import3a.py
Normal file
3
components/language/micropython/tests/import/import3a.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from import1b import *
|
||||
|
||||
print(var)
|
@@ -0,0 +1,3 @@
|
||||
import import1b
|
||||
|
||||
print(import1b.__file__)
|
@@ -0,0 +1 @@
|
||||
from import_long_dyn2 import *
|
@@ -0,0 +1 @@
|
||||
globals()["long_long_very_long_long_name"] = 1
|
@@ -0,0 +1,21 @@
|
||||
# test overriding __import__ combined with importing from the filesystem
|
||||
|
||||
|
||||
def custom_import(name, globals, locals, fromlist, level):
|
||||
print("import", name, fromlist, level)
|
||||
|
||||
class M:
|
||||
var = 456
|
||||
|
||||
return M
|
||||
|
||||
|
||||
orig_import = __import__
|
||||
try:
|
||||
__import__("builtins").__import__ = custom_import
|
||||
except AttributeError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
# import1a will be done via normal import which will import1b via our custom import
|
||||
orig_import("import1a")
|
@@ -0,0 +1,2 @@
|
||||
import import1b None 0
|
||||
456
|
17
components/language/micropython/tests/import/import_pkg1.py
Normal file
17
components/language/micropython/tests/import/import_pkg1.py
Normal file
@@ -0,0 +1,17 @@
|
||||
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)
|
||||
|
||||
# import using "as"
|
||||
import pkg.mod as mm
|
||||
|
||||
print(mm is pkg.mod)
|
||||
print(mm.foo())
|
18
components/language/micropython/tests/import/import_pkg2.py
Normal file
18
components/language/micropython/tests/import/import_pkg2.py
Normal file
@@ -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)
|
@@ -0,0 +1,7 @@
|
||||
from pkg import mod
|
||||
|
||||
print(mod.foo())
|
||||
|
||||
import pkg.mod
|
||||
|
||||
print(mod is pkg.mod)
|
@@ -0,0 +1,2 @@
|
||||
# Testing that "recursive" imports (pkg2/__init__.py imports from pkg2) work
|
||||
import pkg2
|
@@ -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()
|
@@ -0,0 +1,2 @@
|
||||
# This tests relative imports as used in pkg6
|
||||
import pkg6
|
@@ -0,0 +1,2 @@
|
||||
# This tests ... relative imports as used in pkg7 and imports beyond package root
|
||||
import pkg7.subpkg1.subpkg2.mod3
|
@@ -0,0 +1,8 @@
|
||||
pkg __name__: pkg7
|
||||
pkg __name__: pkg7.subpkg1
|
||||
pkg __name__: pkg7.subpkg1.subpkg2
|
||||
mod1
|
||||
mod2
|
||||
mod1.foo
|
||||
mod2.bar
|
||||
ImportError
|
@@ -0,0 +1,2 @@
|
||||
# import with no __init__.py files
|
||||
import pkg8.mod
|
@@ -0,0 +1,13 @@
|
||||
# test errors with import *
|
||||
|
||||
# 'import *' is not allowed in function scope
|
||||
try:
|
||||
exec("def foo(): from x import *")
|
||||
except SyntaxError as er:
|
||||
print("function", "SyntaxError")
|
||||
|
||||
# 'import *' is not allowed in class scope
|
||||
try:
|
||||
exec("class C: from x import *")
|
||||
except SyntaxError as er:
|
||||
print("class", "SyntaxError")
|
@@ -0,0 +1,24 @@
|
||||
# test __getattr__ on module
|
||||
|
||||
# ensure that does_not_exist doesn't exist to start with
|
||||
this = __import__(__name__)
|
||||
try:
|
||||
this.does_not_exist
|
||||
assert False
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
# define __getattr__
|
||||
def __getattr__(attr):
|
||||
if attr == "does_not_exist":
|
||||
return False
|
||||
raise AttributeError
|
||||
|
||||
|
||||
# do feature test (will also test functionality if the feature exists)
|
||||
if not hasattr(this, "does_not_exist"):
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
# check that __getattr__ works as expected
|
||||
print(this.does_not_exist)
|
@@ -0,0 +1 @@
|
||||
False
|
2
components/language/micropython/tests/import/pkg/mod.py
Normal file
2
components/language/micropython/tests/import/pkg/mod.py
Normal file
@@ -0,0 +1,2 @@
|
||||
def foo():
|
||||
return 42
|
@@ -0,0 +1 @@
|
||||
from pkg2 import mod1
|
@@ -0,0 +1 @@
|
||||
from pkg2 import mod2
|
@@ -0,0 +1 @@
|
||||
print("in mod2")
|
@@ -0,0 +1 @@
|
||||
print("pkg __name__:", __name__)
|
@@ -0,0 +1,2 @@
|
||||
print("mod1 __name__:", __name__)
|
||||
from . import mod2
|
@@ -0,0 +1,6 @@
|
||||
print("mod2 __name__:", __name__)
|
||||
print("in mod2")
|
||||
|
||||
|
||||
def foo():
|
||||
print("mod2.foo()")
|
@@ -0,0 +1 @@
|
||||
print("subpkg1 __name__:", __name__)
|
@@ -0,0 +1,2 @@
|
||||
print("subpkg1.mod1 __name__:", __name__)
|
||||
from ..mod2 import foo
|
@@ -0,0 +1,3 @@
|
||||
from .x import *
|
||||
|
||||
print("init")
|
@@ -0,0 +1,3 @@
|
||||
from .y import *
|
||||
|
||||
print("x")
|
1
components/language/micropython/tests/import/pkg6/x/y.py
Normal file
1
components/language/micropython/tests/import/pkg6/x/y.py
Normal file
@@ -0,0 +1 @@
|
||||
print("y")
|
@@ -0,0 +1 @@
|
||||
print("pkg __name__:", __name__)
|
@@ -0,0 +1,2 @@
|
||||
print("mod1")
|
||||
foo = "mod1.foo"
|
@@ -0,0 +1,2 @@
|
||||
print("mod2")
|
||||
bar = "mod2.bar"
|
@@ -0,0 +1 @@
|
||||
print("pkg __name__:", __name__)
|
@@ -0,0 +1 @@
|
||||
print("pkg __name__:", __name__)
|
@@ -0,0 +1,11 @@
|
||||
from ... import mod1
|
||||
from ...mod2 import bar
|
||||
|
||||
print(mod1.foo)
|
||||
print(bar)
|
||||
|
||||
# attempted relative import beyond top-level package
|
||||
try:
|
||||
from .... import mod1
|
||||
except ImportError:
|
||||
print("ImportError")
|
1
components/language/micropython/tests/import/pkg8/mod.py
Normal file
1
components/language/micropython/tests/import/pkg8/mod.py
Normal file
@@ -0,0 +1 @@
|
||||
print("foo")
|
@@ -0,0 +1,4 @@
|
||||
try:
|
||||
from . import foo
|
||||
except:
|
||||
print("Invalid relative import caught")
|
18
components/language/micropython/tests/import/try_module.py
Normal file
18
components/language/micropython/tests/import/try_module.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# 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()
|
Reference in New Issue
Block a user