-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcimport.py
69 lines (53 loc) · 2.08 KB
/
cimport.py
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
import importlib
import types
import sys
import builtins
# 模拟数据库
database = {
"module_a": "def hello():\n return 'Hello from module A!'",
"module_b": "import module_a\n\ndef hello():\n return module_a.hello() + ' And hello from module B!'",
"module_c": "import module_b\n\ndef hello():\n return module_b.hello() + ' And hello from module C!'",
"module_d": "import module_c\n\ndef hello():\n return module_c.hello() + ' And hello from module D!'",
"module_e": "import module_c, module_d\n\ndef hello():\n return module_c.hello() + module_d.hello() + ' And hello from module E!'",
}
class CustomLoader:
def get_code(self, fullname):
return database.get(fullname, "")
def is_package(self, fullname):
return False # 假设没有任何包
def load_module(self, fullname):
code = self.get_code(fullname)
if code == "":
return importlib.import_module(fullname)
mod = sys.modules.setdefault(fullname, types.ModuleType(fullname))
mod.__file__ = "<%s>" % self.__class__.__name__
mod.__loader__ = self
ispkg = self.is_package(fullname)
if ispkg:
mod.__path__ = []
mod.__package__ = fullname
else:
mod.__package__ = fullname.rpartition(".")[0]
mod.__dict__["__builtins__"] = {}
mod.__dict__["__builtins__"]["__import__"] = custom_import
exec(code, mod.__dict__)
return mod
def custom_import(name, globals=None, locals=None, fromlist=(), level=0):
loader = CustomLoader()
return loader.load_module(name)
# 测试代码
code = """
import module_e
from blinker import signal
print(signal)
result = module_e.hello()
"""
# 定义一个命名空间
namespace = {"__builtins__": builtins.__dict__.copy()}
namespace["__builtins__"]["__import__"] = custom_import
# 在受限的环境中执行代码
exec(code, namespace)
# 从命名空间中获取结果
result = namespace["result"]
print(result) # 输出应该包含所有模块的问候语
print(namespace["module_e"]) # 输出导入的module_e模块