##// END OF EJS Templates
Get history file from shell to debugger if it exists.
Can Sarigol -
Show More
@@ -1,165 +1,167 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 else:
72 self.debugger_history = self.shell.debugger_history
71 73
72 74 options = dict(
73 75 message=(lambda: PygmentsTokens(get_prompt_tokens())),
74 76 editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
75 77 key_bindings=create_ipython_shortcuts(self.shell),
76 78 history=self.debugger_history,
77 79 completer=self._ptcomp,
78 80 enable_history_search=True,
79 81 mouse_support=self.shell.mouse_support,
80 82 complete_style=self.shell.pt_complete_style,
81 83 style=getattr(self.shell, "style", None),
82 84 color_depth=self.shell.color_depth,
83 85 )
84 86
85 87 if not PTK3:
86 88 options['inputhook'] = self.shell.inputhook
87 89 options.update(pt_session_options)
88 90 self.pt_loop = asyncio.new_event_loop()
89 91 self.pt_app = PromptSession(**options)
90 92
91 93 def cmdloop(self, intro=None):
92 94 """Repeatedly issue a prompt, accept input, parse an initial prefix
93 95 off the received input, and dispatch to action methods, passing them
94 96 the remainder of the line as argument.
95 97
96 98 override the same methods from cmd.Cmd to provide prompt toolkit replacement.
97 99 """
98 100 if not self.use_rawinput:
99 101 raise ValueError('Sorry ipdb does not support use_rawinput=False')
100 102
101 103 # In order to make sure that prompt, which uses asyncio doesn't
102 104 # interfere with applications in which it's used, we always run the
103 105 # prompt itself in a different thread (we can't start an event loop
104 106 # within an event loop). This new thread won't have any event loop
105 107 # running, and here we run our prompt-loop.
106 108 self.preloop()
107 109
108 110 try:
109 111 if intro is not None:
110 112 self.intro = intro
111 113 if self.intro:
112 114 print(self.intro, file=self.stdout)
113 115 stop = None
114 116 while not stop:
115 117 if self.cmdqueue:
116 118 line = self.cmdqueue.pop(0)
117 119 else:
118 120 self._ptcomp.ipy_completer.namespace = self.curframe_locals
119 121 self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals
120 122
121 123 # Run the prompt in a different thread.
122 124 try:
123 125 line = self.thread_executor.submit(self.pt_app.prompt).result()
124 126 except EOFError:
125 127 line = "EOF"
126 128
127 129 line = self.precmd(line)
128 130 stop = self.onecmd(line)
129 131 stop = self.postcmd(stop, line)
130 132 self.postloop()
131 133 except Exception:
132 134 raise
133 135
134 136 def do_interact(self, arg):
135 137 ipshell = embed.InteractiveShellEmbed(
136 138 config=self.shell.config,
137 139 banner1="*interactive*",
138 140 exit_msg="*exiting interactive console...*",
139 141 )
140 142 global_ns = self.curframe.f_globals
141 143 ipshell(
142 144 module=sys.modules.get(global_ns["__name__"], None),
143 145 local_ns=self.curframe_locals,
144 146 )
145 147
146 148
147 149 def set_trace(frame=None):
148 150 """
149 151 Start debugging from `frame`.
150 152
151 153 If frame is not specified, debugging starts from caller's frame.
152 154 """
153 155 TerminalPdb().set_trace(frame or sys._getframe().f_back)
154 156
155 157
156 158 if __name__ == '__main__':
157 159 import pdb
158 160 # IPython.core.debugger.Pdb.trace_dispatch shall not catch
159 161 # bdb.BdbQuit. When started through __main__ and an exception
160 162 # happened after hitting "c", this is needed in order to
161 163 # be able to quit the debugging session (see #9950).
162 164 old_trace_dispatch = pdb.Pdb.trace_dispatch
163 165 pdb.Pdb = TerminalPdb # type: ignore
164 166 pdb.Pdb.trace_dispatch = old_trace_dispatch # type: ignore
165 167 pdb.main()
General Comments 0
You need to be logged in to leave comments. Login now