Python trace module and file paths

I am using the python tracemodule to track the execution of some code. I notice that when a performance is printed later, it will be printed in the following form (below is a working example):

<filename>(<line number>): <line of code>

You can get the full (or absolute or relative) path to the file, and not just the file name. It makes sense that there trace.Tracewill be a flag in the call that should allow me to do this, but I seem to be unable to find any such parameter in the docs.

If such a flag does not exist, how can I get the file path? I think I could check all directories in sys.path, but how could I handle cases where two different directories have files with the same name?

+2
source share
1 answer

This was not very obvious, but I found that if I use the parameter countfuncsin traceor the parameter --listfuncs, if I use tracethe command line, I get the full path names. Thus, for the program:

import trace
from recurse import recurse

tracer = trace.Trace(count=False, trace=True, countfuncs=True)
tracer.run('recurse(2)')
tracer.results().write_results()

... (using the example of Doug Hellman from here ) I get the following output:

functions called:
filename: <string>, modulename: <string>, funcname: <module>
filename: C:\Python33\lib\encodings\cp850.py, modulename: cp850, funcname: IncrementalEncoder.encode
filename: C:\Python33\lib\trace.py, modulename: trace, funcname: _unsettrace
filename: C:\usr\sjl\dev\test\python\recurse.py, modulename: recurse, funcname: recurse

This is not exactly what you were looking for, because you can see that it ignores the instruction for tracking lines of code, so it does not give the file path next to these lines of code. However, it will tell you exactly which files the Python script used.

0
source

All Articles