debugging.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. """Interactive debugging with PDB, the Python Debugger."""
  2. import argparse
  3. import functools
  4. import sys
  5. import types
  6. from typing import Any
  7. from typing import Callable
  8. from typing import Generator
  9. from typing import List
  10. from typing import Optional
  11. from typing import Tuple
  12. from typing import Type
  13. from typing import TYPE_CHECKING
  14. from typing import Union
  15. from _pytest import outcomes
  16. from _pytest._code import ExceptionInfo
  17. from _pytest.config import Config
  18. from _pytest.config import ConftestImportFailure
  19. from _pytest.config import hookimpl
  20. from _pytest.config import PytestPluginManager
  21. from _pytest.config.argparsing import Parser
  22. from _pytest.config.exceptions import UsageError
  23. from _pytest.nodes import Node
  24. from _pytest.reports import BaseReport
  25. if TYPE_CHECKING:
  26. from _pytest.capture import CaptureManager
  27. from _pytest.runner import CallInfo
  28. def _validate_usepdb_cls(value: str) -> Tuple[str, str]:
  29. """Validate syntax of --pdbcls option."""
  30. try:
  31. modname, classname = value.split(":")
  32. except ValueError as e:
  33. raise argparse.ArgumentTypeError(
  34. f"{value!r} is not in the format 'modname:classname'"
  35. ) from e
  36. return (modname, classname)
  37. def pytest_addoption(parser: Parser) -> None:
  38. group = parser.getgroup("general")
  39. group._addoption(
  40. "--pdb",
  41. dest="usepdb",
  42. action="store_true",
  43. help="start the interactive Python debugger on errors or KeyboardInterrupt.",
  44. )
  45. group._addoption(
  46. "--pdbcls",
  47. dest="usepdb_cls",
  48. metavar="modulename:classname",
  49. type=_validate_usepdb_cls,
  50. help="specify a custom interactive Python debugger for use with --pdb."
  51. "For example: --pdbcls=IPython.terminal.debugger:TerminalPdb",
  52. )
  53. group._addoption(
  54. "--trace",
  55. dest="trace",
  56. action="store_true",
  57. help="Immediately break when running each test.",
  58. )
  59. def pytest_configure(config: Config) -> None:
  60. import pdb
  61. if config.getvalue("trace"):
  62. config.pluginmanager.register(PdbTrace(), "pdbtrace")
  63. if config.getvalue("usepdb"):
  64. config.pluginmanager.register(PdbInvoke(), "pdbinvoke")
  65. pytestPDB._saved.append(
  66. (pdb.set_trace, pytestPDB._pluginmanager, pytestPDB._config)
  67. )
  68. pdb.set_trace = pytestPDB.set_trace
  69. pytestPDB._pluginmanager = config.pluginmanager
  70. pytestPDB._config = config
  71. # NOTE: not using pytest_unconfigure, since it might get called although
  72. # pytest_configure was not (if another plugin raises UsageError).
  73. def fin() -> None:
  74. (
  75. pdb.set_trace,
  76. pytestPDB._pluginmanager,
  77. pytestPDB._config,
  78. ) = pytestPDB._saved.pop()
  79. config.add_cleanup(fin)
  80. class pytestPDB:
  81. """Pseudo PDB that defers to the real pdb."""
  82. _pluginmanager: Optional[PytestPluginManager] = None
  83. _config: Optional[Config] = None
  84. _saved: List[
  85. Tuple[Callable[..., None], Optional[PytestPluginManager], Optional[Config]]
  86. ] = []
  87. _recursive_debug = 0
  88. _wrapped_pdb_cls: Optional[Tuple[Type[Any], Type[Any]]] = None
  89. @classmethod
  90. def _is_capturing(cls, capman: Optional["CaptureManager"]) -> Union[str, bool]:
  91. if capman:
  92. return capman.is_capturing()
  93. return False
  94. @classmethod
  95. def _import_pdb_cls(cls, capman: Optional["CaptureManager"]):
  96. if not cls._config:
  97. import pdb
  98. # Happens when using pytest.set_trace outside of a test.
  99. return pdb.Pdb
  100. usepdb_cls = cls._config.getvalue("usepdb_cls")
  101. if cls._wrapped_pdb_cls and cls._wrapped_pdb_cls[0] == usepdb_cls:
  102. return cls._wrapped_pdb_cls[1]
  103. if usepdb_cls:
  104. modname, classname = usepdb_cls
  105. try:
  106. __import__(modname)
  107. mod = sys.modules[modname]
  108. # Handle --pdbcls=pdb:pdb.Pdb (useful e.g. with pdbpp).
  109. parts = classname.split(".")
  110. pdb_cls = getattr(mod, parts[0])
  111. for part in parts[1:]:
  112. pdb_cls = getattr(pdb_cls, part)
  113. except Exception as exc:
  114. value = ":".join((modname, classname))
  115. raise UsageError(
  116. f"--pdbcls: could not import {value!r}: {exc}"
  117. ) from exc
  118. else:
  119. import pdb
  120. pdb_cls = pdb.Pdb
  121. wrapped_cls = cls._get_pdb_wrapper_class(pdb_cls, capman)
  122. cls._wrapped_pdb_cls = (usepdb_cls, wrapped_cls)
  123. return wrapped_cls
  124. @classmethod
  125. def _get_pdb_wrapper_class(cls, pdb_cls, capman: Optional["CaptureManager"]):
  126. import _pytest.config
  127. # Type ignored because mypy doesn't support "dynamic"
  128. # inheritance like this.
  129. class PytestPdbWrapper(pdb_cls): # type: ignore[valid-type,misc]
  130. _pytest_capman = capman
  131. _continued = False
  132. def do_debug(self, arg):
  133. cls._recursive_debug += 1
  134. ret = super().do_debug(arg)
  135. cls._recursive_debug -= 1
  136. return ret
  137. def do_continue(self, arg):
  138. ret = super().do_continue(arg)
  139. if cls._recursive_debug == 0:
  140. assert cls._config is not None
  141. tw = _pytest.config.create_terminal_writer(cls._config)
  142. tw.line()
  143. capman = self._pytest_capman
  144. capturing = pytestPDB._is_capturing(capman)
  145. if capturing:
  146. if capturing == "global":
  147. tw.sep(">", "PDB continue (IO-capturing resumed)")
  148. else:
  149. tw.sep(
  150. ">",
  151. "PDB continue (IO-capturing resumed for %s)"
  152. % capturing,
  153. )
  154. assert capman is not None
  155. capman.resume()
  156. else:
  157. tw.sep(">", "PDB continue")
  158. assert cls._pluginmanager is not None
  159. cls._pluginmanager.hook.pytest_leave_pdb(config=cls._config, pdb=self)
  160. self._continued = True
  161. return ret
  162. do_c = do_cont = do_continue
  163. def do_quit(self, arg):
  164. """Raise Exit outcome when quit command is used in pdb.
  165. This is a bit of a hack - it would be better if BdbQuit
  166. could be handled, but this would require to wrap the
  167. whole pytest run, and adjust the report etc.
  168. """
  169. ret = super().do_quit(arg)
  170. if cls._recursive_debug == 0:
  171. outcomes.exit("Quitting debugger")
  172. return ret
  173. do_q = do_quit
  174. do_exit = do_quit
  175. def setup(self, f, tb):
  176. """Suspend on setup().
  177. Needed after do_continue resumed, and entering another
  178. breakpoint again.
  179. """
  180. ret = super().setup(f, tb)
  181. if not ret and self._continued:
  182. # pdb.setup() returns True if the command wants to exit
  183. # from the interaction: do not suspend capturing then.
  184. if self._pytest_capman:
  185. self._pytest_capman.suspend_global_capture(in_=True)
  186. return ret
  187. def get_stack(self, f, t):
  188. stack, i = super().get_stack(f, t)
  189. if f is None:
  190. # Find last non-hidden frame.
  191. i = max(0, len(stack) - 1)
  192. while i and stack[i][0].f_locals.get("__tracebackhide__", False):
  193. i -= 1
  194. return stack, i
  195. return PytestPdbWrapper
  196. @classmethod
  197. def _init_pdb(cls, method, *args, **kwargs):
  198. """Initialize PDB debugging, dropping any IO capturing."""
  199. import _pytest.config
  200. if cls._pluginmanager is None:
  201. capman: Optional[CaptureManager] = None
  202. else:
  203. capman = cls._pluginmanager.getplugin("capturemanager")
  204. if capman:
  205. capman.suspend(in_=True)
  206. if cls._config:
  207. tw = _pytest.config.create_terminal_writer(cls._config)
  208. tw.line()
  209. if cls._recursive_debug == 0:
  210. # Handle header similar to pdb.set_trace in py37+.
  211. header = kwargs.pop("header", None)
  212. if header is not None:
  213. tw.sep(">", header)
  214. else:
  215. capturing = cls._is_capturing(capman)
  216. if capturing == "global":
  217. tw.sep(">", f"PDB {method} (IO-capturing turned off)")
  218. elif capturing:
  219. tw.sep(
  220. ">",
  221. "PDB %s (IO-capturing turned off for %s)"
  222. % (method, capturing),
  223. )
  224. else:
  225. tw.sep(">", f"PDB {method}")
  226. _pdb = cls._import_pdb_cls(capman)(**kwargs)
  227. if cls._pluginmanager:
  228. cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config, pdb=_pdb)
  229. return _pdb
  230. @classmethod
  231. def set_trace(cls, *args, **kwargs) -> None:
  232. """Invoke debugging via ``Pdb.set_trace``, dropping any IO capturing."""
  233. frame = sys._getframe().f_back
  234. _pdb = cls._init_pdb("set_trace", *args, **kwargs)
  235. _pdb.set_trace(frame)
  236. class PdbInvoke:
  237. def pytest_exception_interact(
  238. self, node: Node, call: "CallInfo[Any]", report: BaseReport
  239. ) -> None:
  240. capman = node.config.pluginmanager.getplugin("capturemanager")
  241. if capman:
  242. capman.suspend_global_capture(in_=True)
  243. out, err = capman.read_global_capture()
  244. sys.stdout.write(out)
  245. sys.stdout.write(err)
  246. assert call.excinfo is not None
  247. _enter_pdb(node, call.excinfo, report)
  248. def pytest_internalerror(self, excinfo: ExceptionInfo[BaseException]) -> None:
  249. tb = _postmortem_traceback(excinfo)
  250. post_mortem(tb)
  251. class PdbTrace:
  252. @hookimpl(hookwrapper=True)
  253. def pytest_pyfunc_call(self, pyfuncitem) -> Generator[None, None, None]:
  254. wrap_pytest_function_for_tracing(pyfuncitem)
  255. yield
  256. def wrap_pytest_function_for_tracing(pyfuncitem):
  257. """Change the Python function object of the given Function item by a
  258. wrapper which actually enters pdb before calling the python function
  259. itself, effectively leaving the user in the pdb prompt in the first
  260. statement of the function."""
  261. _pdb = pytestPDB._init_pdb("runcall")
  262. testfunction = pyfuncitem.obj
  263. # we can't just return `partial(pdb.runcall, testfunction)` because (on
  264. # python < 3.7.4) runcall's first param is `func`, which means we'd get
  265. # an exception if one of the kwargs to testfunction was called `func`.
  266. @functools.wraps(testfunction)
  267. def wrapper(*args, **kwargs):
  268. func = functools.partial(testfunction, *args, **kwargs)
  269. _pdb.runcall(func)
  270. pyfuncitem.obj = wrapper
  271. def maybe_wrap_pytest_function_for_tracing(pyfuncitem):
  272. """Wrap the given pytestfunct item for tracing support if --trace was given in
  273. the command line."""
  274. if pyfuncitem.config.getvalue("trace"):
  275. wrap_pytest_function_for_tracing(pyfuncitem)
  276. def _enter_pdb(
  277. node: Node, excinfo: ExceptionInfo[BaseException], rep: BaseReport
  278. ) -> BaseReport:
  279. # XXX we re-use the TerminalReporter's terminalwriter
  280. # because this seems to avoid some encoding related troubles
  281. # for not completely clear reasons.
  282. tw = node.config.pluginmanager.getplugin("terminalreporter")._tw
  283. tw.line()
  284. showcapture = node.config.option.showcapture
  285. for sectionname, content in (
  286. ("stdout", rep.capstdout),
  287. ("stderr", rep.capstderr),
  288. ("log", rep.caplog),
  289. ):
  290. if showcapture in (sectionname, "all") and content:
  291. tw.sep(">", "captured " + sectionname)
  292. if content[-1:] == "\n":
  293. content = content[:-1]
  294. tw.line(content)
  295. tw.sep(">", "traceback")
  296. rep.toterminal(tw)
  297. tw.sep(">", "entering PDB")
  298. tb = _postmortem_traceback(excinfo)
  299. rep._pdbshown = True # type: ignore[attr-defined]
  300. post_mortem(tb)
  301. return rep
  302. def _postmortem_traceback(excinfo: ExceptionInfo[BaseException]) -> types.TracebackType:
  303. from doctest import UnexpectedException
  304. if isinstance(excinfo.value, UnexpectedException):
  305. # A doctest.UnexpectedException is not useful for post_mortem.
  306. # Use the underlying exception instead:
  307. return excinfo.value.exc_info[2]
  308. elif isinstance(excinfo.value, ConftestImportFailure):
  309. # A config.ConftestImportFailure is not useful for post_mortem.
  310. # Use the underlying exception instead:
  311. return excinfo.value.excinfo[2]
  312. else:
  313. assert excinfo._excinfo is not None
  314. return excinfo._excinfo[2]
  315. def post_mortem(t: types.TracebackType) -> None:
  316. p = pytestPDB._init_pdb("post_mortem")
  317. p.reset()
  318. p.interaction(None, t)
  319. if p.quitting:
  320. outcomes.exit("Quitting debugger")