Show More
@@ -1,178 +1,188 b'' | |||
|
1 | 1 | import asyncio |
|
2 | 2 | import os |
|
3 | 3 | import sys |
|
4 | 4 | |
|
5 | 5 | from IPython.core.debugger import Pdb |
|
6 | 6 | from IPython.core.completer import IPCompleter |
|
7 | 7 | from .ptutils import IPythonPTCompleter |
|
8 | 8 | from .shortcuts import create_ipython_shortcuts |
|
9 | 9 | from . import embed |
|
10 | 10 | |
|
11 | 11 | from pathlib import Path |
|
12 | 12 | from pygments.token import Token |
|
13 | from prompt_toolkit.application import create_app_session | |
|
13 | 14 | from prompt_toolkit.shortcuts.prompt import PromptSession |
|
14 | 15 | from prompt_toolkit.enums import EditingMode |
|
15 | 16 | from prompt_toolkit.formatted_text import PygmentsTokens |
|
16 | 17 | from prompt_toolkit.history import InMemoryHistory, FileHistory |
|
17 | 18 | from concurrent.futures import ThreadPoolExecutor |
|
18 | 19 | |
|
19 | 20 | from prompt_toolkit import __version__ as ptk_version |
|
20 | 21 | PTK3 = ptk_version.startswith('3.') |
|
21 | 22 | |
|
22 | 23 | |
|
23 | 24 | # we want to avoid ptk as much as possible when using subprocesses |
|
24 | 25 | # as it uses cursor positioning requests, deletes color .... |
|
25 | 26 | _use_simple_prompt = "IPY_TEST_SIMPLE_PROMPT" in os.environ |
|
26 | 27 | |
|
27 | 28 | |
|
28 | 29 | class TerminalPdb(Pdb): |
|
29 | 30 | """Standalone IPython debugger.""" |
|
30 | 31 | |
|
31 | 32 | def __init__(self, *args, pt_session_options=None, **kwargs): |
|
32 | 33 | Pdb.__init__(self, *args, **kwargs) |
|
33 | 34 | self._ptcomp = None |
|
34 | 35 | self.pt_init(pt_session_options) |
|
35 | 36 | self.thread_executor = ThreadPoolExecutor(1) |
|
36 | 37 | |
|
37 | 38 | def pt_init(self, pt_session_options=None): |
|
38 | 39 | """Initialize the prompt session and the prompt loop |
|
39 | 40 | and store them in self.pt_app and self.pt_loop. |
|
40 | 41 | |
|
41 | 42 | Additional keyword arguments for the PromptSession class |
|
42 | 43 | can be specified in pt_session_options. |
|
43 | 44 | """ |
|
44 | 45 | if pt_session_options is None: |
|
45 | 46 | pt_session_options = {} |
|
46 | 47 | |
|
47 | 48 | def get_prompt_tokens(): |
|
48 | 49 | return [(Token.Prompt, self.prompt)] |
|
49 | 50 | |
|
50 | 51 | if self._ptcomp is None: |
|
51 | 52 | compl = IPCompleter( |
|
52 | 53 | shell=self.shell, namespace={}, global_namespace={}, parent=self.shell |
|
53 | 54 | ) |
|
54 | 55 | # add a completer for all the do_ methods |
|
55 | 56 | methods_names = [m[3:] for m in dir(self) if m.startswith("do_")] |
|
56 | 57 | |
|
57 | 58 | def gen_comp(self, text): |
|
58 | 59 | return [m for m in methods_names if m.startswith(text)] |
|
59 | 60 | import types |
|
60 | 61 | newcomp = types.MethodType(gen_comp, compl) |
|
61 | 62 | compl.custom_matchers.insert(0, newcomp) |
|
62 | 63 | # end add completer. |
|
63 | 64 | |
|
64 | 65 | self._ptcomp = IPythonPTCompleter(compl) |
|
65 | 66 | |
|
66 | 67 | # setup history only when we start pdb |
|
67 | 68 | if self.shell.debugger_history is None: |
|
68 | 69 | if self.shell.debugger_history_file is not None: |
|
69 | 70 | |
|
70 | 71 | p = Path(self.shell.debugger_history_file).expanduser() |
|
71 | 72 | if not p.exists(): |
|
72 | 73 | p.touch() |
|
73 | 74 | self.debugger_history = FileHistory(os.path.expanduser(str(p))) |
|
74 | 75 | else: |
|
75 | 76 | self.debugger_history = InMemoryHistory() |
|
76 | 77 | else: |
|
77 | 78 | self.debugger_history = self.shell.debugger_history |
|
78 | 79 | |
|
79 | 80 | options = dict( |
|
80 | 81 | message=(lambda: PygmentsTokens(get_prompt_tokens())), |
|
81 | 82 | editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()), |
|
82 | 83 | key_bindings=create_ipython_shortcuts(self.shell), |
|
83 | 84 | history=self.debugger_history, |
|
84 | 85 | completer=self._ptcomp, |
|
85 | 86 | enable_history_search=True, |
|
86 | 87 | mouse_support=self.shell.mouse_support, |
|
87 | 88 | complete_style=self.shell.pt_complete_style, |
|
88 | 89 | style=getattr(self.shell, "style", None), |
|
89 | 90 | color_depth=self.shell.color_depth, |
|
90 | 91 | ) |
|
91 | 92 | |
|
92 | 93 | if not PTK3: |
|
93 | 94 | options['inputhook'] = self.shell.inputhook |
|
94 | 95 | options.update(pt_session_options) |
|
95 | 96 | if not _use_simple_prompt: |
|
96 | 97 | self.pt_loop = asyncio.new_event_loop() |
|
97 | 98 | self.pt_app = PromptSession(**options) |
|
98 | 99 | |
|
100 | def _prompt(self): | |
|
101 | """ | |
|
102 | In case another prompt_toolkit apps have to run in parallel to this one (e.g. in madbg), | |
|
103 | create_app_session must be used to prevent mixing up between them. According to the prompt_toolkit docs: | |
|
104 | ||
|
105 | If you need multiple applications running at the same time, you have to create a separate | |
|
106 | `AppSession` using a `with create_app_session():` block. | |
|
107 | """ | |
|
108 | with create_app_session(): | |
|
109 | return self.pt_app.prompt() | |
|
110 | ||
|
99 | 111 | def cmdloop(self, intro=None): |
|
100 | 112 | """Repeatedly issue a prompt, accept input, parse an initial prefix |
|
101 | 113 | off the received input, and dispatch to action methods, passing them |
|
102 | 114 | the remainder of the line as argument. |
|
103 | 115 | |
|
104 | 116 | override the same methods from cmd.Cmd to provide prompt toolkit replacement. |
|
105 | 117 | """ |
|
106 | 118 | if not self.use_rawinput: |
|
107 | 119 | raise ValueError('Sorry ipdb does not support use_rawinput=False') |
|
108 | 120 | |
|
109 | 121 | # In order to make sure that prompt, which uses asyncio doesn't |
|
110 | 122 | # interfere with applications in which it's used, we always run the |
|
111 | 123 | # prompt itself in a different thread (we can't start an event loop |
|
112 | 124 | # within an event loop). This new thread won't have any event loop |
|
113 | 125 | # running, and here we run our prompt-loop. |
|
114 | 126 | self.preloop() |
|
115 | 127 | |
|
116 | 128 | try: |
|
117 | 129 | if intro is not None: |
|
118 | 130 | self.intro = intro |
|
119 | 131 | if self.intro: |
|
120 | 132 | print(self.intro, file=self.stdout) |
|
121 | 133 | stop = None |
|
122 | 134 | while not stop: |
|
123 | 135 | if self.cmdqueue: |
|
124 | 136 | line = self.cmdqueue.pop(0) |
|
125 | 137 | else: |
|
126 | 138 | self._ptcomp.ipy_completer.namespace = self.curframe_locals |
|
127 | 139 | self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals |
|
128 | 140 | |
|
129 | 141 | # Run the prompt in a different thread. |
|
130 | 142 | if not _use_simple_prompt: |
|
131 | 143 | try: |
|
132 | line = self.thread_executor.submit( | |
|
133 | self.pt_app.prompt | |
|
134 | ).result() | |
|
144 | line = self.thread_executor.submit(self._prompt).result() | |
|
135 | 145 | except EOFError: |
|
136 | 146 | line = "EOF" |
|
137 | 147 | else: |
|
138 | 148 | line = input("ipdb> ") |
|
139 | 149 | |
|
140 | 150 | line = self.precmd(line) |
|
141 | 151 | stop = self.onecmd(line) |
|
142 | 152 | stop = self.postcmd(stop, line) |
|
143 | 153 | self.postloop() |
|
144 | 154 | except Exception: |
|
145 | 155 | raise |
|
146 | 156 | |
|
147 | 157 | def do_interact(self, arg): |
|
148 | 158 | ipshell = embed.InteractiveShellEmbed( |
|
149 | 159 | config=self.shell.config, |
|
150 | 160 | banner1="*interactive*", |
|
151 | 161 | exit_msg="*exiting interactive console...*", |
|
152 | 162 | ) |
|
153 | 163 | global_ns = self.curframe.f_globals |
|
154 | 164 | ipshell( |
|
155 | 165 | module=sys.modules.get(global_ns["__name__"], None), |
|
156 | 166 | local_ns=self.curframe_locals, |
|
157 | 167 | ) |
|
158 | 168 | |
|
159 | 169 | |
|
160 | 170 | def set_trace(frame=None): |
|
161 | 171 | """ |
|
162 | 172 | Start debugging from `frame`. |
|
163 | 173 | |
|
164 | 174 | If frame is not specified, debugging starts from caller's frame. |
|
165 | 175 | """ |
|
166 | 176 | TerminalPdb().set_trace(frame or sys._getframe().f_back) |
|
167 | 177 | |
|
168 | 178 | |
|
169 | 179 | if __name__ == '__main__': |
|
170 | 180 | import pdb |
|
171 | 181 | # IPython.core.debugger.Pdb.trace_dispatch shall not catch |
|
172 | 182 | # bdb.BdbQuit. When started through __main__ and an exception |
|
173 | 183 | # happened after hitting "c", this is needed in order to |
|
174 | 184 | # be able to quit the debugging session (see #9950). |
|
175 | 185 | old_trace_dispatch = pdb.Pdb.trace_dispatch |
|
176 | 186 | pdb.Pdb = TerminalPdb # type: ignore |
|
177 | 187 | pdb.Pdb.trace_dispatch = old_trace_dispatch # type: ignore |
|
178 | 188 | pdb.main() |
General Comments 0
You need to be logged in to leave comments.
Login now