Wednesday, June 12, 2013

Python's trace module and chained decorators

By Vasudev Ram

I saw this example of using chained decorators on StackOverflow some time ago.

The trace module from the Python standard library allows you "to trace program execution, generate annotated statement coverage listings, print caller/callee relationships and list functions executed during a program run. It can be used in another program or from the command line." - from the trace module page (italics mine).

I used the trace module to trace the execution of statements in the chained decorators example (I modified the StackOverflow example a bit to add a main() function):
# An example of chained decorators.
# From Stack Overflow question at:

def makebold(fn):
    def wrapped():
        return "<b>" + fn() + "</b>"
    return wrapped

def makeitalic(fn):
    def wrapped():
        return "<i>" + fn() + "</i>"
    return wrapped

def hello():
    return "hello world"

def main():
    print hello()
if __name__ == '__main__':
Running the program as follows, under control of the trace module:
python -m trace -t
gives this output:
--- modulename: test_chained_decorators, funcname: def makebold(fn): def makeitalic(fn): @makebold @makeitalic
 --- modulename: test_chained_decorators, funcname: makeitalic     def wrapped():     return wrapped
 --- modulename: test_chained_decorators, funcname: makebold     def wrapped():     return wrapped def main(): if __name__ == '__main__':     main()
 --- modulename: test_chained_decorators, funcname: main     print hello()
 --- modulename: test_chained_decorators, funcname: wrapped         return "<b>" + fn() + "</b>"
 --- modulename: test_chained_decorators, funcname: wrapped         return "<i>" + fn() + "</i>"
 --- modulename: test_chained_decorators, funcname: hello     return "hello world"
<b><i>hello world</i></b>
 --- modulename: trace, funcname: _unsettrace         sys.settrace(None)

An interesting detail: if you look carefully at the trace output, you'll see that def statements get printed, but the statements nested inside each def, do not get printed immediately after the def's are printed (though they occur immediately after the defs, in the text of the program); I guess this is due to the fact that when Python executes a program, def's get executed, but "executing a def" is not the same as calling the defined function; it means that the function definition is parsed/compiled and stored, for later execution whenever the function is called. You can see in the output that the statements inside each def are actually called only when the function is called from other code.

The trace module has other options apart from -t; see the Python documentation for it, linked above in this post.

Overall, the trace module can be helpful to understand and debug Python programs, your own or those of others.

- Vasudev Ram - Dancing Bison Enterprises

Contact me

No comments: