##// END OF EJS Templates
Merge pull request #9950 from mbyt/enable_quit_for_debugging_with_minus_m_IPython.terminal.debugger...
Fernando Perez -
r22905:3ad65eb3 merge
parent child Browse files
Show More
@@ -1,90 +1,96 b''
1 from IPython.core.debugger import Pdb
1 from IPython.core.debugger import Pdb
2
2
3 from IPython.core.completer import IPCompleter
3 from IPython.core.completer import IPCompleter
4 from .ptutils import IPythonPTCompleter
4 from .ptutils import IPythonPTCompleter
5
5
6 from prompt_toolkit.token import Token
6 from prompt_toolkit.token import Token
7 from prompt_toolkit.shortcuts import create_prompt_application
7 from prompt_toolkit.shortcuts import create_prompt_application
8 from prompt_toolkit.interface import CommandLineInterface
8 from prompt_toolkit.interface import CommandLineInterface
9 from prompt_toolkit.enums import EditingMode
9 from prompt_toolkit.enums import EditingMode
10 import sys
10 import sys
11
11
12
12
13 class TerminalPdb(Pdb):
13 class TerminalPdb(Pdb):
14 def __init__(self, *args, **kwargs):
14 def __init__(self, *args, **kwargs):
15 Pdb.__init__(self, *args, **kwargs)
15 Pdb.__init__(self, *args, **kwargs)
16 self._ptcomp = None
16 self._ptcomp = None
17 self.pt_init()
17 self.pt_init()
18
18
19 def pt_init(self):
19 def pt_init(self):
20 def get_prompt_tokens(cli):
20 def get_prompt_tokens(cli):
21 return [(Token.Prompt, self.prompt)]
21 return [(Token.Prompt, self.prompt)]
22
22
23 if self._ptcomp is None:
23 if self._ptcomp is None:
24 compl = IPCompleter(shell=self.shell,
24 compl = IPCompleter(shell=self.shell,
25 namespace={},
25 namespace={},
26 global_namespace={},
26 global_namespace={},
27 use_readline=False,
27 use_readline=False,
28 parent=self.shell,
28 parent=self.shell,
29 )
29 )
30 self._ptcomp = IPythonPTCompleter(compl)
30 self._ptcomp = IPythonPTCompleter(compl)
31
31
32 self._pt_app = create_prompt_application(
32 self._pt_app = create_prompt_application(
33 editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
33 editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
34 history=self.shell.debugger_history,
34 history=self.shell.debugger_history,
35 completer= self._ptcomp,
35 completer= self._ptcomp,
36 enable_history_search=True,
36 enable_history_search=True,
37 mouse_support=self.shell.mouse_support,
37 mouse_support=self.shell.mouse_support,
38 get_prompt_tokens=get_prompt_tokens
38 get_prompt_tokens=get_prompt_tokens
39 )
39 )
40 self.pt_cli = CommandLineInterface(self._pt_app, eventloop=self.shell._eventloop)
40 self.pt_cli = CommandLineInterface(self._pt_app, eventloop=self.shell._eventloop)
41
41
42 def cmdloop(self, intro=None):
42 def cmdloop(self, intro=None):
43 """Repeatedly issue a prompt, accept input, parse an initial prefix
43 """Repeatedly issue a prompt, accept input, parse an initial prefix
44 off the received input, and dispatch to action methods, passing them
44 off the received input, and dispatch to action methods, passing them
45 the remainder of the line as argument.
45 the remainder of the line as argument.
46
46
47 override the same methods from cmd.Cmd to provide prompt toolkit replacement.
47 override the same methods from cmd.Cmd to provide prompt toolkit replacement.
48 """
48 """
49 if not self.use_rawinput:
49 if not self.use_rawinput:
50 raise ValueError('Sorry ipdb does not support use_rawinput=False')
50 raise ValueError('Sorry ipdb does not support use_rawinput=False')
51
51
52 self.preloop()
52 self.preloop()
53
53
54 try:
54 try:
55 if intro is not None:
55 if intro is not None:
56 self.intro = intro
56 self.intro = intro
57 if self.intro:
57 if self.intro:
58 self.stdout.write(str(self.intro)+"\n")
58 self.stdout.write(str(self.intro)+"\n")
59 stop = None
59 stop = None
60 while not stop:
60 while not stop:
61 if self.cmdqueue:
61 if self.cmdqueue:
62 line = self.cmdqueue.pop(0)
62 line = self.cmdqueue.pop(0)
63 else:
63 else:
64 self._ptcomp.ipy_completer.namespace = self.curframe_locals
64 self._ptcomp.ipy_completer.namespace = self.curframe_locals
65 self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals
65 self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals
66 try:
66 try:
67 line = self.pt_cli.run(reset_current_buffer=True).text
67 line = self.pt_cli.run(reset_current_buffer=True).text
68 except EOFError:
68 except EOFError:
69 line = 'EOF'
69 line = 'EOF'
70 line = self.precmd(line)
70 line = self.precmd(line)
71 stop = self.onecmd(line)
71 stop = self.onecmd(line)
72 stop = self.postcmd(stop, line)
72 stop = self.postcmd(stop, line)
73 self.postloop()
73 self.postloop()
74 except Exception:
74 except Exception:
75 raise
75 raise
76
76
77
77
78 def set_trace(frame=None):
78 def set_trace(frame=None):
79 """
79 """
80 Start debugging from `frame`.
80 Start debugging from `frame`.
81
81
82 If frame is not specified, debugging starts from caller's frame.
82 If frame is not specified, debugging starts from caller's frame.
83 """
83 """
84 TerminalPdb().set_trace(frame or sys._getframe().f_back)
84 TerminalPdb().set_trace(frame or sys._getframe().f_back)
85
85
86
86
87 if __name__ == '__main__':
87 if __name__ == '__main__':
88 import pdb
88 import pdb
89 # IPython.core.debugger.Pdb.trace_dispatch shall not catch
90 # bdb.BdbQuit. When started through __main__ and an exception
91 # happened after hitting "c", this is needed in order to
92 # be able to quit the debugging session (see #9950).
93 old_trace_dispatch = pdb.Pdb.trace_dispatch
89 pdb.Pdb = TerminalPdb
94 pdb.Pdb = TerminalPdb
95 pdb.Pdb.trace_dispatch = old_trace_dispatch
90 pdb.main()
96 pdb.main()
General Comments 0
You need to be logged in to leave comments. Login now