|
49 | 49 | import re |
50 | 50 | import time |
51 | 51 | import functools |
| 52 | +from pprint import pprint |
52 | 53 |
|
53 | 54 | import struct |
54 | 55 | if platform.system() != 'Windows': |
@@ -160,6 +161,7 @@ def __init__(self, interface): |
160 | 161 | self.encoding = getpreferredencoding() |
161 | 162 | self.interface = interface |
162 | 163 | self.buffer = list() |
| 164 | + self.errors = "strict" |
163 | 165 |
|
164 | 166 | def __iter__(self): |
165 | 167 | return iter(self.readlines()) |
@@ -957,6 +959,13 @@ def p_key(self, key): |
957 | 959 | self.write2file() |
958 | 960 | return '' |
959 | 961 |
|
| 962 | + elif key in key_dispatch[config.debug_key]: |
| 963 | + if sys.excepthook is not debugger_hook: |
| 964 | + sys.excepthook = debugger_hook |
| 965 | + else: |
| 966 | + sys.excepthook = sys.__excepthook__ |
| 967 | + return '' |
| 968 | + |
960 | 969 | elif key in key_dispatch[config.pastebin_key]: |
961 | 970 | self.pastebin() |
962 | 971 | return '' |
@@ -1696,7 +1705,8 @@ def init_wins(scr, config): |
1696 | 1705 | (_('Save'), config.save_key), |
1697 | 1706 | (_('Pastebin'), config.pastebin_key), |
1698 | 1707 | (_('Pager'), config.last_output_key), |
1699 | | - (_('Show Source'), config.show_source_key) |
| 1708 | + (_('Show Source'), config.show_source_key), |
| 1709 | + (_('Auto Debug'), config.debug_key), |
1700 | 1710 | ) |
1701 | 1711 |
|
1702 | 1712 | message = ' '.join('<%s> %s' % (key, command) for command, key in commands |
@@ -1944,17 +1954,59 @@ def main_curses(scr, args, config, interactive=True, locals_=None, |
1944 | 1954 | return (exit_value, clirepl.getstdout()) |
1945 | 1955 |
|
1946 | 1956 |
|
| 1957 | +def prettydisplayhook(obj): |
| 1958 | + pprint(obj) |
| 1959 | + |
| 1960 | + |
| 1961 | +def debugger_hook(exc, value, tb): |
| 1962 | + |
| 1963 | + if exc in (SyntaxError, IndentationError, KeyboardInterrupt): |
| 1964 | + sys.__excepthook__(exc, value, tb) |
| 1965 | + return |
| 1966 | + |
| 1967 | + global stdscr |
| 1968 | + from bpython import debugger |
| 1969 | + |
| 1970 | + orig_stdin = sys.stdin |
| 1971 | + orig_stdout = sys.stdout |
| 1972 | + orig_stderr = sys.stderr |
| 1973 | + |
| 1974 | + sys.stdin = sys.__stdin__ |
| 1975 | + sys.stderr = sys.__stderr__ |
| 1976 | + sys.stdout = sys.__stdout__ |
| 1977 | + |
| 1978 | + stdscr.keypad(0) |
| 1979 | + curses.echo() |
| 1980 | + curses.nocbreak() |
| 1981 | + curses.endwin() |
| 1982 | + try: |
| 1983 | + debugger.post_mortem(tb, exc, value) |
| 1984 | + finally: |
| 1985 | + sys.stdin = orig_stdin |
| 1986 | + sys.stdout = orig_stdout |
| 1987 | + sys.stderr = orig_stderr |
| 1988 | + curses.cbreak() |
| 1989 | + curses.noecho() |
| 1990 | + stdscr.keypad(1) |
| 1991 | + if stdscr is None: |
| 1992 | + stdscr = curses.initscr() |
| 1993 | + |
| 1994 | + |
1947 | 1995 | def main(args=None, locals_=None, banner=None): |
1948 | 1996 | translations.init() |
1949 | 1997 |
|
1950 | | - |
1951 | 1998 | config, options, exec_args = bpython.args.parse(args) |
1952 | 1999 |
|
1953 | 2000 | # Save stdin, stdout and stderr for later restoration |
1954 | 2001 | orig_stdin = sys.stdin |
1955 | 2002 | orig_stdout = sys.stdout |
1956 | 2003 | orig_stderr = sys.stderr |
1957 | 2004 |
|
| 2005 | + if options.debugger: |
| 2006 | + sys.excepthook = debugger_hook |
| 2007 | + if options.pretty: |
| 2008 | + sys.displayhook = prettydisplayhook |
| 2009 | + |
1958 | 2010 | try: |
1959 | 2011 | (exit_value, output) = curses_wrapper( |
1960 | 2012 | main_curses, exec_args, config, options.interactive, locals_, |
|
0 commit comments