debugger.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import asyncio
  2. import os
  3. import sys
  4. from IPython.core.debugger import Pdb
  5. from IPython.core.completer import IPCompleter
  6. from .ptutils import IPythonPTCompleter
  7. from .shortcuts import create_ipython_shortcuts
  8. from . import embed
  9. from pathlib import Path
  10. from pygments.token import Token
  11. from prompt_toolkit.application import create_app_session
  12. from prompt_toolkit.shortcuts.prompt import PromptSession
  13. from prompt_toolkit.enums import EditingMode
  14. from prompt_toolkit.formatted_text import PygmentsTokens
  15. from prompt_toolkit.history import InMemoryHistory, FileHistory
  16. from concurrent.futures import ThreadPoolExecutor
  17. # we want to avoid ptk as much as possible when using subprocesses
  18. # as it uses cursor positioning requests, deletes color ....
  19. _use_simple_prompt = "IPY_TEST_SIMPLE_PROMPT" in os.environ
  20. class TerminalPdb(Pdb):
  21. """Standalone IPython debugger."""
  22. def __init__(self, *args, pt_session_options=None, **kwargs):
  23. Pdb.__init__(self, *args, **kwargs)
  24. self._ptcomp = None
  25. self.pt_init(pt_session_options)
  26. self.thread_executor = ThreadPoolExecutor(1)
  27. def pt_init(self, pt_session_options=None):
  28. """Initialize the prompt session and the prompt loop
  29. and store them in self.pt_app and self.pt_loop.
  30. Additional keyword arguments for the PromptSession class
  31. can be specified in pt_session_options.
  32. """
  33. if pt_session_options is None:
  34. pt_session_options = {}
  35. def get_prompt_tokens():
  36. return [(Token.Prompt, self.prompt)]
  37. if self._ptcomp is None:
  38. compl = IPCompleter(
  39. shell=self.shell, namespace={}, global_namespace={}, parent=self.shell
  40. )
  41. # add a completer for all the do_ methods
  42. methods_names = [m[3:] for m in dir(self) if m.startswith("do_")]
  43. def gen_comp(self, text):
  44. return [m for m in methods_names if m.startswith(text)]
  45. import types
  46. newcomp = types.MethodType(gen_comp, compl)
  47. compl.custom_matchers.insert(0, newcomp)
  48. # end add completer.
  49. self._ptcomp = IPythonPTCompleter(compl)
  50. # setup history only when we start pdb
  51. if self.shell.debugger_history is None:
  52. if self.shell.debugger_history_file is not None:
  53. p = Path(self.shell.debugger_history_file).expanduser()
  54. if not p.exists():
  55. p.touch()
  56. self.debugger_history = FileHistory(os.path.expanduser(str(p)))
  57. else:
  58. self.debugger_history = InMemoryHistory()
  59. else:
  60. self.debugger_history = self.shell.debugger_history
  61. options = dict(
  62. message=(lambda: PygmentsTokens(get_prompt_tokens())),
  63. editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
  64. key_bindings=create_ipython_shortcuts(self.shell),
  65. history=self.debugger_history,
  66. completer=self._ptcomp,
  67. enable_history_search=True,
  68. mouse_support=self.shell.mouse_support,
  69. complete_style=self.shell.pt_complete_style,
  70. style=getattr(self.shell, "style", None),
  71. color_depth=self.shell.color_depth,
  72. )
  73. options.update(pt_session_options)
  74. if not _use_simple_prompt:
  75. self.pt_loop = asyncio.new_event_loop()
  76. self.pt_app = PromptSession(**options)
  77. def _prompt(self):
  78. """
  79. In case other prompt_toolkit apps have to run in parallel to this one (e.g. in madbg),
  80. create_app_session must be used to prevent mixing up between them. According to the prompt_toolkit docs:
  81. > If you need multiple applications running at the same time, you have to create a separate
  82. > `AppSession` using a `with create_app_session():` block.
  83. """
  84. with create_app_session():
  85. return self.pt_app.prompt()
  86. def cmdloop(self, intro=None):
  87. """Repeatedly issue a prompt, accept input, parse an initial prefix
  88. off the received input, and dispatch to action methods, passing them
  89. the remainder of the line as argument.
  90. override the same methods from cmd.Cmd to provide prompt toolkit replacement.
  91. """
  92. if not self.use_rawinput:
  93. raise ValueError('Sorry ipdb does not support use_rawinput=False')
  94. # In order to make sure that prompt, which uses asyncio doesn't
  95. # interfere with applications in which it's used, we always run the
  96. # prompt itself in a different thread (we can't start an event loop
  97. # within an event loop). This new thread won't have any event loop
  98. # running, and here we run our prompt-loop.
  99. self.preloop()
  100. try:
  101. if intro is not None:
  102. self.intro = intro
  103. if self.intro:
  104. print(self.intro, file=self.stdout)
  105. stop = None
  106. while not stop:
  107. if self.cmdqueue:
  108. line = self.cmdqueue.pop(0)
  109. else:
  110. self._ptcomp.ipy_completer.namespace = self.curframe_locals
  111. self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals
  112. # Run the prompt in a different thread.
  113. if not _use_simple_prompt:
  114. try:
  115. line = self.thread_executor.submit(self._prompt).result()
  116. except EOFError:
  117. line = "EOF"
  118. else:
  119. line = input("ipdb> ")
  120. line = self.precmd(line)
  121. stop = self.onecmd(line)
  122. stop = self.postcmd(stop, line)
  123. self.postloop()
  124. except Exception:
  125. raise
  126. def do_interact(self, arg):
  127. ipshell = embed.InteractiveShellEmbed(
  128. config=self.shell.config,
  129. banner1="*interactive*",
  130. exit_msg="*exiting interactive console...*",
  131. )
  132. global_ns = self.curframe.f_globals
  133. ipshell(
  134. module=sys.modules.get(global_ns["__name__"], None),
  135. local_ns=self.curframe_locals,
  136. )
  137. def set_trace(frame=None):
  138. """
  139. Start debugging from `frame`.
  140. If frame is not specified, debugging starts from caller's frame.
  141. """
  142. TerminalPdb().set_trace(frame or sys._getframe().f_back)
  143. if __name__ == '__main__':
  144. import pdb
  145. # IPython.core.debugger.Pdb.trace_dispatch shall not catch
  146. # bdb.BdbQuit. When started through __main__ and an exception
  147. # happened after hitting "c", this is needed in order to
  148. # be able to quit the debugging session (see #9950).
  149. old_trace_dispatch = pdb.Pdb.trace_dispatch
  150. pdb.Pdb = TerminalPdb # type: ignore
  151. pdb.Pdb.trace_dispatch = old_trace_dispatch # type: ignore
  152. pdb.main()