【发布时间】:2017-05-04 02:23:46
【问题描述】:
我正在尝试使用 Python 的自省模块 inspect 来检索已使用 importlib.util 的 module_from_spec 函数加载到作用域中的活动对象的源代码。尝试在 spec_file 本身或 spec 文件中的任何函数上使用 inspect.getsource() 成功返回所需的源代码。但是,用于检索规范文件中类类型的源代码的相同方法会引发 TypeError: None is a built-in class。
from importlib.util import spec_from_file_location, module_from_spec
spec_file = spec_from_file_location("file_module", 'test_file.py')
spec_module = module_from_spec(spec_file)
spec_file.loader.exec_module(spec_module)
# spec module
import inspect
assert isinstance(inspect.getsource(spec_module), str)
# function in spec module
func_obj = getattr(spec_module, 'test_function')
assert isinstance(inspect.getsource(func_obj), str)
# class in spec module
class_obj = getattr(spec_module, 'testClass')
try:
inspect.getsource(class_obj)
except TypeError:
print('where did the source code data go?')
罪魁祸首似乎是回溯链中的 inspect.getfile() 调用,其中函数对象返回 object.__code__,而类对象试图加载其模块以检索模块。__file__。为什么函数有__code__ 方法,而类没有?这是 Python 如何处理无意中破坏动态加载类的自省的类型的副作用吗?
【问题讨论】:
标签: python introspection inspect python-importlib