Minimal debug/rep functionality

Peter Hansen peter at
Wed Sep 18 23:14:25 EDT 2002

Mark wrote:
> Hello all,
> In a nutshell, what I'd like to do is develop a utility that would print out 
> every line in a python program as the program executes (I may even just 
> want to print line numbers).  I can see hints of how to do this in pdb (and 
> more directly in the underlying bdb class) and also in the code module.
> So, 
> 1) Should I try and inherit a class and override some methods or should I 
> just pull out the relevant code and hand spin a small interpreter.
> 2) Anyone have a 20 liner solution for this?

Sorry, I couldn't find a solution that was larger than six lines:

 >>> 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)

Well, if you want to print the actual line of source you could probably
stretch it out to eight or ten lines... although I think you need
only one more line for the simplest approach.  ;-)


More information about the Python-list mailing list