February 25, 2010
Debugging Django

Often you find yourself dealing with an error that only occurs in certain circumstances—a function might be called from dozens of different places in your program but only runs in to trouble in a very specific case. You can use the traceback module to log the current stack, which will allow you to tell how a function was called when something went wrong:

import logging, traceback, pprint    def my_buggy_function(arg):  ...  if error_condition:  stack = pprint.pformat(traceback.extract_stack())  logging.debug('An error occurred: %s' % stack)

The tuple returned by traceback.extract_stack() includes line numbers, function names and paths to Python files so you can use it to reconstruct a good amount of information about your program.

Posted via web from 原宿工業大学 | Comment »