使用sys.settrace跟踪Python函数
函数sys.settrace可用于找出函数返回的值,如下:
"""How to trace values returned by functions, even if unassigned.
For more details:
https://docs.python.org/3/library/sys.html#sys.settrace
"""
import random
import sys
def test_random_test():
"""Sample function to trace."""
assert random.randint(0, 10) >= 8
def tracer(frame, event, arg):
"""System's trace function."""
if event == 'call':
return trace_returns
def trace_returns(frame, event, arg):
"""Intercept and print values returned by functions.
Trace function for local scopes.
"""
co = frame.f_code
func_name = co.co_name
if event == 'return':
print(f'function `{func_name}` returns: {arg}')
# A local trace function returns the function to
# be called the next time a trace event is
# generated in the same local scope,
# or `None` to turn off further tracing in
# that scope.
return trace_returns
if __name__ == "__main__":
sys.settrace(tracer)
test_random_test()
上述代码输出(在断言通过的其中一次运行中)以下内容(打印的整数值可能因调用而异):
function `_randbelow_with_getrandbits` returns: 9
function `randrange` returns: 9
function `randint` returns: 9
function `test_random_test` returns: None
更多examples。值得注意的是,跟踪函数需要如何处理 sys.settrace 已从 Python 2 更改为 Python 3。文档中要强调的要点:
每当输入新的本地范围时,都会调用跟踪函数(event 设置为'call');它应该返回对要用于新范围的本地跟踪函数的引用,如果不应该跟踪范围,则返回 None。
本地跟踪函数应返回对自身的引用(或返回另一个函数以在该范围内进行进一步跟踪),或 None 以关闭该范围内的跟踪。
此外,还有一个包function_trace 即works using sys.settrace。顺便说一句,Python 的标准库中还有一个模块trace。
使用sys.setprofile 也可以跟踪 C 函数
上述基于sys.settrace 的方法不跟踪C 函数。其中的名称可以使用函数sys.setprofile进行追踪,如下:
"""How to trace the names of C functions, including builtins.
For more details:
https://docs.python.org/3.9/library/sys.html#sys.setprofile
"""
import random
import sys
def test_random_test():
"""Sample function to trace."""
assert random.randint(0, 10) >= 8
def profiler(frame, event, arg):
"""Intercept also builtins."""
co = frame.f_code
func_name = co.co_name
if event == 'return':
print(f'function `{func_name}` returns: {arg}')
elif event == 'c_return':
# note the difference in the meaning of `func_name`:
# it is the caller's name, not the callee's
# (i.e., the name of the function from where the
# C function was called, not the name of the C
# function itself).
# Also, we do not get the value returned by
# the C function
print(
f'C function `{arg}` returns to '
f'function `{func_name}`')
if __name__ == "__main__":
sys.setprofile(profiler)
test_random_test()
上面的代码在断言通过以下运行时输出:
C function `<built-in method bit_length of int object at 0x10e12ba70>` returns to function `_randbelow_with_getrandbits`
C function `<built-in method getrandbits of Random object at 0x7fe91404de10>` returns to function `_randbelow_with_getrandbits`
function `_randbelow_with_getrandbits` returns: 9
function `randrange` returns: 9
function `randint` returns: 9
function `test_random_test` returns: None
function `<module>` returns: None
关于跟踪文字
上述方法不打印文字 8 的值。基于这个问题,这不一定是跟踪的要求,因为8 是一个已知值,不是在运行时决定的。此外,通过阅读函数test_random_test 的bytecode 观察到,文字8 似乎不会引发任何可追溯的函数或方法事件:
"""How to print a function's bytecode.
For more details:
https://docs.python.org/3/library/dis.html
"""
import dis
import random
def test_random_test():
"""Sample function to trace."""
assert random.randint(0, 10) >= 8
if __name__ == "__main__":
dis.dis(test_random_test)
上面的代码打印:
12 0 LOAD_GLOBAL 0 (random)
2 LOAD_METHOD 1 (randint)
4 LOAD_CONST 1 (0)
6 LOAD_CONST 2 (10)
8 CALL_METHOD 2
10 LOAD_CONST 3 (8)
12 COMPARE_OP 5 (>=)
14 POP_JUMP_IF_TRUE 20
16 LOAD_ASSERTION_ERROR
18 RAISE_VARARGS 1
>> 20 LOAD_CONST 4 (None)
22 RETURN_VALUE
由此我们得知整数 8 是直接作为常量加载的:
10 LOAD_CONST 3 (8)
所以跟踪 每个 未分配的值似乎需要跟踪字节码 (example for Python 2)。但是,我不确定在字节码级别跟踪结果的可读性如何(例如,考虑使用多个运算符评估表达式时创建的中间值)。