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