我正在尝试跟踪测试文件的执行并需要比较和变量的所有值。变量值的跟踪是有效的,但在没有分配它们的情况下进行比较的值是无效的。
例如:
def test_random_test():
assert random.randint(0, 10) >= 8
如果断言通过,我将得不到有关随机函数生成的值的任何信息。我知道 Pytest 支持实验版本,但我的目标是不使用他们的 API。
那么,是否有可能从随机函数中获取(追踪)生成的值?
最佳答案
使用 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。文档中要强调的要点:
The trace function is invoked (with event set to
'call'
) whenever a new local scope is entered; it should return a reference to a local trace function to be used for the new scope, orNone
if the scope shouldn’t be traced.The local trace function should return a reference to itself (or to another function for further tracing in that scope), or
None
to turn off tracing in that scope.
还有一个包function_trace
那works using sys.settrace
.对了,还有一个模块trace
在 Python 的标准库中。
使用 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
是一个已知值,不是在运行时决定的。此外,正如通过阅读 bytecode 观察到的,文字 8
似乎没有引起任何可追踪的函数或方法事件。函数test_random_test
:
"""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)。但是,我不确定字节码级别的跟踪结果的可读性如何(例如,考虑在使用多个运算符评估表达式时创建的中间值)。
关于python - 是否可以跟踪未分配的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67194977/