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