Show More
@@ -1,118 +1,120 b'' | |||
|
1 | 1 | import signal |
|
2 | 2 | import sys |
|
3 | 3 | |
|
4 | 4 | from IPython.core.debugger import Pdb |
|
5 | 5 | |
|
6 | 6 | from IPython.core.completer import IPCompleter |
|
7 | 7 | from .ptutils import IPythonPTCompleter |
|
8 | 8 | from .shortcuts import suspend_to_bg, cursor_in_leading_ws |
|
9 | 9 | |
|
10 | 10 | from prompt_toolkit.enums import DEFAULT_BUFFER |
|
11 | 11 | from prompt_toolkit.filters import (Condition, has_focus, has_selection, |
|
12 | 12 | vi_insert_mode, emacs_insert_mode) |
|
13 | 13 | from prompt_toolkit.key_binding import KeyBindings |
|
14 | 14 | from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline |
|
15 | 15 | from pygments.token import Token |
|
16 | 16 | from prompt_toolkit.shortcuts.prompt import PromptSession |
|
17 | 17 | from prompt_toolkit.enums import EditingMode |
|
18 | 18 | from prompt_toolkit.formatted_text import PygmentsTokens |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | class TerminalPdb(Pdb): |
|
22 | """Standalone IPython debugger.""" | |
|
23 | ||
|
22 | 24 | def __init__(self, *args, **kwargs): |
|
23 | 25 | Pdb.__init__(self, *args, **kwargs) |
|
24 | 26 | self._ptcomp = None |
|
25 | 27 | self.pt_init() |
|
26 | 28 | |
|
27 | 29 | def pt_init(self): |
|
28 | 30 | def get_prompt_tokens(): |
|
29 | 31 | return [(Token.Prompt, self.prompt)] |
|
30 | 32 | |
|
31 | 33 | if self._ptcomp is None: |
|
32 | 34 | compl = IPCompleter(shell=self.shell, |
|
33 | 35 | namespace={}, |
|
34 | 36 | global_namespace={}, |
|
35 | 37 | parent=self.shell, |
|
36 | 38 | ) |
|
37 | 39 | self._ptcomp = IPythonPTCompleter(compl) |
|
38 | 40 | |
|
39 | 41 | kb = KeyBindings() |
|
40 | 42 | supports_suspend = Condition(lambda: hasattr(signal, 'SIGTSTP')) |
|
41 | 43 | kb.add('c-z', filter=supports_suspend)(suspend_to_bg) |
|
42 | 44 | |
|
43 | 45 | if self.shell.display_completions == 'readlinelike': |
|
44 | 46 | kb.add('tab', filter=(has_focus(DEFAULT_BUFFER) |
|
45 | 47 | & ~has_selection |
|
46 | 48 | & vi_insert_mode | emacs_insert_mode |
|
47 | 49 | & ~cursor_in_leading_ws |
|
48 | 50 | ))(display_completions_like_readline) |
|
49 | 51 | |
|
50 | 52 | self.pt_app = PromptSession( |
|
51 | 53 | message=(lambda: PygmentsTokens(get_prompt_tokens())), |
|
52 | 54 | editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()), |
|
53 | 55 | key_bindings=kb, |
|
54 | 56 | history=self.shell.debugger_history, |
|
55 | 57 | completer=self._ptcomp, |
|
56 | 58 | enable_history_search=True, |
|
57 | 59 | mouse_support=self.shell.mouse_support, |
|
58 | 60 | complete_style=self.shell.pt_complete_style, |
|
59 | 61 | style=self.shell.style, |
|
60 | 62 | inputhook=self.shell.inputhook, |
|
61 | 63 | color_depth=self.shell.color_depth, |
|
62 | 64 | ) |
|
63 | 65 | |
|
64 | 66 | def cmdloop(self, intro=None): |
|
65 | 67 | """Repeatedly issue a prompt, accept input, parse an initial prefix |
|
66 | 68 | off the received input, and dispatch to action methods, passing them |
|
67 | 69 | the remainder of the line as argument. |
|
68 | 70 | |
|
69 | 71 | override the same methods from cmd.Cmd to provide prompt toolkit replacement. |
|
70 | 72 | """ |
|
71 | 73 | if not self.use_rawinput: |
|
72 | 74 | raise ValueError('Sorry ipdb does not support use_rawinput=False') |
|
73 | 75 | |
|
74 | 76 | self.preloop() |
|
75 | 77 | |
|
76 | 78 | try: |
|
77 | 79 | if intro is not None: |
|
78 | 80 | self.intro = intro |
|
79 | 81 | if self.intro: |
|
80 | 82 | self.stdout.write(str(self.intro)+"\n") |
|
81 | 83 | stop = None |
|
82 | 84 | while not stop: |
|
83 | 85 | if self.cmdqueue: |
|
84 | 86 | line = self.cmdqueue.pop(0) |
|
85 | 87 | else: |
|
86 | 88 | self._ptcomp.ipy_completer.namespace = self.curframe_locals |
|
87 | 89 | self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals |
|
88 | 90 | try: |
|
89 | 91 | line = self.pt_app.prompt() # reset_current_buffer=True) |
|
90 | 92 | except EOFError: |
|
91 | 93 | line = 'EOF' |
|
92 | 94 | line = self.precmd(line) |
|
93 | 95 | stop = self.onecmd(line) |
|
94 | 96 | stop = self.postcmd(stop, line) |
|
95 | 97 | self.postloop() |
|
96 | 98 | except Exception: |
|
97 | 99 | raise |
|
98 | 100 | |
|
99 | 101 | |
|
100 | 102 | def set_trace(frame=None): |
|
101 | 103 | """ |
|
102 | 104 | Start debugging from `frame`. |
|
103 | 105 | |
|
104 | 106 | If frame is not specified, debugging starts from caller's frame. |
|
105 | 107 | """ |
|
106 | 108 | TerminalPdb().set_trace(frame or sys._getframe().f_back) |
|
107 | 109 | |
|
108 | 110 | |
|
109 | 111 | if __name__ == '__main__': |
|
110 | 112 | import pdb |
|
111 | 113 | # IPython.core.debugger.Pdb.trace_dispatch shall not catch |
|
112 | 114 | # bdb.BdbQuit. When started through __main__ and an exception |
|
113 | 115 | # happened after hitting "c", this is needed in order to |
|
114 | 116 | # be able to quit the debugging session (see #9950). |
|
115 | 117 | old_trace_dispatch = pdb.Pdb.trace_dispatch |
|
116 | 118 | pdb.Pdb = TerminalPdb |
|
117 | 119 | pdb.Pdb.trace_dispatch = old_trace_dispatch |
|
118 | 120 | pdb.main() |
General Comments 0
You need to be logged in to leave comments.
Login now