2010-11-06 18:38:53 +08:00
|
|
|
""" interactive debugging with PDB, the Python Debugger. """
|
|
|
|
|
2010-11-22 06:17:59 +08:00
|
|
|
import pytest, py
|
2010-10-06 17:55:12 +08:00
|
|
|
import sys
|
2009-05-23 01:57:21 +08:00
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
2010-07-27 03:15:15 +08:00
|
|
|
group = parser.getgroup("general")
|
2009-05-23 01:57:21 +08:00
|
|
|
group._addoption('--pdb',
|
|
|
|
action="store_true", dest="usepdb", default=False,
|
2010-01-03 19:41:29 +08:00
|
|
|
help="start the interactive Python debugger on errors.")
|
2009-05-23 01:57:21 +08:00
|
|
|
|
2010-10-06 20:48:24 +08:00
|
|
|
def pytest_namespace():
|
|
|
|
return {'set_trace': pytestPDB().set_trace}
|
|
|
|
|
2010-01-13 23:00:33 +08:00
|
|
|
def pytest_configure(config):
|
|
|
|
if config.getvalue("usepdb"):
|
2010-10-06 20:48:24 +08:00
|
|
|
config.pluginmanager.register(PdbInvoke(), 'pdbinvoke')
|
|
|
|
|
2013-09-06 21:29:00 +08:00
|
|
|
old_trace = py.std.pdb.set_trace
|
|
|
|
def fin():
|
|
|
|
py.std.pdb.set_trace = old_trace
|
|
|
|
py.std.pdb.set_trace = pytest.set_trace
|
|
|
|
config._cleanup.append(fin)
|
|
|
|
|
2010-10-06 20:48:24 +08:00
|
|
|
class pytestPDB:
|
|
|
|
""" Pseudo PDB that defers to the real pdb. """
|
|
|
|
item = None
|
2011-08-01 16:53:37 +08:00
|
|
|
collector = None
|
2010-10-06 20:48:24 +08:00
|
|
|
|
|
|
|
def set_trace(self):
|
|
|
|
""" invoke PDB set_trace debugging, dropping any IO capturing. """
|
|
|
|
frame = sys._getframe().f_back
|
2011-08-01 16:53:37 +08:00
|
|
|
item = self.item or self.collector
|
|
|
|
|
2010-10-06 20:48:24 +08:00
|
|
|
if item is not None:
|
|
|
|
capman = item.config.pluginmanager.getplugin("capturemanager")
|
|
|
|
out, err = capman.suspendcapture()
|
|
|
|
if hasattr(item, 'outerr'):
|
|
|
|
item.outerr = (item.outerr[0] + out, item.outerr[1] + err)
|
|
|
|
tw = py.io.TerminalWriter()
|
|
|
|
tw.line()
|
|
|
|
tw.sep(">", "PDB set_trace (IO-capturing turned off)")
|
|
|
|
py.std.pdb.Pdb().set_trace(frame)
|
|
|
|
|
|
|
|
def pdbitem(item):
|
|
|
|
pytestPDB.item = item
|
|
|
|
pytest_runtest_setup = pytest_runtest_call = pytest_runtest_teardown = pdbitem
|
2009-05-23 01:57:21 +08:00
|
|
|
|
2011-08-01 16:53:37 +08:00
|
|
|
@pytest.mark.tryfirst
|
|
|
|
def pytest_make_collect_report(__multicall__, collector):
|
|
|
|
try:
|
|
|
|
pytestPDB.collector = collector
|
|
|
|
return __multicall__.execute()
|
|
|
|
finally:
|
|
|
|
pytestPDB.collector = None
|
|
|
|
|
2010-10-06 20:48:24 +08:00
|
|
|
def pytest_runtest_makereport():
|
|
|
|
pytestPDB.item = None
|
2012-06-23 17:32:32 +08:00
|
|
|
|
2009-05-23 01:57:21 +08:00
|
|
|
class PdbInvoke:
|
2013-09-06 17:56:04 +08:00
|
|
|
def pytest_exception_interact(self, node, call, report):
|
|
|
|
return _enter_pdb(node, call.excinfo, report)
|
2013-04-18 17:18:24 +08:00
|
|
|
|
2013-09-06 17:56:04 +08:00
|
|
|
def pytest_internalerror(self, excrepr, excinfo):
|
|
|
|
for line in str(excrepr).split("\n"):
|
|
|
|
sys.stderr.write("INTERNALERROR> %s\n" %line)
|
|
|
|
sys.stderr.flush()
|
|
|
|
tb = _postmortem_traceback(excinfo)
|
|
|
|
post_mortem(tb)
|
2013-04-18 17:18:24 +08:00
|
|
|
|
|
|
|
|
2013-09-06 17:56:04 +08:00
|
|
|
def _enter_pdb(node, excinfo, rep):
|
2013-04-18 17:18:24 +08:00
|
|
|
# XXX we re-use the TerminalReporter's terminalwriter
|
|
|
|
# because this seems to avoid some encoding related troubles
|
|
|
|
# for not completely clear reasons.
|
2013-09-06 17:56:04 +08:00
|
|
|
tw = node.config.pluginmanager.getplugin("terminalreporter")._tw
|
2013-04-18 17:18:24 +08:00
|
|
|
tw.line()
|
|
|
|
tw.sep(">", "traceback")
|
|
|
|
rep.toterminal(tw)
|
|
|
|
tw.sep(">", "entering PDB")
|
|
|
|
tb = _postmortem_traceback(excinfo)
|
|
|
|
post_mortem(tb)
|
|
|
|
rep._pdbshown = True
|
|
|
|
return rep
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2013-04-16 16:18:08 +08:00
|
|
|
|
|
|
|
def _postmortem_traceback(excinfo):
|
|
|
|
# A doctest.UnexpectedException is not useful for post_mortem.
|
|
|
|
# Use the underlying exception instead:
|
|
|
|
if isinstance(excinfo.value, py.std.doctest.UnexpectedException):
|
|
|
|
return excinfo.value.exc_info[2]
|
|
|
|
else:
|
|
|
|
return excinfo._excinfo[2]
|
|
|
|
|
2013-04-16 14:46:55 +08:00
|
|
|
|
2013-04-16 16:19:20 +08:00
|
|
|
def _find_last_non_hidden_frame(stack):
|
|
|
|
i = max(0, len(stack) - 1)
|
|
|
|
while i and stack[i][0].f_locals.get("__tracebackhide__", False):
|
|
|
|
i -= 1
|
|
|
|
return i
|
|
|
|
|
|
|
|
|
2009-05-23 01:57:21 +08:00
|
|
|
def post_mortem(t):
|
2010-10-06 17:55:12 +08:00
|
|
|
pdb = py.std.pdb
|
|
|
|
class Pdb(pdb.Pdb):
|
|
|
|
def get_stack(self, f, t):
|
|
|
|
stack, i = pdb.Pdb.get_stack(self, f, t)
|
|
|
|
if f is None:
|
2013-04-16 16:19:20 +08:00
|
|
|
i = _find_last_non_hidden_frame(stack)
|
2010-10-06 17:55:12 +08:00
|
|
|
return stack, i
|
2009-05-23 01:57:21 +08:00
|
|
|
p = Pdb()
|
|
|
|
p.reset()
|
|
|
|
p.interaction(None, t)
|