José Fonseca | ae4dc6d | 2008-07-09 12:18:26 +0900 | [diff] [blame] | 1 | '''Debugging utilities.''' |
| 2 | |
| 3 | |
| 4 | import sys |
| 5 | import traceback |
| 6 | import inspect |
| 7 | |
| 8 | |
| 9 | def 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 | |
| 31 | oldexcepthook, sys.excepthook = sys.excepthook, excepthook |
| 32 | |
| 33 | |
| 34 | def dump(var): |
| 35 | sys.stderr.write(repr(var) + '\n') |
| 36 | |