[Tutor] questions about debugging program

Alan Gauld alan.gauld at blueyonder.co.uk
Mon Aug 18 09:48:10 EDT 2003


> print "%s %d" __FILE__ __LINE__ statement
> as we used to do in C/C++, so i can go to the right line in the
right
> file easier.

AFAIK There is no easy way to do this in Python.
I think the only option is to raise an exception and catch
it immediately. The traceback object can then be queried
to find the line number where it occured - I think.

Something like:

try:   raise TypeError   # any old exception.
except TypeError: printError(e)

Where printError looks like:

def printError(anException):
    tb = sys.exc_info()
    print "line = ",tb.tb_lineno

Check the manual for the traceback module, I think there are
some functions there to help get exactly what you want displayed...

Caveat: I have never tried this just read about it! Usually I
just live without the __Line__ info. And if dealing with big
programs I usually prefer to use the dbg module to watch whats
happening rather than insert lots of print statements.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld





More information about the Tutor mailing list