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