How to use traceit to report function input variables in stack trace

Posted by reckoner on Stack Overflow See other posts from Stack Overflow or by reckoner
Published on 2010-04-11T13:22:18Z Indexed on 2010/04/11 14:03 UTC
Read the original article Hit count: 237

Filed under:
|
|
|

Hi,

I've been using the following code to trace the execution of my programs:

import sys
import linecache
import random

def traceit(frame, event, arg):
    if event == "line":
        lineno = frame.f_lineno
        filename = frame.f_globals["__file__"]
        if filename == "<stdin>":
            filename = "traceit.py"
        if (filename.endswith(".pyc") or
            filename.endswith(".pyo")):
            filename = filename[:-1]
        name = frame.f_globals["__name__"]
        line = linecache.getline(filename, lineno)
        print "%s:%s:%s: %s" % (name,  lineno,frame.f_code.co_name , line.rstrip())
    return traceit


def main():
    print "In main"
    for i in range(5):
        print i, random.randrange(0, 10)
    print "Done."

sys.settrace(traceit)
main()

Using this code, or something like it, is it possible to report the values of certain function arguments? In other words, the above code tells me "which" functions were called and I would like to know "what" the corresponding values of the input variables for those function calls.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about python

Related posts about debugging