Show More
@@ -1,100 +1,113 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 |
|
8 | from .shortcuts import suspend_to_bg, cursor_in_leading_ws | |
9 |
|
9 | |||
10 |
from prompt_toolkit. |
|
10 | from prompt_toolkit.enums import DEFAULT_BUFFER | |
|
11 | from prompt_toolkit.filters import (Condition, HasFocus, HasSelection, | |||
|
12 | ViInsertMode, EmacsInsertMode) | |||
11 | from prompt_toolkit.keys import Keys |
|
13 | from prompt_toolkit.keys import Keys | |
12 | from prompt_toolkit.key_binding.manager import KeyBindingManager |
|
14 | from prompt_toolkit.key_binding.manager import KeyBindingManager | |
|
15 | from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline | |||
13 | from prompt_toolkit.token import Token |
|
16 | from prompt_toolkit.token import Token | |
14 | from prompt_toolkit.shortcuts import create_prompt_application |
|
17 | from prompt_toolkit.shortcuts import create_prompt_application | |
15 | from prompt_toolkit.interface import CommandLineInterface |
|
18 | from prompt_toolkit.interface import CommandLineInterface | |
16 | from prompt_toolkit.enums import EditingMode |
|
19 | from prompt_toolkit.enums import EditingMode | |
17 |
|
20 | |||
18 |
|
21 | |||
19 | class TerminalPdb(Pdb): |
|
22 | class TerminalPdb(Pdb): | |
20 | def __init__(self, *args, **kwargs): |
|
23 | def __init__(self, *args, **kwargs): | |
21 | Pdb.__init__(self, *args, **kwargs) |
|
24 | Pdb.__init__(self, *args, **kwargs) | |
22 | self._ptcomp = None |
|
25 | self._ptcomp = None | |
23 | self.pt_init() |
|
26 | self.pt_init() | |
24 |
|
27 | |||
25 | def pt_init(self): |
|
28 | def pt_init(self): | |
26 | def get_prompt_tokens(cli): |
|
29 | def get_prompt_tokens(cli): | |
27 | return [(Token.Prompt, self.prompt)] |
|
30 | return [(Token.Prompt, self.prompt)] | |
28 |
|
31 | |||
29 | def patch_stdout(**kwargs): |
|
32 | def patch_stdout(**kwargs): | |
30 | return self.pt_cli.patch_stdout_context(**kwargs) |
|
33 | return self.pt_cli.patch_stdout_context(**kwargs) | |
31 |
|
34 | |||
32 | if self._ptcomp is None: |
|
35 | if self._ptcomp is None: | |
33 | compl = IPCompleter(shell=self.shell, |
|
36 | compl = IPCompleter(shell=self.shell, | |
34 | namespace={}, |
|
37 | namespace={}, | |
35 | global_namespace={}, |
|
38 | global_namespace={}, | |
36 | use_readline=False, |
|
39 | use_readline=False, | |
37 | parent=self.shell, |
|
40 | parent=self.shell, | |
38 | ) |
|
41 | ) | |
39 | self._ptcomp = IPythonPTCompleter(compl, patch_stdout=patch_stdout) |
|
42 | self._ptcomp = IPythonPTCompleter(compl, patch_stdout=patch_stdout) | |
40 |
|
43 | |||
41 | kbmanager = KeyBindingManager.for_prompt() |
|
44 | kbmanager = KeyBindingManager.for_prompt() | |
42 | supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP')) |
|
45 | supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP')) | |
43 | kbmanager.registry.add_binding(Keys.ControlZ, filter=supports_suspend |
|
46 | kbmanager.registry.add_binding(Keys.ControlZ, filter=supports_suspend | |
44 | )(suspend_to_bg) |
|
47 | )(suspend_to_bg) | |
45 |
|
48 | |||
|
49 | if self.shell.display_completions == 'readlinelike': | |||
|
50 | kbmanager.registry.add_binding(Keys.ControlI, | |||
|
51 | filter=(HasFocus(DEFAULT_BUFFER) | |||
|
52 | & ~HasSelection() | |||
|
53 | & ViInsertMode() | EmacsInsertMode() | |||
|
54 | & ~cursor_in_leading_ws | |||
|
55 | ))(display_completions_like_readline) | |||
|
56 | multicolumn = (self.shell.display_completions == 'multicolumn') | |||
|
57 | ||||
46 | self._pt_app = create_prompt_application( |
|
58 | self._pt_app = create_prompt_application( | |
47 | editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()), |
|
59 | editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()), | |
48 | key_bindings_registry=kbmanager.registry, |
|
60 | key_bindings_registry=kbmanager.registry, | |
49 | history=self.shell.debugger_history, |
|
61 | history=self.shell.debugger_history, | |
50 | completer= self._ptcomp, |
|
62 | completer= self._ptcomp, | |
51 | enable_history_search=True, |
|
63 | enable_history_search=True, | |
52 | mouse_support=self.shell.mouse_support, |
|
64 | mouse_support=self.shell.mouse_support, | |
53 | get_prompt_tokens=get_prompt_tokens |
|
65 | get_prompt_tokens=get_prompt_tokens, | |
|
66 | display_completions_in_columns=multicolumn, | |||
54 | ) |
|
67 | ) | |
55 | self.pt_cli = CommandLineInterface(self._pt_app, eventloop=self.shell._eventloop) |
|
68 | self.pt_cli = CommandLineInterface(self._pt_app, eventloop=self.shell._eventloop) | |
56 |
|
69 | |||
57 | def cmdloop(self, intro=None): |
|
70 | def cmdloop(self, intro=None): | |
58 | """Repeatedly issue a prompt, accept input, parse an initial prefix |
|
71 | """Repeatedly issue a prompt, accept input, parse an initial prefix | |
59 | off the received input, and dispatch to action methods, passing them |
|
72 | off the received input, and dispatch to action methods, passing them | |
60 | the remainder of the line as argument. |
|
73 | the remainder of the line as argument. | |
61 |
|
74 | |||
62 | override the same methods from cmd.Cmd to provide prompt toolkit replacement. |
|
75 | override the same methods from cmd.Cmd to provide prompt toolkit replacement. | |
63 | """ |
|
76 | """ | |
64 | if not self.use_rawinput: |
|
77 | if not self.use_rawinput: | |
65 | raise ValueError('Sorry ipdb does not support use_rawinput=False') |
|
78 | raise ValueError('Sorry ipdb does not support use_rawinput=False') | |
66 |
|
79 | |||
67 | self.preloop() |
|
80 | self.preloop() | |
68 |
|
81 | |||
69 | try: |
|
82 | try: | |
70 | if intro is not None: |
|
83 | if intro is not None: | |
71 | self.intro = intro |
|
84 | self.intro = intro | |
72 | if self.intro: |
|
85 | if self.intro: | |
73 | self.stdout.write(str(self.intro)+"\n") |
|
86 | self.stdout.write(str(self.intro)+"\n") | |
74 | stop = None |
|
87 | stop = None | |
75 | while not stop: |
|
88 | while not stop: | |
76 | if self.cmdqueue: |
|
89 | if self.cmdqueue: | |
77 | line = self.cmdqueue.pop(0) |
|
90 | line = self.cmdqueue.pop(0) | |
78 | else: |
|
91 | else: | |
79 | self._ptcomp.ipy_completer.namespace = self.curframe_locals |
|
92 | self._ptcomp.ipy_completer.namespace = self.curframe_locals | |
80 | self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals |
|
93 | self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals | |
81 | try: |
|
94 | try: | |
82 | line = self.pt_cli.run(reset_current_buffer=True).text |
|
95 | line = self.pt_cli.run(reset_current_buffer=True).text | |
83 | except EOFError: |
|
96 | except EOFError: | |
84 | line = 'EOF' |
|
97 | line = 'EOF' | |
85 | line = self.precmd(line) |
|
98 | line = self.precmd(line) | |
86 | stop = self.onecmd(line) |
|
99 | stop = self.onecmd(line) | |
87 | stop = self.postcmd(stop, line) |
|
100 | stop = self.postcmd(stop, line) | |
88 | self.postloop() |
|
101 | self.postloop() | |
89 | except Exception: |
|
102 | except Exception: | |
90 | raise |
|
103 | raise | |
91 |
|
104 | |||
92 |
|
105 | |||
93 | def set_trace(frame=None): |
|
106 | def set_trace(frame=None): | |
94 | """ |
|
107 | """ | |
95 | Start debugging from `frame`. |
|
108 | Start debugging from `frame`. | |
96 |
|
109 | |||
97 | If frame is not specified, debugging starts from caller's frame. |
|
110 | If frame is not specified, debugging starts from caller's frame. | |
98 | """ |
|
111 | """ | |
99 | TerminalPdb().set_trace(frame or sys._getframe().f_back) |
|
112 | TerminalPdb().set_trace(frame or sys._getframe().f_back) | |
100 |
|
113 |
General Comments 0
You need to be logged in to leave comments.
Login now