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

Peter Slížik peter.slizik at gmail.com
Thu Feb 16 05:01:19 EST 2023


Gerard - I did not use the filtering options. Thank you for bringing them
to my attention.

Barry - thank you for the insight.

Now the tracing works as expected. I'm not sure why it didn't work
before... Maybe the program redirected stdout?

Thank you guys,
Peter

On Thu, Feb 16, 2023 at 9:56 AM Barry <barry at barrys-emacs.org> wrote:

>
>
> On 15 Feb 2023, at 17:23, Peter Slížik <peter.slizik at gmail.com> wrote:
>
> 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)
>
>
>
> The docs show that you need to do either add the outfile to trace
> or generate and write the report after runfunc returns.
>
> I have not tested this, just read the docs out of curiosity
> Here https://docs.python.org/3/library/trace.html
>
> Barry
>
> 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
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>


More information about the Python-list mailing list