trace — 跟随程序流
优质
小牛编辑
138浏览
2023-12-01
Example Program
# trace_example/main.py
from recurse import recurse
def main():
print('This is the main program.')
recurse(2)
if __name__ == '__main__':
main()
Tracing Execution
$ python3 -m trace --ignore-dir=.../lib/python3.6 \
--trace trace_example/main.py
--- modulename: main, funcname: <module>
main.py(7): """
main.py(10): from recurse import recurse
--- modulename: recurse, funcname: <module>
recurse.py(7): """
recurse.py(11): def recurse(level):
recurse.py(17): def not_called():
main.py(13): def main():
main.py(18): if __name__ == '__main__':
main.py(19): main()
--- modulename: main, funcname: main
main.py(14): print('This is the main program.')
This is the main program.
main.py(15): recurse(2)
--- modulename: recurse, funcname: recurse
recurse.py(12): print('recurse({})'.format(level))
recurse(2)
recurse.py(13): if level:
recurse.py(14): recurse(level - 1)
--- modulename: recurse, funcname: recurse
recurse.py(12): print('recurse({})'.format(level))
recurse(1)
recurse.py(13): if level:
recurse.py(14): recurse(level - 1)
--- modulename: recurse, funcname: recurse
recurse.py(12): print('recurse({})'.format(level))
recurse(0)
recurse.py(13): if level:
--- modulename: trace, funcname: _unsettrace
trace.py(77): sys.settrace(None)
Code Coverage
$ python3 -m trace --count trace_example/main.py
This is the main program.
recurse(2)
recurse(1)
recurse(0)
from recurse import recurse
def main():
print 'This is the main program.'
recurse(2)
return
if __name__ == '__main__':
main()
def recurse(level):
print 'recurse(%s)' % level
if level:
recurse(level-1)
return
def not_called():
print 'This function is never called.'
Programming Interface
# trace_run.py
import trace
from trace_example.recurse import recurse
tracer = trace.Trace(count=False, trace=True)
tracer.run('recurse(2)')
Saving Result Data
# trace_CoverageResults.py
import trace
from trace_example.recurse import recurse
tracer = trace.Trace(count=True, trace=False)
tracer.runfunc(recurse, 2)
results = tracer.results()
results.write_results(coverdir='coverdir2')
# trace_report.py
import trace
from trace_example.recurse import recurse
tracer = trace.Trace(count=True,
trace=False,
outfile='trace_report.dat')
tracer.runfunc(recurse, 2)
report_tracer = trace.Trace(count=False,
trace=False,
infile='trace_report.dat')
results = tracer.results()
results.write_results(summary=True, coverdir='/tmp')