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