Friday, February 13, 2009

Getting a Python Traceback without an Exception

Any Python programmer will be familiar with the stack traceback associated with an exception; most will be particularly familiar with a traceback being displayed for an unhandled exception. This is extremely useful in finding and resolving the issue in the code. However, for a long time now, I've often thought that it would be extremely useful to be able to get a stack traceback at any arbitrary point without needing an exception, which would aid in determining what code path was taken to get to a particular function call. I knew this was possible - Python does allow you to get at the stack frames - but I thought I might have to do a bit of work to obtain a nice traceback. Finally, figuring someone else must have wanted this, I did a bit of Googling and turned up this article. I somehow missed print_stack(), format_stack() and extract_stack() in the traceback module. :) Sooo easy! :)

No comments:

Post a Comment