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