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