Skip to content Skip to sidebar Skip to footer

Trace Execution Path Of Python Interpreter

The following works nice to see what the interpreter does: python -m trace --ignore-dir=$HOME/lib64:$HOME/lib:/usr -t path-to/script.py But there are too many lines. I would like

Solution 1:

If you create a script trace2.py as

import trace

OrigTrace = trace.Trace
classTrace2(trace.Trace):
    deflocaltrace_trace(self, frame, why, arg):
        if why == "line"and frame.f_code.co_name == '<module>':
            return self.localtrace
        return OrigTrace.localtrace_trace(self, frame, why, arg)

trace.Trace=Trace2
trace.main()

and run python -m trace2 -t script.py you will not see trace output from lines that are on the module level.

Solution 2:

you can use pdb library, by this library you can make breakpoint and/or inject python statements to the break point line by interactive console UI and doing more other actions. ;)

Solution 3:

If you just want to hide code paths during import, just pipe it through a filter like this one:

import re
import sys

cur = None
skip_re = re.compile(r'^(?P<filename>.+)?\((\d*)\):\s*import')
for line in sys.stdin:
    if cur andnot line.startswith(cur):
        continue
    cur = None
    match = skip_re.match(line)
    if match:
        cur = match.group('filename')
    sys.stdout.write(line)

Post a Comment for "Trace Execution Path Of Python Interpreter"