[英]get function signature from FrameInfo or frame in python
While writing my own exception-hook in python, I came to the idea of using the inspect
-module to provide myselfe more information about how the function got called. 在python中编写自己的异常钩子时,我想到了使用
inspect
-module来向自己提供有关如何调用该函数的更多信息。
This means the signature of the function as well as the arguments passed to it. 这意味着函数的签名以及传递给它的参数 。
import inspect
frame_infos = inspect.trace() # get the FrameInfos
for f_idx, f_info in enumerate(frame_infos):
frame_dict.update(f_info.frame.f_locals) # update namespace with deeper frame levels
#Output basic Error-Information
print(f' File "{f_info.filename}", line {f_info.lineno}, in {f_info.function}')
for line in f_info.code_context:
print(f' {line.strip()}')
########################################################
# show signature and arguments 1 level deeper
if f_idx+1 < len(frame_infos):
func_name = frame_infos[f_idx+1].function #name of the function
try:
func_ref = frame_dict[func_name] # look up in namespace
sig = inspect.signature(func_ref) # call signature for function_reference
except KeyError: sig = '(signature unknown)'
print(f' {func_name} {sig}\n')
print(f' {frame_infos[f_idx+1].frame.f_locals}\n')
This works pretty fine with a basic example like this: 像这样的基本示例可以很好地工作:
def test1 ( x: int, y: tuple = 0 )->list: # the types obviously dont match
return test2(y, b=x, help=0)
def test2 ( a, *args, b, **kwargs ):
return a + b / 0
try:
test1(5)
except: ...
output: 输出:
File "C:/test/errorHandler.py", line 136, in <module>
test1(5)
test1 (x:int, y:tuple=0) -> list
{'y': 0, 'x': 5}
File "C:/test/errorHandler.py", line 130, in test1
return test2(y, b=x, help=0)
test2 (a, *args, b, **kwargs)
{'kwargs': {'help': 0}, 'args': (), 'b': 5, 'a': 0}
File "C:/test/errorHandler.py", line 133, in test2
return a + b / 0
but, as soon as you are leaving 1 file you can't map a functions name against the basic namespace. 但是,一旦您离开1个文件,就无法将函数名称映射到基本名称空间。
file1:
import file2; try: file2.foo() except: ...
file1:
import file2; try: file2.foo() except: ...
文件2import file2; try: file2.foo() except: ...
import file2; try: file2.foo() except: ...
file2:import file3; def foo(): file3.foo()
file2:
import file3; def foo(): file3.foo()
import file3; def foo(): file3.foo()
file3:def foo(): return 0/0
file3:
def foo(): return 0/0
so essentialy, I'm looking for a way to get a function (like <function foo at 0x000002F4A43ACD08>
) from a FrameInfo
or frame
-object, but the only information that I see is the name and the file and line. 因此,从
FrameInfo
,我正在寻找一种从FrameInfo
或frame
FrameInfo
获取函数的方法(例如<function foo at 0x000002F4A43ACD08>
),但是我看到的唯一信息是名称,文件和行。
(I don't like the idea of getting the signature by looking in the sourcefile at a specific line.) (我不喜欢通过在特定行中查看源文件来获得签名的想法。)
Best reference so far was the Inspect
-documentation , but I havent' found something usefull yet. 到目前为止,最好的参考是
Inspect
-documentation ,但是我还没有找到有用的东西。
Based on this answer by jsbueno , I found a solution for recovering the signature. 基于jsbueno的 回答 ,我找到了恢复签名的解决方案。
Using the gc
(garbage collector) function get_referrers()
you can search for all objects that directly refer to a specific object. 使用
gc
(垃圾收集器)函数get_referrers()
可以搜索直接引用特定对象的所有对象。
With the code-object provided by the frame's f_code
you can use this function to find the frames itselve as well es the function. 通过框架的
f_code
提供的代码对象,您可以使用此函数查找框架以及该函数。
code_obj = frame.f_code
import gc #garbage collector
print(gc.get_referrers(code_obj))
# [<function foo at 0x0000020F758F4EA0>, <frame object at 0x0000020F75618CF8>]
so, just find the real function and you are done: 因此,只需找到真正的功能就可以了:
# find the object that has __code__ and is actally the object with that specific code
[obj for obj in garbage_collector.get_referrers(code_obj)
if hasattr(obj, '__code__')
and obj.__code__ is code_obj][0]
now you can use inspect.signature()
on the filtered object. 现在您可以在过滤对象上使用
inspect.signature()
。
Disclamer from gc.get_referrers(objs)
: 来自
gc.get_referrers(objs)
objs)的gc.get_referrers(objs)
:
This function will only locate those containers which support garbage collection;
此功能将仅定位那些支持垃圾收集的容器; extension types which do refer to other objects but do not support garbage collection will not be found.
找不到引用其他对象但不支持垃圾回收的扩展类型。
import inspect
import gc
def ERROR_Printer_Inspection ( stream = sys.stderr ) :
"""
called in try: except: <here>
prints the last error-traceback in the given "stream"
includes signature and function arguments if possible
"""
stream.write('Traceback (most recent call last):\n')
etype, value, _ = sys.exc_info() # get type and value for last line of output
frame_infos = inspect.trace() # get frames for source-lines and arguments
for f_idx, f_info in enumerate(frame_infos):
stream.write(f' File "{f_info.filename}", line {f_info.lineno}, in {f_info.function}\n')
for line in f_info.code_context: # print location and code parts
stream.write(f' {line.lstrip()}')
if f_idx+1 < len(frame_infos): # signature and arguments
code_obj = frame_infos[f_idx+1].frame.f_code # codeobject from next frame
function_obj = [obj for obj in gc.get_referrers(code_obj) if hasattr(obj, '__code__') and obj.__code__ is code_obj]
if function_obj: # found some matching object
function_obj=function_obj[0] # function_object
func_name = frame_infos[f_idx + 1].function # name
stream.write(f' > {func_name} {inspect.signature(function_obj)}\n')
next_frame_locals = frame_infos[f_idx+1].frame.f_locals # calling arguments
# filter them to the "calling"-arguments
arguments = dict((key, next_frame_locals[key]) for key in code_obj.co_varnames if key in next_frame_locals.keys())
stream.write(f' -> {str(arguments)[1:-1]}\n')
stream.write(f'{etype.__name__}: {value}\n')
stream.flush()
displaying "calling" arguments can be missleading if they got edited after function-start: 如果在函数启动后对其进行编辑,则显示“调用”参数可能会误导用户:
def foo (a, b, **kwargs):
del a, kwargs
b = 'fail'
return 0/0
try: foo(0, 1, test=True)
except: ERROR_Printer_Inspection()
output: 输出:
Traceback (most recent call last):
File "C:/test/errorHandler.py", line 142, in <module>
try: foo(0, 1, test=True)
> foo (a, b, **kwargs)
-> 'b': 'fail'
File "C:/test/errorHandler.py", line 140, in foo
return 0 / 0
ZeroDivisionError: division by zero
you can't trust that, but this is a problem for an other question. 您无法相信,但这是另一个问题。
Here are some links, if you like to research for yourselfe: 如果您想自己研究,请点击以下链接:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.