Today is: 11 January, 2012
Check todays hot topics

Drop to Interpreter / Debug Mode in Python

The following code will drop you back into the Python Interpreter or "debug mode" anytime debug_mode() is called or an unhandled exception occurs. Useful for debugging when you want to automate a portion of a script then get manual control.

import sys,os,readline
 
def debug_exception(type, value, traceback):
        # Restore redirected standard I/O
        sys.stdin = sys.__stdin__
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__
        # Kick the interpreter into interactive mode and call the original
        # exception handler.
        os.environ['PYTHONINSPECT'] = '1'
        sys.__excepthook__(type, value, traceback)
 
def debug_mode():
        raise RuntimeError('Gogo Interactive')
 
sys.excepthook = debug_exception
 
# Insert debug_mode() call where you want to drop back to Interpreter.
# If any unhandled exceptions occur it will automatically call debug_mode()
 
debug_mode()