Tuesday, January 11, 2011

Python Function Tracing

Found this after a bit of googling:
def traceit(frame, event, arg):
if event == "line":
    lineno = frame.f_lineno
    fn = frame.f_globals["__file__"]
    if fn.find("fft") != -1:
        print "file %s line %d" % (fn, lineno)
return traceit

Defining a traceit method like the one above lets you examine where the Python interpreter is going after you make functions calls. The other piece is a one-liner in the main function:

if __name__ == '__main__':
    sys.settrace(traceit)
    main ()

No comments:

Post a Comment