[英]`inspect.getsource` from a function defined in a string? `s="def f(): return 5"`
給定一個內聯定義的函數,如何讓getsource
提供輸出? - 這是為了測試,這是我正在嘗試的事情:
from importlib.util import module_from_spec, spec_from_loader
_locals = module_from_spec(
spec_from_loader("helper", loader=None, origin="str") # loader=MemoryInspectLoader
)
exec(
'def f(): return "foo"',
_locals.__dict__,
)
f = getattr(_locals, "f")
setattr(f, "__loader__", MemoryInspectLoader)
通過我的嘗試,它看起來像一個linecache
問題:
from importlib.abc import Loader
class MemoryInspectLoader(Loader):
def get_code(self): raise NotImplementedError()
但錯誤永遠不會被提出。 從getsource(f)
,我得到:
In [2]: import inspect
...: inspect.getsource(f)
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-3-1348c7a45f75> in <module>
----> 1 inspect.getsource(f)
/usr/lib/python3.8/inspect.py in getsource(object)
983 or code object. The source code is returned as a single string. An
984 OSError is raised if the source code cannot be retrieved."""
--> 985 lines, lnum = getsourcelines(object)
986 return ''.join(lines)
987
/usr/lib/python3.8/inspect.py in getsourcelines(object)
965 raised if the source code cannot be retrieved."""
966 object = unwrap(object)
--> 967 lines, lnum = findsource(object)
968
969 if istraceback(object):
/usr/lib/python3.8/inspect.py in findsource(object)
796 lines = linecache.getlines(file)
797 if not lines:
--> 798 raise OSError('could not get source code')
799
800 if ismodule(object):
OSError: could not get source code
這是我的解決方案:
import os.path
import sys
import tempfile
from importlib.util import module_from_spec, spec_from_loader
from types import ModuleType
from typing import Any, Callable
class ShowSourceLoader:
def __init__(self, modname: str, source: str) -> None:
self.modname = modname
self.source = source
def get_source(self, modname: str) -> str:
if modname != self.modname:
raise ImportError(modname)
return self.source
def make_function(s: str) -> Callable[..., Any]:
filename = tempfile.mktemp(suffix='.py')
modname = os.path.splitext(os.path.basename(filename))[0]
assert modname not in sys.modules
# our loader is a dummy one which just spits out our source
loader = ShowSourceLoader(modname, s)
spec = spec_from_loader(modname, loader, origin=filename)
module = module_from_spec(spec)
# the code must be compiled so the function's code object has a filename
code = compile(s, mode='exec', filename=filename)
exec(code, module.__dict__)
# inspect.getmodule(...) requires it to be in sys.modules
sys.modules[modname] = module
return module.f
import inspect
func = make_function('def f(): print("hi")')
print(inspect.getsource(func))
輸出:
$ python3 t.py
def f(): print("hi")
有一些微妙的,不幸的點:
sys.modules
東西( inspect.getsource
總是在那里尋找inspect.getmodule
)__loader__
是假的,如果您正在做任何其他需要__loader__
功能正常的__loader__
這可能會因此而中斷順便說一句,您可能最好以其他方式保留原始源,而不是通過幾個全局變量( sys.modules
、 linecache
、 __loader__
等)
Monkey 補丁 linecache.getlines 以使 inspect.getsource() 與來自 exec() 的代碼一起工作。 當您查看錯誤堆棧時,它會在inspect.py 中的findsource() 處停止。 當你查看findsource() 的代碼時,你會看到一個提示:
# Allow filenames in form of "<something>" to pass through.
# `doctest` monkeypatches `linecache` module to enable
# inspection, so let `linecache.getlines` to be called.
然后,如果您查看這個測試函數,您就會明白它的含義。 您可以臨時更改核心 Python 函數之一來滿足您的目的。
無論如何,這是解決方案:
import linecache
import inspect
def exec_getsource(code):
getlines = linecache.getlines
def monkey_patch(filename, module_globals=None):
if filename == '<string>':
return code.splitlines(keepends=True)
else:
return getlines(filename, module_globals)
linecache.getlines = monkey_patch
try:
exec(code)
#you can now use inspect.getsource() on the result of exec() here
finally:
linecache.getlines = getlines
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.