debugging.py 14 KB

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