Python tracer

From Noah.org
Revision as of 00:56, 16 May 2014 by Root (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


"""This traces python code. Add a settrace call somewhere in your program:

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

import sys, os, linecache

def tracer(frame, event, arg):

    if event == "line":
        lineno = frame.f_lineno
        filename = frame.f_globals["__file__"]
        if filename[-1] != 'y': # convert .pyc and .pyo files to .py
            filename = filename[:-1]
        line = linecache.getline(filename, lineno).rstrip().replace('\t','        ')
        sys.stderr.write("%s : %04s : %s\n" % (os.path.basename(filename), lineno, line))
        sys.stderr.flush()
    return tracer