簡體   English   中英

來自字符串中定義的函數的`inspect.getsource`? `s="def f(): 返回 5"`

[英]`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

如何使getsource與 Python 3.6+ 中的內聯定義函數getsource工作?

這是我的解決方案:

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")

有一些微妙的,不幸的點:

  1. 它需要注入sys.modules東西( inspect.getsource總是在那里尋找inspect.getmodule
  2. 我構建的__loader__是假的,如果您正在做任何其他需要__loader__功能正常的__loader__這可能會因此而中斷
  3. 內聯記錄了其他奇怪的事情

順便說一句,您可能最好以其他方式保留原始源,而不是通過幾個全局變量( sys.moduleslinecache__loader__等)

不完全確定我是否正確回答了這個問題。

但是如果你有以下代碼:

class MemoryInspectLoader(Loader):
    def get_code(self): raise NotImplementedError()

您可以使用dill提取函數體。

from dill.source import getsource

print(getsource(MemoryInspectLoader.get_code))

這將輸出:

        def get_code(self): raise NotImplementedError()

這也在this SO answer中得到了證明。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM