Using sys.settrace to trace Python functions
The function sys.settrace could be used for finding out what values are returned by functions, as follows:
"""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()
The above code outputs (in one of the runs where the assertion passes) the following (the printed integer value can vary from call to call):
function `_randbelow_with_getrandbits` returns: 9
function `randrange` returns: 9
function `randint` returns: 9
function `test_random_test` returns: None
More examples. It is worth noting that how trace functions need to behave with sys.settrace has changed from Python 2 to Python 3. Points to highlight from the documentation:
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.
Also, there is a package function_trace that works using sys.settrace. By the way, there is also a module trace in Python's standard library.
Using sys.setprofile to trace also C functions
The above sys.settrace-based approach does not trace C functions. The names of these can be traced using the function sys.setprofile, as follows:
"""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()
The above code outputs in a run in which the assertion passes the following:
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
About tracing literals
The above approaches do not print the value of the literal 8. Based on the question, this is not necessarily a requirement from tracing, because 8 is a known value, not decided at runtime. Besides, the literal 8 does not appear to give rise to any traceable function or method event, as observed by reading the bytecode of the function 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)
The above code prints:
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
From this we learn that the integer 8 is directly loaded as a constant:
10 LOAD_CONST 3 (8)
So tracing every unassigned value seems to require tracing of bytecode (example for Python 2). However, I am not sure how readable the results of tracing at the bytecode level would be (e.g., consider the intermediate values that are created when evaluating an expression with several operators).