Published in 22:41 of 01/02/2013 by

Published in 22:41 of 01/02/2013

←Home

Automatically start the debugger on an exception

When Python runs a script and an uncatched exception is raised, a traceback is printed and the script is terminated. Python2.1 has introduced sys.excepthook, which can be used to override the handling of uncaught exceptions. This allows to automatically start the debugger on an unexpected exception, even if python is not running in interactive mode.

# code snippet, to be included in 'sitecustomize.py'
import sys

def info(type, value, tb):
   if hasattr(sys, 'ps1') or not sys.stderr.isatty():
      # we are in interactive mode or we don't have a tty-like
      # device, so we call the default hook
      sys.__excepthook__(type, value, tb)
   else:
      import traceback, pdb
      # we are NOT in interactive mode, print the exception...
      traceback.print_exception(type, value, tb)
      print
      # ...then start the debugger in post-mortem mode.
      pdb.pm()

sys.excepthook = info

The above snipper can be easily included in your editor snippets and you can set it on top of your files, or even better you can include on your sitecustomize.py

NOTE: Use: import pywin.debugger and pywin.debugger.pm() if you want a gui

Based on this stacj overflow thread. http://code.activestate.com/recipes/65287/

  • Dynaconf 1.0.x released - Layered configuration system for python with flask and django support in python · 14:49 of 05/31/2018
  • py2rs - from Python to Rust - Reference Guide in python · 21:23 of 11/28/2017
  • Simple Login Extension for Flask in flask · 23:46 of 08/23/2017

  • comments powered by Disqus Go Top