forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.py
More file actions
77 lines (51 loc) · 1.67 KB
/
import.py
File metadata and controls
77 lines (51 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import import_target, import_target as aliased
from import_target import func, other_func
from import_target import func as aliased_func, other_func as aliased_other_func
from import_star import *
import import_mutual1
assert import_target.X == import_target.func()
assert import_target.X == func()
assert import_mutual1.__name__ == "import_mutual1"
assert import_target.Y == other_func()
assert import_target.X == aliased.X
assert import_target.Y == aliased.Y
assert import_target.X == aliased_func()
assert import_target.Y == aliased_other_func()
assert STAR_IMPORT == "123"
try:
from import_target import func, unknown_name
raise AssertionError("`unknown_name` does not cause an exception")
except ImportError:
pass
try:
import mymodule
except ModuleNotFoundError as exc:
assert exc.name == "mymodule"
test = __import__("import_target")
assert test.X == import_target.X
import builtins
class OverrideImportContext:
def __enter__(self):
self.original_import = builtins.__import__
def __exit__(self, exc_type, exc_val, exc_tb):
builtins.__import__ = self.original_import
with OverrideImportContext():
def fake_import(name, globals=None, locals=None, fromlist=(), level=0):
return len(name)
builtins.__import__ = fake_import
import test
assert test == 4
# TODO: Once we can determine current directory, use that to construct this
# path:
# import sys
# sys.path.append("snippets/import_directory")
# import nested_target
# try:
# X
# except NameError:
# pass
# else:
# raise AssertionError('X should not be imported')
from testutils import assert_raises
with assert_raises(SyntaxError):
exec("import")