micropython: add micropython component

This commit is contained in:
KY-zhang-X
2022-09-29 12:10:37 +08:00
parent 1514f1cb9b
commit dd76146324
2679 changed files with 354110 additions and 0 deletions

View File

@@ -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")

View File

@@ -0,0 +1,11 @@
import gen_context2
GLOBAL = "GLOBAL"
def gen():
print(GLOBAL)
yield 1
gen_context2.call(gen())

View File

@@ -0,0 +1,2 @@
def call(g):
next(g)

View File

@@ -0,0 +1,3 @@
import import1b
print(import1b.var)

View File

@@ -0,0 +1,5 @@
var = 123
def throw():
raise ValueError

View File

@@ -0,0 +1,7 @@
from import1b import var
print(var)
from import1b import var as var2
print(var2)

View File

@@ -0,0 +1,3 @@
from import1b import *
print(var)

View File

@@ -0,0 +1,3 @@
import import1b
print(import1b.__file__)

View File

@@ -0,0 +1 @@
from import_long_dyn2 import *

View File

@@ -0,0 +1 @@
globals()["long_long_very_long_long_name"] = 1

View File

@@ -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")

View File

@@ -0,0 +1,2 @@
import import1b None 0
456

View 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())

View 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)

View File

@@ -0,0 +1,7 @@
from pkg import mod
print(mod.foo())
import pkg.mod
print(mod is pkg.mod)

View File

@@ -0,0 +1,2 @@
# Testing that "recursive" imports (pkg2/__init__.py imports from pkg2) work
import pkg2

View File

@@ -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()

View File

@@ -0,0 +1,2 @@
# This tests relative imports as used in pkg6
import pkg6

View File

@@ -0,0 +1,2 @@
# This tests ... relative imports as used in pkg7 and imports beyond package root
import pkg7.subpkg1.subpkg2.mod3

View File

@@ -0,0 +1,8 @@
pkg __name__: pkg7
pkg __name__: pkg7.subpkg1
pkg __name__: pkg7.subpkg1.subpkg2
mod1
mod2
mod1.foo
mod2.bar
ImportError

View File

@@ -0,0 +1,2 @@
# import with no __init__.py files
import pkg8.mod

View File

@@ -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")

View File

@@ -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)

View File

@@ -0,0 +1 @@
False

View File

@@ -0,0 +1,2 @@
def foo():
return 42

View File

@@ -0,0 +1 @@
from pkg2 import mod1

View File

@@ -0,0 +1 @@
from pkg2 import mod2

View File

@@ -0,0 +1 @@
print("in mod2")

View File

@@ -0,0 +1 @@
print("pkg __name__:", __name__)

View File

@@ -0,0 +1,2 @@
print("mod1 __name__:", __name__)
from . import mod2

View File

@@ -0,0 +1,6 @@
print("mod2 __name__:", __name__)
print("in mod2")
def foo():
print("mod2.foo()")

View File

@@ -0,0 +1 @@
print("subpkg1 __name__:", __name__)

View File

@@ -0,0 +1,2 @@
print("subpkg1.mod1 __name__:", __name__)
from ..mod2 import foo

View File

@@ -0,0 +1,3 @@
from .x import *
print("init")

View File

@@ -0,0 +1,3 @@
from .y import *
print("x")

View File

@@ -0,0 +1 @@
print("y")

View File

@@ -0,0 +1 @@
print("pkg __name__:", __name__)

View File

@@ -0,0 +1,2 @@
print("mod1")
foo = "mod1.foo"

View File

@@ -0,0 +1,2 @@
print("mod2")
bar = "mod2.bar"

View File

@@ -0,0 +1 @@
print("pkg __name__:", __name__)

View File

@@ -0,0 +1 @@
print("pkg __name__:", __name__)

View File

@@ -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")

View File

@@ -0,0 +1 @@
print("foo")

View File

@@ -0,0 +1,4 @@
try:
from . import foo
except:
print("Invalid relative import caught")

View 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()