Debian Codegraph @

_pytest/pdb.py

raw symbols SCIP
1 """ interactive debugging with PDB, the Python Debugger. """
2 from __future__ import absolute_import
3 import pdb
4 import sys
5
6 import pytest
7
8
9 def pytest_addoption(parser):
10 group = parser.getgroup("general")
11 group._addoption('--pdb',
12 action="store_true", dest="usepdb", default=False,
13 help="start the interactive Python debugger on errors.")
14
15 def pytest_namespace():
16 return {'set_trace': pytestPDB().set_trace}
17
18 def pytest_configure(config):
19 if config.getvalue("usepdb"):
20 config.pluginmanager.register(PdbInvoke(), 'pdbinvoke')
21
22 old = (pdb.set_trace, pytestPDB._pluginmanager)
23 def fin():
24 pdb.set_trace, pytestPDB._pluginmanager = old
25 pytestPDB._config = None
26 pdb.set_trace = pytest.set_trace
27 pytestPDB._pluginmanager = config.pluginmanager
28 pytestPDB._config = config
29 config._cleanup.append(fin)
30
31 class pytestPDB:
32 """ Pseudo PDB that defers to the real pdb. """
33 _pluginmanager = None
34 _config = None
35
36 def set_trace(self):
37 """ invoke PDB set_trace debugging, dropping any IO capturing. """
38 import _pytest.config
39 frame = sys._getframe().f_back
40 if self._pluginmanager is not None:
41 capman = self._pluginmanager.getplugin("capturemanager")
42 if capman:
43 capman.suspendcapture(in_=True)
44 tw = _pytest.config.create_terminal_writer(self._config)
45 tw.line()
46 tw.sep(">", "PDB set_trace (IO-capturing turned off)")
47 self._pluginmanager.hook.pytest_enter_pdb(config=self._config)
48 pdb.Pdb().set_trace(frame)
49
50
51 class PdbInvoke:
52 def pytest_exception_interact(self, node, call, report):
53 capman = node.config.pluginmanager.getplugin("capturemanager")
54 if capman:
55 out, err = capman.suspendcapture(in_=True)
56 sys.stdout.write(out)
57 sys.stdout.write(err)
58 _enter_pdb(node, call.excinfo, report)
59
60 def pytest_internalerror(self, excrepr, excinfo):
61 for line in str(excrepr).split("\n"):
62 sys.stderr.write("INTERNALERROR> %s\n" %line)
63 sys.stderr.flush()
64 tb = _postmortem_traceback(excinfo)
65 post_mortem(tb)
66
67
68 def _enter_pdb(node, excinfo, rep):
69 # XXX we re-use the TerminalReporter's terminalwriter
70 # because this seems to avoid some encoding related troubles
71 # for not completely clear reasons.
72 tw = node.config.pluginmanager.getplugin("terminalreporter")._tw
73 tw.line()
74 tw.sep(">", "traceback")
75 rep.toterminal(tw)
76 tw.sep(">", "entering PDB")
77 tb = _postmortem_traceback(excinfo)
78 post_mortem(tb)
79 rep._pdbshown = True
80 return rep
81
82
83 def _postmortem_traceback(excinfo):
84 # A doctest.UnexpectedException is not useful for post_mortem.
85 # Use the underlying exception instead:
86 from doctest import UnexpectedException
87 if isinstance(excinfo.value, UnexpectedException):
88 return excinfo.value.exc_info[2]
89 else:
90 return excinfo._excinfo[2]
91
92
93 def _find_last_non_hidden_frame(stack):
94 i = max(0, len(stack) - 1)
95 while i and stack[i][0].f_locals.get("__tracebackhide__", False):
96 i -= 1
97 return i
98
99
100 def post_mortem(t):
101 class Pdb(pdb.Pdb):
102 def get_stack(self, f, t):
103 stack, i = pdb.Pdb.get_stack(self, f, t)
104 if f is None:
105 i = _find_last_non_hidden_frame(stack)
106 return stack, i
107 p = Pdb()
108 p.reset()
109 p.interaction(None, t)