diff options
Diffstat (limited to 'tests/import')
| -rw-r--r-- | tests/import/builtin_import.py | 10 | ||||
| -rw-r--r-- | tests/import/gen_context.py | 2 | ||||
| -rw-r--r-- | tests/import/import1a.py | 1 | ||||
| -rw-r--r-- | tests/import/import1b.py | 1 | ||||
| -rw-r--r-- | tests/import/import2a.py | 2 | ||||
| -rw-r--r-- | tests/import/import3a.py | 1 | ||||
| -rw-r--r-- | tests/import/import_file.py | 1 | ||||
| -rw-r--r-- | tests/import/import_override.py | 8 | ||||
| -rw-r--r-- | tests/import/import_pkg1.py | 1 | ||||
| -rw-r--r-- | tests/import/import_pkg3.py | 1 | ||||
| -rw-r--r-- | tests/import/import_star_error.py | 8 | ||||
| -rw-r--r-- | tests/import/module_getattr.py | 7 | ||||
| -rw-r--r-- | tests/import/mpy_invalid.py | 26 | ||||
| -rw-r--r-- | tests/import/mpy_native.py | 24 | ||||
| -rw-r--r-- | tests/import/pkg3/mod2.py | 1 | ||||
| -rw-r--r-- | tests/import/pkg6/__init__.py | 3 | ||||
| -rw-r--r-- | tests/import/pkg6/x/__init__.py | 3 | ||||
| -rw-r--r-- | tests/import/pkg6/x/y.py | 2 | ||||
| -rw-r--r-- | tests/import/pkg7/mod1.py | 4 | ||||
| -rw-r--r-- | tests/import/pkg7/mod2.py | 4 | ||||
| -rw-r--r-- | tests/import/pkg7/subpkg1/subpkg2/mod3.py | 3 | ||||
| -rw-r--r-- | tests/import/pkg8/mod.py | 2 | ||||
| -rw-r--r-- | tests/import/try_module.py | 5 |
23 files changed, 80 insertions, 40 deletions
diff --git a/tests/import/builtin_import.py b/tests/import/builtin_import.py index 157da9839..734498d1b 100644 --- a/tests/import/builtin_import.py +++ b/tests/import/builtin_import.py @@ -1,22 +1,22 @@ # test calling builtin import function # basic test -__import__('builtins') +__import__("builtins") # first arg should be a string try: __import__(1) except TypeError: - print('TypeError') + print("TypeError") # module name should not be empty try: __import__("") except ValueError: - print('ValueError') + print("ValueError") # level argument should be non-negative try: - __import__('xyz', None, None, None, -1) + __import__("xyz", None, None, None, -1) except ValueError: - print('ValueError') + print("ValueError") diff --git a/tests/import/gen_context.py b/tests/import/gen_context.py index 02f153146..b7567cf02 100644 --- a/tests/import/gen_context.py +++ b/tests/import/gen_context.py @@ -2,8 +2,10 @@ import gen_context2 GLOBAL = "GLOBAL" + def gen(): print(GLOBAL) yield 1 + gen_context2.call(gen()) diff --git a/tests/import/import1a.py b/tests/import/import1a.py index 16b2d4d30..9d7d72ff7 100644 --- a/tests/import/import1a.py +++ b/tests/import/import1a.py @@ -1,2 +1,3 @@ import import1b + print(import1b.var) diff --git a/tests/import/import1b.py b/tests/import/import1b.py index be74eca09..8c9d15a71 100644 --- a/tests/import/import1b.py +++ b/tests/import/import1b.py @@ -1,4 +1,5 @@ var = 123 + def throw(): raise ValueError diff --git a/tests/import/import2a.py b/tests/import/import2a.py index def6aeb6a..8fb490525 100644 --- a/tests/import/import2a.py +++ b/tests/import/import2a.py @@ -1,5 +1,7 @@ from import1b import var + print(var) from import1b import var as var2 + print(var2) diff --git a/tests/import/import3a.py b/tests/import/import3a.py index 2e9d41f71..2fadd8a52 100644 --- a/tests/import/import3a.py +++ b/tests/import/import3a.py @@ -1,2 +1,3 @@ from import1b import * + print(var) diff --git a/tests/import/import_file.py b/tests/import/import_file.py index cb9a88a70..90ec4e41e 100644 --- a/tests/import/import_file.py +++ b/tests/import/import_file.py @@ -1,2 +1,3 @@ import import1b + print(import1b.__file__) diff --git a/tests/import/import_override.py b/tests/import/import_override.py index 6fe99009e..029ebe54c 100644 --- a/tests/import/import_override.py +++ b/tests/import/import_override.py @@ -1,11 +1,15 @@ # test overriding __import__ combined with importing from the filesystem + def custom_import(name, globals, locals, fromlist, level): - print('import', name, fromlist, level) + print("import", name, fromlist, level) + class M: var = 456 + return M + orig_import = __import__ try: __import__("builtins").__import__ = custom_import @@ -14,4 +18,4 @@ except AttributeError: raise SystemExit # import1a will be done via normal import which will import1b via our custom import -orig_import('import1a') +orig_import("import1a") diff --git a/tests/import/import_pkg1.py b/tests/import/import_pkg1.py index fe6e4473e..5c1b2ef4c 100644 --- a/tests/import/import_pkg1.py +++ b/tests/import/import_pkg1.py @@ -12,5 +12,6 @@ print(pkg_.mod is pkg.mod) # import using "as" import pkg.mod as mm + print(mm is pkg.mod) print(mm.foo()) diff --git a/tests/import/import_pkg3.py b/tests/import/import_pkg3.py index 0ee885b22..ec4697906 100644 --- a/tests/import/import_pkg3.py +++ b/tests/import/import_pkg3.py @@ -3,4 +3,5 @@ from pkg import mod print(mod.foo()) import pkg.mod + print(mod is pkg.mod) diff --git a/tests/import/import_star_error.py b/tests/import/import_star_error.py index 17e237b8c..9e1757b6e 100644 --- a/tests/import/import_star_error.py +++ b/tests/import/import_star_error.py @@ -2,12 +2,12 @@ # 'import *' is not allowed in function scope try: - exec('def foo(): from x import *') + exec("def foo(): from x import *") except SyntaxError as er: - print('function', 'SyntaxError') + print("function", "SyntaxError") # 'import *' is not allowed in class scope try: - exec('class C: from x import *') + exec("class C: from x import *") except SyntaxError as er: - print('class', 'SyntaxError') + print("class", "SyntaxError") diff --git a/tests/import/module_getattr.py b/tests/import/module_getattr.py index 4a18f414d..df7a62181 100644 --- a/tests/import/module_getattr.py +++ b/tests/import/module_getattr.py @@ -10,13 +10,14 @@ except AttributeError: # define __getattr__ def __getattr__(attr): - if attr == 'does_not_exist': + 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') +if not hasattr(this, "does_not_exist"): + print("SKIP") raise SystemExit # check that __getattr__ works as expected diff --git a/tests/import/mpy_invalid.py b/tests/import/mpy_invalid.py index 6e9cbc9db..d6d01e7f1 100644 --- a/tests/import/mpy_invalid.py +++ b/tests/import/mpy_invalid.py @@ -5,6 +5,7 @@ import sys, uio try: uio.IOBase import uos + uos.mount except (ImportError, AttributeError): print("SKIP") @@ -15,8 +16,10 @@ class UserFile(uio.IOBase): def __init__(self, data): self.data = data self.pos = 0 + def read(self): return self.data + def readinto(self, buf): n = 0 while n < len(buf) and self.pos < len(self.data): @@ -24,6 +27,7 @@ class UserFile(uio.IOBase): n += 1 self.pos += 1 return n + def ioctl(self, req, arg): return 0 @@ -31,38 +35,42 @@ class UserFile(uio.IOBase): class UserFS: def __init__(self, files): self.files = files + def mount(self, readonly, mksfs): pass + def umount(self): pass + def stat(self, path): if path in self.files: return (32768, 0, 0, 0, 0, 0, 0, 0, 0, 0) raise OSError + def open(self, path, mode): return UserFile(self.files[path]) # these are the test .mpy files user_files = { - '/mod0.mpy': b'', # empty file - '/mod1.mpy': b'M', # too short header - '/mod2.mpy': b'M\x00\x00\x00', # bad version - '/mod3.mpy': b'M\x00\x00\x00\x7f', # qstr window too large + "/mod0.mpy": b"", # empty file + "/mod1.mpy": b"M", # too short header + "/mod2.mpy": b"M\x00\x00\x00", # bad version + "/mod3.mpy": b"M\x00\x00\x00\x7f", # qstr window too large } # create and mount a user filesystem -uos.mount(UserFS(user_files), '/userfs') -sys.path.append('/userfs') +uos.mount(UserFS(user_files), "/userfs") +sys.path.append("/userfs") # import .mpy files from the user filesystem for i in range(len(user_files)): - mod = 'mod%u' % i + mod = "mod%u" % i try: __import__(mod) except ValueError as er: - print(mod, 'ValueError', er) + print(mod, "ValueError", er) # unmount and undo path addition -uos.umount('/userfs') +uos.umount("/userfs") sys.path.pop() diff --git a/tests/import/mpy_native.py b/tests/import/mpy_native.py index 4ee537f4a..5d7bdab4a 100644 --- a/tests/import/mpy_native.py +++ b/tests/import/mpy_native.py @@ -5,12 +5,13 @@ import sys, uio try: uio.IOBase import uos + uos.mount except (ImportError, AttributeError): print("SKIP") raise SystemExit -if not (sys.platform == 'linux' and sys.maxsize > 2 ** 32): +if not (sys.platform == "linux" and sys.maxsize > 2 ** 32): print("SKIP") raise SystemExit @@ -19,8 +20,10 @@ class UserFile(uio.IOBase): def __init__(self, data): self.data = data self.pos = 0 + def read(self): return self.data + def readinto(self, buf): n = 0 while n < len(buf) and self.pos < len(self.data): @@ -28,6 +31,7 @@ class UserFile(uio.IOBase): n += 1 self.pos += 1 return n + def ioctl(self, req, arg): return 0 @@ -35,19 +39,24 @@ class UserFile(uio.IOBase): class UserFS: def __init__(self, files): self.files = files + def mount(self, readonly, mksfs): pass + def umount(self): pass + def stat(self, path): if path in self.files: return (32768, 0, 0, 0, 0, 0, 0, 0, 0, 0) raise OSError + def open(self, path, mode): return UserFile(self.files[path]) # these are the test .mpy files +# fmt: off user_files = { # bad architecture '/mod0.mpy': b'M\x05\xff\x00\x10', @@ -95,20 +104,21 @@ user_files = { b'\x03\x01\x00' # dummy relocation of rodata ), } +# fmt: on # create and mount a user filesystem -uos.mount(UserFS(user_files), '/userfs') -sys.path.append('/userfs') +uos.mount(UserFS(user_files), "/userfs") +sys.path.append("/userfs") # import .mpy files from the user filesystem for i in range(len(user_files)): - mod = 'mod%u' % i + mod = "mod%u" % i try: __import__(mod) - print(mod, 'OK') + print(mod, "OK") except ValueError as er: - print(mod, 'ValueError', er) + print(mod, "ValueError", er) # unmount and undo path addition -uos.umount('/userfs') +uos.umount("/userfs") sys.path.pop() diff --git a/tests/import/pkg3/mod2.py b/tests/import/pkg3/mod2.py index 67f43bad5..37721faaf 100644 --- a/tests/import/pkg3/mod2.py +++ b/tests/import/pkg3/mod2.py @@ -1,5 +1,6 @@ print("mod2 __name__:", __name__) print("in mod2") + def foo(): print("mod2.foo()") diff --git a/tests/import/pkg6/__init__.py b/tests/import/pkg6/__init__.py index 923531c1b..5215da2ec 100644 --- a/tests/import/pkg6/__init__.py +++ b/tests/import/pkg6/__init__.py @@ -1,2 +1,3 @@ from .x import * -print('init') + +print("init") diff --git a/tests/import/pkg6/x/__init__.py b/tests/import/pkg6/x/__init__.py index 6b8b84d0e..80817917d 100644 --- a/tests/import/pkg6/x/__init__.py +++ b/tests/import/pkg6/x/__init__.py @@ -1,2 +1,3 @@ from .y import * -print('x') + +print("x") diff --git a/tests/import/pkg6/x/y.py b/tests/import/pkg6/x/y.py index e8d863c6c..0abc82404 100644 --- a/tests/import/pkg6/x/y.py +++ b/tests/import/pkg6/x/y.py @@ -1 +1 @@ -print('y') +print("y") diff --git a/tests/import/pkg7/mod1.py b/tests/import/pkg7/mod1.py index 6b574114d..0a5eb1505 100644 --- a/tests/import/pkg7/mod1.py +++ b/tests/import/pkg7/mod1.py @@ -1,2 +1,2 @@ -print('mod1') -foo = 'mod1.foo' +print("mod1") +foo = "mod1.foo" diff --git a/tests/import/pkg7/mod2.py b/tests/import/pkg7/mod2.py index 039a5d174..657a6fb52 100644 --- a/tests/import/pkg7/mod2.py +++ b/tests/import/pkg7/mod2.py @@ -1,2 +1,2 @@ -print('mod2') -bar = 'mod2.bar' +print("mod2") +bar = "mod2.bar" diff --git a/tests/import/pkg7/subpkg1/subpkg2/mod3.py b/tests/import/pkg7/subpkg1/subpkg2/mod3.py index c73e2081f..0aa916d20 100644 --- a/tests/import/pkg7/subpkg1/subpkg2/mod3.py +++ b/tests/import/pkg7/subpkg1/subpkg2/mod3.py @@ -1,5 +1,6 @@ from ... import mod1 from ...mod2 import bar + print(mod1.foo) print(bar) @@ -7,4 +8,4 @@ print(bar) try: from .... import mod1 except ValueError: - print('ValueError') + print("ValueError") diff --git a/tests/import/pkg8/mod.py b/tests/import/pkg8/mod.py index b98f02ce6..3d3d53a16 100644 --- a/tests/import/pkg8/mod.py +++ b/tests/import/pkg8/mod.py @@ -1 +1 @@ -print('foo') +print("foo") diff --git a/tests/import/try_module.py b/tests/import/try_module.py index 03a9db15b..7c97df28b 100644 --- a/tests/import/try_module.py +++ b/tests/import/try_module.py @@ -2,8 +2,10 @@ # its namespace stick and namespace of current module not coming back. import import1b + def func1(): - print('func1') + print("func1") + def func2(): try: @@ -12,4 +14,5 @@ def func2(): pass func1() + func2() |
