Skip to content

debugging: make pytest_configure re-entrant #4287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/4287.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix nested usage of debugging plugin (pdb), e.g. with pytester's ``testdir.runpytest``.
22 changes: 15 additions & 7 deletions src/_pytest/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,24 @@ def pytest_configure(config):
if config.getvalue("usepdb"):
config.pluginmanager.register(PdbInvoke(), "pdbinvoke")

old = (pdb.set_trace, pytestPDB._pluginmanager)

def fin():
pdb.set_trace, pytestPDB._pluginmanager = old
pytestPDB._config = None
pytestPDB._pdb_cls = pdb.Pdb

pytestPDB._saved.append(
(pdb.set_trace, pytestPDB._pluginmanager, pytestPDB._config, pytestPDB._pdb_cls)
)
pdb.set_trace = pytestPDB.set_trace
pytestPDB._pluginmanager = config.pluginmanager
pytestPDB._config = config
pytestPDB._pdb_cls = pdb_cls

# NOTE: not using pytest_unconfigure, since it might get called although
# pytest_configure was not (if another plugin raises UsageError).
def fin():
(
pdb.set_trace,
pytestPDB._pluginmanager,
pytestPDB._config,
pytestPDB._pdb_cls,
) = pytestPDB._saved.pop()

config._cleanup.append(fin)


Expand All @@ -67,6 +74,7 @@ class pytestPDB(object):
_pluginmanager = None
_config = None
_pdb_cls = pdb.Pdb
_saved = []

@classmethod
def set_trace(cls, set_break=True):
Expand Down
27 changes: 27 additions & 0 deletions testing/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,3 +784,30 @@ def test_1():
assert "1 passed" in rest
assert "reading from stdin while output" not in rest
TestPDB.flush(child)


def test_trace_after_runpytest(testdir):
"""Test that debugging's pytest_configure is re-entrant."""
p1 = testdir.makepyfile(
"""
from _pytest.debugging import pytestPDB

def test_outer(testdir):
from _pytest.debugging import pytestPDB

assert len(pytestPDB._saved) == 1

testdir.runpytest("-k test_inner")

__import__('pdb').set_trace()

def test_inner(testdir):
assert len(pytestPDB._saved) == 2
"""
)
child = testdir.spawn_pytest("-p pytester %s -k test_outer" % p1)
child.expect(r"\(Pdb")
child.sendline("c")
rest = child.read().decode("utf8")
TestPDB.flush(child)
assert child.exitstatus == 0, rest