extended traceback printer

The standard Python traceback module provides very useful functions to produce useful information about where and why an error occurred. Traceback objects actually contain a great deal more information than the traceback module displays, however. That information can greatly assist in detecting the cause of your error.

There is a Python recipe that, while not an interactive debugger, makes it easier to debug Python scripts within SciTE.

Here’s an example of an extended traceback printer you might use, followed by a usage example.

import sys,traceback
defprint_exc_plus():
    “””
    Print the usual traceback information, followed by a listing of all the
    local variables in each frame.
    “””
    tb=sys.exc_info()[2]
    stack=[]
    
    whiletb:
        stack.append(tb.tb_frame)
        tb=tb.tb_next
    traceback.print_exc()
    print“Locals by frame, innermost last”
    forframeinstack:
        print
        print“Frame %s in %s at line %s”%(frame.f_code.co_name,
                                             frame.f_code.co_filename,
                                             frame.f_lineno)
        forkey,valueinframe.f_locals.items():
            print“t%20s = “%key,
            #We have to be careful not to cause a new error in our error
            #printer! Calling str() on an unknown object could cause an
            #error we don’t want.
            try:                   
                printvalue
            except:
                print“<ERROR WHILE PRINTING VALUE>”
        
try:
        iflen(sys.argv)>1:
                length=len(sys.argv)
                foriinrange(1,length):       
                        sys.argv[i1]=sys.argv[i]
                delsys.argv[length1]
                execfile(sys.argv[0])
except:
        print_exc_plus()