2010-11-06 18:38:53 +08:00
|
|
|
""" interactive debugging with PDB, the Python Debugger. """
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2014-08-01 06:13:40 +08:00
|
|
|
import pdb
|
2010-10-06 17:55:12 +08:00
|
|
|
import sys
|
2009-05-23 01:57:21 +08:00
|
|
|
|
2014-08-01 06:13:40 +08:00
|
|
|
|
2009-05-23 01:57:21 +08:00
|
|
|
def pytest_addoption(parser):
|
2010-07-27 03:15:15 +08:00
|
|
|
group = parser.getgroup("general")
|
2016-07-10 11:55:43 +08:00
|
|
|
group._addoption(
|
|
|
|
'--pdb', dest="usepdb", action="store_true",
|
|
|
|
help="start the interactive Python debugger on errors.")
|
|
|
|
group._addoption(
|
|
|
|
'--pdbcls', dest="usepdb_cls", metavar="modulename:classname",
|
2016-07-12 08:43:06 +08:00
|
|
|
help="start a custom interactive Python debugger on errors. "
|
2016-08-30 03:15:12 +08:00
|
|
|
"For example: --pdbcls=IPython.terminal.debugger:TerminalPdb")
|
2009-05-23 01:57:21 +08:00
|
|
|
|
2010-10-06 20:48:24 +08:00
|
|
|
|
2010-01-13 23:00:33 +08:00
|
|
|
def pytest_configure(config):
|
2016-09-19 23:43:54 +08:00
|
|
|
if config.getvalue("usepdb_cls"):
|
|
|
|
modname, classname = config.getvalue("usepdb_cls").split(":")
|
|
|
|
__import__(modname)
|
|
|
|
pdb_cls = getattr(sys.modules[modname], classname)
|
|
|
|
else:
|
|
|
|
pdb_cls = pdb.Pdb
|
|
|
|
|
|
|
|
if config.getvalue("usepdb"):
|
2010-10-06 20:48:24 +08:00
|
|
|
config.pluginmanager.register(PdbInvoke(), 'pdbinvoke')
|
|
|
|
|
2014-08-01 06:13:40 +08:00
|
|
|
old = (pdb.set_trace, pytestPDB._pluginmanager)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-09-06 21:29:00 +08:00
|
|
|
def fin():
|
2014-08-01 06:13:40 +08:00
|
|
|
pdb.set_trace, pytestPDB._pluginmanager = old
|
2015-07-19 03:39:55 +08:00
|
|
|
pytestPDB._config = None
|
2016-07-10 11:55:43 +08:00
|
|
|
pytestPDB._pdb_cls = pdb.Pdb
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
pdb.set_trace = pytestPDB.set_trace
|
2014-03-14 19:49:37 +08:00
|
|
|
pytestPDB._pluginmanager = config.pluginmanager
|
2015-07-19 03:39:55 +08:00
|
|
|
pytestPDB._config = config
|
2016-09-19 23:43:54 +08:00
|
|
|
pytestPDB._pdb_cls = pdb_cls
|
2013-09-06 21:29:00 +08:00
|
|
|
config._cleanup.append(fin)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-01-25 04:23:42 +08:00
|
|
|
class pytestPDB(object):
|
2010-10-06 20:48:24 +08:00
|
|
|
""" Pseudo PDB that defers to the real pdb. """
|
2014-03-14 19:49:37 +08:00
|
|
|
_pluginmanager = None
|
2015-07-19 03:39:55 +08:00
|
|
|
_config = None
|
2016-07-10 11:55:43 +08:00
|
|
|
_pdb_cls = pdb.Pdb
|
2010-10-06 20:48:24 +08:00
|
|
|
|
2017-02-28 20:40:38 +08:00
|
|
|
@classmethod
|
|
|
|
def set_trace(cls):
|
2010-10-06 20:48:24 +08:00
|
|
|
""" invoke PDB set_trace debugging, dropping any IO capturing. """
|
2015-07-19 03:39:55 +08:00
|
|
|
import _pytest.config
|
2010-10-06 20:48:24 +08:00
|
|
|
frame = sys._getframe().f_back
|
2017-02-28 20:40:38 +08:00
|
|
|
if cls._pluginmanager is not None:
|
|
|
|
capman = cls._pluginmanager.getplugin("capturemanager")
|
2014-03-14 19:49:36 +08:00
|
|
|
if capman:
|
2017-09-27 06:54:26 +08:00
|
|
|
capman.suspend_global_capture(in_=True)
|
2017-02-28 20:40:38 +08:00
|
|
|
tw = _pytest.config.create_terminal_writer(cls._config)
|
2010-10-06 20:48:24 +08:00
|
|
|
tw.line()
|
|
|
|
tw.sep(">", "PDB set_trace (IO-capturing turned off)")
|
2017-02-28 20:40:38 +08:00
|
|
|
cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config)
|
|
|
|
cls._pdb_cls().set_trace(frame)
|
2010-10-06 20:48:24 +08:00
|
|
|
|
2012-06-23 17:32:32 +08:00
|
|
|
|
2018-01-25 04:23:42 +08:00
|
|
|
class PdbInvoke(object):
|
2013-09-06 17:56:04 +08:00
|
|
|
def pytest_exception_interact(self, node, call, report):
|
2014-03-14 19:49:36 +08:00
|
|
|
capman = node.config.pluginmanager.getplugin("capturemanager")
|
|
|
|
if capman:
|
2017-09-27 06:54:26 +08:00
|
|
|
out, err = capman.suspend_global_capture(in_=True)
|
2016-01-08 20:41:01 +08:00
|
|
|
sys.stdout.write(out)
|
|
|
|
sys.stdout.write(err)
|
2014-04-01 20:32:12 +08:00
|
|
|
_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"):
|
2017-03-16 01:00:59 +08:00
|
|
|
sys.stderr.write("INTERNALERROR> %s\n" % line)
|
2013-09-06 17:56:04 +08:00
|
|
|
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.
|
2017-11-24 05:26:57 +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:
|
2014-08-01 06:13:40 +08:00
|
|
|
from doctest import UnexpectedException
|
|
|
|
if isinstance(excinfo.value, UnexpectedException):
|
2013-04-16 16:18:08 +08:00
|
|
|
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):
|
2016-07-10 11:55:43 +08:00
|
|
|
class Pdb(pytestPDB._pdb_cls):
|
2010-10-06 17:55:12 +08:00
|
|
|
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)
|