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