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')
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2009-05-23 01:57:21 +08:00
|
|
|
class PdbInvoke:
|
2010-11-22 06:17:59 +08:00
|
|
|
@pytest.mark.tryfirst
|
2010-06-16 18:35:08 +08:00
|
|
|
def pytest_runtest_makereport(self, item, call, __multicall__):
|
2010-11-24 18:48:55 +08:00
|
|
|
rep = __multicall__.execute()
|
2010-06-16 18:35:08 +08:00
|
|
|
if not call.excinfo or \
|
2010-11-22 06:17:59 +08:00
|
|
|
call.excinfo.errisinstance(pytest.skip.Exception) or \
|
2010-10-06 17:55:12 +08:00
|
|
|
call.excinfo.errisinstance(py.std.bdb.BdbQuit):
|
2010-11-24 18:48:55 +08:00
|
|
|
return rep
|
2010-06-16 18:35:08 +08:00
|
|
|
if "xfail" in rep.keywords:
|
|
|
|
return rep
|
|
|
|
# we assume that the above execute() suspended capturing
|
2011-03-07 20:17:07 +08:00
|
|
|
# XXX we re-use the TerminalReporter's terminalwriter
|
|
|
|
# because this seems to avoid some encoding related troubles
|
|
|
|
# for not completely clear reasons.
|
|
|
|
tw = item.config.pluginmanager.getplugin("terminalreporter")._tw
|
2010-06-16 18:35:08 +08:00
|
|
|
tw.line()
|
|
|
|
tw.sep(">", "traceback")
|
|
|
|
rep.toterminal(tw)
|
|
|
|
tw.sep(">", "entering PDB")
|
2011-11-15 21:28:22 +08:00
|
|
|
# A doctest.UnexpectedException is not useful for post_mortem.
|
|
|
|
# Use the underlying exception instead:
|
|
|
|
if isinstance(call.excinfo.value, py.std.doctest.UnexpectedException):
|
|
|
|
tb = call.excinfo.value.exc_info[2]
|
|
|
|
else:
|
|
|
|
tb = call.excinfo._excinfo[2]
|
|
|
|
post_mortem(tb)
|
2010-11-23 23:10:47 +08:00
|
|
|
rep._pdbshown = True
|
2010-06-16 18:35:08 +08:00
|
|
|
return rep
|
2009-07-31 20:21:02 +08:00
|
|
|
|
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:
|
|
|
|
i = max(0, len(stack) - 1)
|
|
|
|
while i and stack[i][0].f_locals.get("__tracebackhide__", False):
|
|
|
|
i-=1
|
|
|
|
return stack, i
|
2009-05-23 01:57:21 +08:00
|
|
|
p = Pdb()
|
|
|
|
p.reset()
|
|
|
|
p.interaction(None, t)
|