是否可以跟踪未分配的值?

Is it possible to trace non-assigned values?

我正在尝试跟踪测试文件的执行并需要比较和变量的所有值。变量值的跟踪工作正常,但在没有分配它们的情况下进行比较的值无效。

例如:

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, or None 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.

此外,Python 的标准库中有一个包 function_trace that works using sys.settrace. By the way, there is also a module 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 是一个已知值,而不是在 运行 时间决定的。此外,文字 8 似乎没有引起任何可追踪的函数或方法事件,正如通过阅读函数 test_random_test:

bytecode 所观察到的
"""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)。但是,我不确定在字节码级别跟踪结果的可读性如何(例如,考虑在使用多个运算符评估表达式时创建的中间值)。