Python: How to use the 'trace' module programmatically?

Peter Slížik peter.slizik at gmail.com
Wed Feb 15 04:56:13 EST 2023


Hello,

I'm trying to analyze complex Python code. For some specific reasons, I
decided to use tracing instead of a debugger.

The first thing I tried was:

python -m trace -t /path/to/file.py

The output of this command turned out to be completely useless. The reason
is that there was a thread running in the background, doing some work
every *0.1
s* and this generated thousands of lines of tracing information. The useful
information (a reaction to my interaction with app GUI) scrolled away in a
blink.

For this reason, I decided to limit the scope of tracing. I did the
following.

The original code:

def caller():
    print("I'm the caller.")
    callee()
def callee():
    print("Callee here.")

Code modified for tracing:

import trace

tracer = trace.Tracer(
    count=0,
    trace=1,
)
def outer():
    print("I'm the caller.")
    tracer.runfunc(inner)
def inner():
    print("Callee here.")

Now I launched the program and the tracer did not generate any output. I
was hoping that this would provide complete tracing information, but only
for the limited scope of inner().

No success with tracer.run() either.

What I was able to do, when I set count=1, I was able to catch the coverage
data with tracer.results() and write them to a file. But the tracing
information was not generated even in this case.

Am I doing anything wrong?

Peter


More information about the Python-list mailing list