From ae4dc6dd100a095d3e5ad7e7b5e89f58ffda9d91 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Wed, 9 Jul 2008 12:18:26 +0900 Subject: [PATCH] Auxiliary debugging module. --- debug.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 debug.py diff --git a/debug.py b/debug.py new file mode 100644 index 0000000..b425e28 --- /dev/null +++ b/debug.py @@ -0,0 +1,36 @@ +'''Debugging utilities.''' + + +import sys +import traceback +import inspect + + +def excepthook(type, value, tb): + """ + Automatically start the debugger on an exception. + + See also: + - http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65287 + """ + + if hasattr(sys, 'ps1') \ + or not (sys.stdin.isatty() and sys.stdout.isatty() and sys.stderr.isatty()) \ + or type == SyntaxError or type == KeyboardInterrupt: + # we are in interactive mode or we don't have a tty-like + # device, so we call the default hook + oldexcepthook(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() + +oldexcepthook, sys.excepthook = sys.excepthook, excepthook + + +def dump(var): + sys.stderr.write(repr(var) + '\n') + -- 2.45.2