José Fonseca | fd304a2 | 2008-07-10 07:29:18 +0900 | [diff] [blame^] | 1 | ############################################################################# |
| 2 | # |
| 3 | # Copyright 2007 Jose Fonseca |
| 4 | # |
| 5 | # This program is free software: you can redistribute it and/or modify it |
| 6 | # under the terms of the GNU Lesser General Public License as published |
| 7 | # by the Free Software Foundation, either version 3 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU Lesser General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU Lesser General Public License |
| 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | # |
| 18 | ############################################################################# |
| 19 | |
José Fonseca | ae4dc6d | 2008-07-09 12:18:26 +0900 | [diff] [blame] | 20 | '''Debugging utilities.''' |
| 21 | |
| 22 | |
| 23 | import sys |
| 24 | import traceback |
| 25 | import inspect |
| 26 | |
| 27 | |
| 28 | def excepthook(type, value, tb): |
| 29 | """ |
| 30 | Automatically start the debugger on an exception. |
| 31 | |
| 32 | See also: |
| 33 | - http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65287 |
| 34 | """ |
| 35 | |
| 36 | if hasattr(sys, 'ps1') \ |
| 37 | or not (sys.stdin.isatty() and sys.stdout.isatty() and sys.stderr.isatty()) \ |
| 38 | or type == SyntaxError or type == KeyboardInterrupt: |
| 39 | # we are in interactive mode or we don't have a tty-like |
| 40 | # device, so we call the default hook |
| 41 | oldexcepthook(type, value, tb) |
| 42 | else: |
| 43 | import traceback, pdb |
| 44 | # we are NOT in interactive mode, print the exception... |
| 45 | traceback.print_exception(type, value, tb) |
| 46 | print |
| 47 | # ...then start the debugger in post-mortem mode. |
| 48 | pdb.pm() |
| 49 | |
| 50 | oldexcepthook, sys.excepthook = sys.excepthook, excepthook |
| 51 | |
| 52 | |
| 53 | def dump(var): |
| 54 | sys.stderr.write(repr(var) + '\n') |
| 55 | |