blob: b425e28e3373de0c9068669f99db3fc4be529fab [file] [log] [blame]
José Fonsecaae4dc6d2008-07-09 12:18:26 +09001'''Debugging utilities.'''
2
3
4import sys
5import traceback
6import inspect
7
8
9def excepthook(type, value, tb):
10 """
11 Automatically start the debugger on an exception.
12
13 See also:
14 - http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65287
15 """
16
17 if hasattr(sys, 'ps1') \
18 or not (sys.stdin.isatty() and sys.stdout.isatty() and sys.stderr.isatty()) \
19 or type == SyntaxError or type == KeyboardInterrupt:
20 # we are in interactive mode or we don't have a tty-like
21 # device, so we call the default hook
22 oldexcepthook(type, value, tb)
23 else:
24 import traceback, pdb
25 # we are NOT in interactive mode, print the exception...
26 traceback.print_exception(type, value, tb)
27 print
28 # ...then start the debugger in post-mortem mode.
29 pdb.pm()
30
31oldexcepthook, sys.excepthook = sys.excepthook, excepthook
32
33
34def dump(var):
35 sys.stderr.write(repr(var) + '\n')
36