Minimal debug/rep functionality

Peter Hansen peter at
Thu Sep 19 07:20:21 EDT 2002

Delaney, Timothy wrote:
>>From: Peter Hansen [mailto:peter at]
>> >>> def trace(frame, event, arg):
>>...   if event == 'line':
>>...      print '%s: %s' % (frame.f_code.co_filename, frame.f_lineno)
>>...   return trace
>> >>> import sys
>> >>> sys.settrace(trace)
> 1. frame.f_code.co_filename will return the file that the .pyc was compiled
> from. Particularly in the case of standard libraries, this will often not be
> the path to the file in *your* filesystem. There is a way to get the
> corresponding file in your filesystem (basically, determine the module the
> frame comes from, then get the module filename using inspect).

True... presumably either the OP is not concerned about standard
library modules anyway, or he would check the archives and add
a simple way of doing this.  YAGNI...

> 2. The above will be *slow*. Very very slow. For a short program, this may
> not matter. For a long program, it will (I'm talking order of magnitude
> slower).

Hmmm... I didn't see any implication that speed was even remotely
a concern for the OP.  After all, he did ask to *print* each line
as it executed.  What could possibly be slower than that?
(I doubt he would care to deal with the volume of information
generated when using this with a large program anyway... needs
a way to turn it on and off, at least.)

My standard comments about Python speed and Knuth's comments about
premature optimization apply...

I'm looking forward to seeing your tool though, Timothy. :)


More information about the Python-list mailing list