Stick this in a file (say, printfile.py): print __file__ If I run it as "python printfile.py" using Python 2.4, 2.5 or 2.6 it prints as expected, e.g.: % python ~/tmp/printfile.py /Users/skip/tmp/printfile.py If, however, I run it under control of the trace module I get something entirely different: % python -m trace --count ~/tmp/printfile.py /Users/skip/local/lib/python2.6/trace.py % python -m trace --trace ~/tmp/printfile.py --- modulename: threading, funcname: settrace threading.py(70): _trace_hook = func --- modulename: trace, funcname: (1): --- modulename: trace, funcname: printfile.py(1): print __file__ /Users/skip/local/lib/python2.6/trace.py Definitely looks buggy to me...
Hmmm... Maybe not so hard after all. See attached diff. Does anyone see a problem with simply overwriting the trace module's __file__ attribute? File Added: trace.diff
Wouldn't it be better for trace.main() to call Tracer.runctx() instead of Tracer.run()? That way you could use a copy of __main__.__dict__ for locals and globals and overwrite the filename there. Note that your solution would still report the wrong filename if someone called tracer.main() from his own main script.