Skip to content

Commit

Permalink
pytest_{enter,leave}_pdb: pass through pdb instance
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Oct 25, 2018
1 parent a4ea66c commit ede3a4e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
3 changes: 3 additions & 0 deletions changelog/2619.feature.rst
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
Resume capturing output after ``continue`` with ``__import__("pdb").set_trace()``.

This also adds a new ``pytest_leave_pdb`` hook, and passes in ``pdb`` to the
existing ``pytest_enter_pdb`` hook.
6 changes: 4 additions & 2 deletions src/_pytest/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def set_trace(cls, set_break=True):
tw = _pytest.config.create_terminal_writer(cls._config)
tw.line()
tw.sep(">", "PDB set_trace (IO-capturing turned off)")
cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config)

class _PdbWrapper(cls._pdb_cls, object):
_pytest_capman = capman
Expand All @@ -108,7 +107,9 @@ def do_continue(self, arg):
tw.line()
tw.sep(">", "PDB continue (IO-capturing resumed)")
self._pytest_capman.resume_global_capture()
cls._pluginmanager.hook.pytest_leave_pdb(config=cls._config)
cls._pluginmanager.hook.pytest_leave_pdb(
config=cls._config, pdb=self
)
self._continued = True
return ret

Expand All @@ -129,6 +130,7 @@ def setup(self, f, tb):
return ret

_pdb = _PdbWrapper()
cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config, pdb=_pdb)
else:
_pdb = cls._pdb_cls()

Expand Down
6 changes: 4 additions & 2 deletions src/_pytest/hookspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,19 +603,21 @@ def pytest_exception_interact(node, call, report):
"""


def pytest_enter_pdb(config):
def pytest_enter_pdb(config, pdb):
""" called upon pdb.set_trace(), can be used by plugins to take special
action just before the python debugger enters in interactive mode.
:param _pytest.config.Config config: pytest config object
:param pdb.Pdb pdb: Pdb instance
"""


def pytest_leave_pdb(config):
def pytest_leave_pdb(config, pdb):
""" called when leaving pdb (e.g. with continue after pdb.set_trace()).
Can be used by plugins to take special action just after the python
debugger leaves interactive mode.
:param _pytest.config.Config config: pytest config object
:param pdb.Pdb pdb: Pdb instance
"""
14 changes: 12 additions & 2 deletions testing/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,16 +550,26 @@ def test_pdb_collection_failure_is_shown(self, testdir):
def test_enter_leave_pdb_hooks_are_called(self, testdir):
testdir.makeconftest(
"""
mypdb = None
def pytest_configure(config):
config.testing_verification = 'configured'
def pytest_enter_pdb(config):
def pytest_enter_pdb(config, pdb):
assert config.testing_verification == 'configured'
print('enter_pdb_hook')
def pytest_leave_pdb(config):
global mypdb
mypdb = pdb
mypdb.set_attribute = "bar"
def pytest_leave_pdb(config, pdb):
assert config.testing_verification == 'configured'
print('leave_pdb_hook')
global mypdb
assert mypdb is pdb
assert mypdb.set_attribute == "bar"
"""
)
p1 = testdir.makepyfile(
Expand Down

0 comments on commit ede3a4e

Please sign in to comment.