##// END OF EJS Templates
Add class-docstring for terminal.debugger.TerminalPdb....
Terry Davis -
Show More
@@ -1,118 +1,120 b''
1 import signal
1 import signal
2 import sys
2 import sys
3
3
4 from IPython.core.debugger import Pdb
4 from IPython.core.debugger import Pdb
5
5
6 from IPython.core.completer import IPCompleter
6 from IPython.core.completer import IPCompleter
7 from .ptutils import IPythonPTCompleter
7 from .ptutils import IPythonPTCompleter
8 from .shortcuts import suspend_to_bg, cursor_in_leading_ws
8 from .shortcuts import suspend_to_bg, cursor_in_leading_ws
9
9
10 from prompt_toolkit.enums import DEFAULT_BUFFER
10 from prompt_toolkit.enums import DEFAULT_BUFFER
11 from prompt_toolkit.filters import (Condition, has_focus, has_selection,
11 from prompt_toolkit.filters import (Condition, has_focus, has_selection,
12 vi_insert_mode, emacs_insert_mode)
12 vi_insert_mode, emacs_insert_mode)
13 from prompt_toolkit.key_binding import KeyBindings
13 from prompt_toolkit.key_binding import KeyBindings
14 from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline
14 from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline
15 from pygments.token import Token
15 from pygments.token import Token
16 from prompt_toolkit.shortcuts.prompt import PromptSession
16 from prompt_toolkit.shortcuts.prompt import PromptSession
17 from prompt_toolkit.enums import EditingMode
17 from prompt_toolkit.enums import EditingMode
18 from prompt_toolkit.formatted_text import PygmentsTokens
18 from prompt_toolkit.formatted_text import PygmentsTokens
19
19
20
20
21 class TerminalPdb(Pdb):
21 class TerminalPdb(Pdb):
22 """Standalone IPython debugger."""
23
22 def __init__(self, *args, **kwargs):
24 def __init__(self, *args, **kwargs):
23 Pdb.__init__(self, *args, **kwargs)
25 Pdb.__init__(self, *args, **kwargs)
24 self._ptcomp = None
26 self._ptcomp = None
25 self.pt_init()
27 self.pt_init()
26
28
27 def pt_init(self):
29 def pt_init(self):
28 def get_prompt_tokens():
30 def get_prompt_tokens():
29 return [(Token.Prompt, self.prompt)]
31 return [(Token.Prompt, self.prompt)]
30
32
31 if self._ptcomp is None:
33 if self._ptcomp is None:
32 compl = IPCompleter(shell=self.shell,
34 compl = IPCompleter(shell=self.shell,
33 namespace={},
35 namespace={},
34 global_namespace={},
36 global_namespace={},
35 parent=self.shell,
37 parent=self.shell,
36 )
38 )
37 self._ptcomp = IPythonPTCompleter(compl)
39 self._ptcomp = IPythonPTCompleter(compl)
38
40
39 kb = KeyBindings()
41 kb = KeyBindings()
40 supports_suspend = Condition(lambda: hasattr(signal, 'SIGTSTP'))
42 supports_suspend = Condition(lambda: hasattr(signal, 'SIGTSTP'))
41 kb.add('c-z', filter=supports_suspend)(suspend_to_bg)
43 kb.add('c-z', filter=supports_suspend)(suspend_to_bg)
42
44
43 if self.shell.display_completions == 'readlinelike':
45 if self.shell.display_completions == 'readlinelike':
44 kb.add('tab', filter=(has_focus(DEFAULT_BUFFER)
46 kb.add('tab', filter=(has_focus(DEFAULT_BUFFER)
45 & ~has_selection
47 & ~has_selection
46 & vi_insert_mode | emacs_insert_mode
48 & vi_insert_mode | emacs_insert_mode
47 & ~cursor_in_leading_ws
49 & ~cursor_in_leading_ws
48 ))(display_completions_like_readline)
50 ))(display_completions_like_readline)
49
51
50 self.pt_app = PromptSession(
52 self.pt_app = PromptSession(
51 message=(lambda: PygmentsTokens(get_prompt_tokens())),
53 message=(lambda: PygmentsTokens(get_prompt_tokens())),
52 editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
54 editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
53 key_bindings=kb,
55 key_bindings=kb,
54 history=self.shell.debugger_history,
56 history=self.shell.debugger_history,
55 completer=self._ptcomp,
57 completer=self._ptcomp,
56 enable_history_search=True,
58 enable_history_search=True,
57 mouse_support=self.shell.mouse_support,
59 mouse_support=self.shell.mouse_support,
58 complete_style=self.shell.pt_complete_style,
60 complete_style=self.shell.pt_complete_style,
59 style=self.shell.style,
61 style=self.shell.style,
60 inputhook=self.shell.inputhook,
62 inputhook=self.shell.inputhook,
61 color_depth=self.shell.color_depth,
63 color_depth=self.shell.color_depth,
62 )
64 )
63
65
64 def cmdloop(self, intro=None):
66 def cmdloop(self, intro=None):
65 """Repeatedly issue a prompt, accept input, parse an initial prefix
67 """Repeatedly issue a prompt, accept input, parse an initial prefix
66 off the received input, and dispatch to action methods, passing them
68 off the received input, and dispatch to action methods, passing them
67 the remainder of the line as argument.
69 the remainder of the line as argument.
68
70
69 override the same methods from cmd.Cmd to provide prompt toolkit replacement.
71 override the same methods from cmd.Cmd to provide prompt toolkit replacement.
70 """
72 """
71 if not self.use_rawinput:
73 if not self.use_rawinput:
72 raise ValueError('Sorry ipdb does not support use_rawinput=False')
74 raise ValueError('Sorry ipdb does not support use_rawinput=False')
73
75
74 self.preloop()
76 self.preloop()
75
77
76 try:
78 try:
77 if intro is not None:
79 if intro is not None:
78 self.intro = intro
80 self.intro = intro
79 if self.intro:
81 if self.intro:
80 self.stdout.write(str(self.intro)+"\n")
82 self.stdout.write(str(self.intro)+"\n")
81 stop = None
83 stop = None
82 while not stop:
84 while not stop:
83 if self.cmdqueue:
85 if self.cmdqueue:
84 line = self.cmdqueue.pop(0)
86 line = self.cmdqueue.pop(0)
85 else:
87 else:
86 self._ptcomp.ipy_completer.namespace = self.curframe_locals
88 self._ptcomp.ipy_completer.namespace = self.curframe_locals
87 self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals
89 self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals
88 try:
90 try:
89 line = self.pt_app.prompt() # reset_current_buffer=True)
91 line = self.pt_app.prompt() # reset_current_buffer=True)
90 except EOFError:
92 except EOFError:
91 line = 'EOF'
93 line = 'EOF'
92 line = self.precmd(line)
94 line = self.precmd(line)
93 stop = self.onecmd(line)
95 stop = self.onecmd(line)
94 stop = self.postcmd(stop, line)
96 stop = self.postcmd(stop, line)
95 self.postloop()
97 self.postloop()
96 except Exception:
98 except Exception:
97 raise
99 raise
98
100
99
101
100 def set_trace(frame=None):
102 def set_trace(frame=None):
101 """
103 """
102 Start debugging from `frame`.
104 Start debugging from `frame`.
103
105
104 If frame is not specified, debugging starts from caller's frame.
106 If frame is not specified, debugging starts from caller's frame.
105 """
107 """
106 TerminalPdb().set_trace(frame or sys._getframe().f_back)
108 TerminalPdb().set_trace(frame or sys._getframe().f_back)
107
109
108
110
109 if __name__ == '__main__':
111 if __name__ == '__main__':
110 import pdb
112 import pdb
111 # IPython.core.debugger.Pdb.trace_dispatch shall not catch
113 # IPython.core.debugger.Pdb.trace_dispatch shall not catch
112 # bdb.BdbQuit. When started through __main__ and an exception
114 # bdb.BdbQuit. When started through __main__ and an exception
113 # happened after hitting "c", this is needed in order to
115 # happened after hitting "c", this is needed in order to
114 # be able to quit the debugging session (see #9950).
116 # be able to quit the debugging session (see #9950).
115 old_trace_dispatch = pdb.Pdb.trace_dispatch
117 old_trace_dispatch = pdb.Pdb.trace_dispatch
116 pdb.Pdb = TerminalPdb
118 pdb.Pdb = TerminalPdb
117 pdb.Pdb.trace_dispatch = old_trace_dispatch
119 pdb.Pdb.trace_dispatch = old_trace_dispatch
118 pdb.main()
120 pdb.main()
General Comments 0
You need to be logged in to leave comments. Login now