| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  | import signal | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-27 04:23:21 +00:00
										 |  |  | from IPython.core.debugger import Pdb | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-27 04:23:21 +00:00
										 |  |  | from IPython.core.completer import IPCompleter | 
					
						
							| 
									
										
										
										
											2018-10-15 13:48:49 +01:00
										 |  |  | from .ptutils import IPythonPTCompleter | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  | from .shortcuts import suspend_to_bg, cursor_in_leading_ws | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from prompt_toolkit.enums import DEFAULT_BUFFER | 
					
						
							| 
									
										
										
										
											2019-02-27 04:23:21 +00:00
										 |  |  | from prompt_toolkit.filters import (Condition, has_focus, has_selection, | 
					
						
							|  |  |  |     vi_insert_mode, emacs_insert_mode) | 
					
						
							|  |  |  | from prompt_toolkit.key_binding import KeyBindings | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  | from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline | 
					
						
							| 
									
										
										
										
											2019-02-27 04:23:21 +00:00
										 |  |  | from pygments.token import Token | 
					
						
							| 
									
										
										
										
											2018-10-15 13:48:49 +01:00
										 |  |  | from prompt_toolkit.shortcuts.prompt import PromptSession | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  | from prompt_toolkit.enums import EditingMode | 
					
						
							| 
									
										
										
										
											2018-10-15 13:48:49 +01:00
										 |  |  | from prompt_toolkit.formatted_text import PygmentsTokens | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TerminalPdb(Pdb): | 
					
						
							|  |  |  |     def __init__(self, *args, **kwargs): | 
					
						
							|  |  |  |         Pdb.__init__(self, *args, **kwargs) | 
					
						
							|  |  |  |         self._ptcomp = None | 
					
						
							|  |  |  |         self.pt_init() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def pt_init(self): | 
					
						
							| 
									
										
										
										
											2018-10-15 13:48:49 +01:00
										 |  |  |         def get_prompt_tokens(): | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  |             return [(Token.Prompt, self.prompt)] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if self._ptcomp is None: | 
					
						
							|  |  |  |             compl = IPCompleter(shell=self.shell, | 
					
						
							|  |  |  |                                         namespace={}, | 
					
						
							|  |  |  |                                         global_namespace={}, | 
					
						
							|  |  |  |                                         parent=self.shell, | 
					
						
							|  |  |  |                                        ) | 
					
						
							| 
									
										
										
										
											2018-10-15 13:48:49 +01:00
										 |  |  |             self._ptcomp = IPythonPTCompleter(compl) | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 13:48:49 +01:00
										 |  |  |         kb = KeyBindings() | 
					
						
							|  |  |  |         supports_suspend = Condition(lambda: hasattr(signal, 'SIGTSTP')) | 
					
						
							|  |  |  |         kb.add('c-z', filter=supports_suspend)(suspend_to_bg) | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if self.shell.display_completions == 'readlinelike': | 
					
						
							| 
									
										
										
										
											2018-10-15 13:48:49 +01:00
										 |  |  |             kb.add('tab', filter=(has_focus(DEFAULT_BUFFER) | 
					
						
							|  |  |  |                                   & ~has_selection | 
					
						
							|  |  |  |                                   & vi_insert_mode | emacs_insert_mode | 
					
						
							|  |  |  |                                   & ~cursor_in_leading_ws | 
					
						
							|  |  |  |                               ))(display_completions_like_readline) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.pt_app = PromptSession( | 
					
						
							|  |  |  |                             message=(lambda: PygmentsTokens(get_prompt_tokens())), | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  |                             editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()), | 
					
						
							| 
									
										
										
										
											2018-10-15 13:48:49 +01:00
										 |  |  |                             key_bindings=kb, | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  |                             history=self.shell.debugger_history, | 
					
						
							| 
									
										
										
										
											2018-10-15 13:48:49 +01:00
										 |  |  |                             completer=self._ptcomp, | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  |                             enable_history_search=True, | 
					
						
							|  |  |  |                             mouse_support=self.shell.mouse_support, | 
					
						
							| 
									
										
										
										
											2018-10-15 13:48:49 +01:00
										 |  |  |                             complete_style=self.shell.pt_complete_style, | 
					
						
							|  |  |  |                             style=self.shell.style, | 
					
						
							|  |  |  |                             inputhook=self.shell.inputhook, | 
					
						
							| 
									
										
										
										
											2019-02-27 04:23:21 +00:00
										 |  |  |                             color_depth=self.shell.color_depth, | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def cmdloop(self, intro=None): | 
					
						
							|  |  |  |         """Repeatedly issue a prompt, accept input, parse an initial prefix
 | 
					
						
							|  |  |  |         off the received input, and dispatch to action methods, passing them | 
					
						
							|  |  |  |         the remainder of the line as argument. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         override the same methods from cmd.Cmd to provide prompt toolkit replacement. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if not self.use_rawinput: | 
					
						
							|  |  |  |             raise ValueError('Sorry ipdb does not support use_rawinput=False') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.preloop() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             if intro is not None: | 
					
						
							|  |  |  |                 self.intro = intro | 
					
						
							|  |  |  |             if self.intro: | 
					
						
							|  |  |  |                 self.stdout.write(str(self.intro)+"\n") | 
					
						
							|  |  |  |             stop = None | 
					
						
							|  |  |  |             while not stop: | 
					
						
							|  |  |  |                 if self.cmdqueue: | 
					
						
							|  |  |  |                     line = self.cmdqueue.pop(0) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     self._ptcomp.ipy_completer.namespace = self.curframe_locals | 
					
						
							|  |  |  |                     self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals | 
					
						
							|  |  |  |                     try: | 
					
						
							| 
									
										
										
										
											2018-10-15 13:48:49 +01:00
										 |  |  |                         line = self.pt_app.prompt() # reset_current_buffer=True) | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  |                     except EOFError: | 
					
						
							|  |  |  |                         line = 'EOF' | 
					
						
							|  |  |  |                 line = self.precmd(line) | 
					
						
							|  |  |  |                 stop = self.onecmd(line) | 
					
						
							|  |  |  |                 stop = self.postcmd(stop, line) | 
					
						
							|  |  |  |             self.postloop() | 
					
						
							|  |  |  |         except Exception: | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def set_trace(frame=None): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Start debugging from `frame`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If frame is not specified, debugging starts from caller's frame. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     TerminalPdb().set_trace(frame or sys._getframe().f_back) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     import pdb | 
					
						
							| 
									
										
										
										
											2019-02-27 04:23:21 +00:00
										 |  |  |     # IPython.core.debugger.Pdb.trace_dispatch shall not catch | 
					
						
							| 
									
										
										
										
											2018-01-05 16:57:38 +00:00
										 |  |  |     # bdb.BdbQuit. When started through __main__ and an exception | 
					
						
							|  |  |  |     # happened after hitting "c", this is needed in order to | 
					
						
							|  |  |  |     # be able to quit the debugging session (see #9950). | 
					
						
							|  |  |  |     old_trace_dispatch = pdb.Pdb.trace_dispatch | 
					
						
							|  |  |  |     pdb.Pdb = TerminalPdb | 
					
						
							|  |  |  |     pdb.Pdb.trace_dispatch = old_trace_dispatch | 
					
						
							|  |  |  |     pdb.main() |