Skip to content

Commit 8152b68

Browse files
authored
Merge pull request #4419 from blueyed/set_trace-kwargs
pdb: support kwargs with `pdb.set_trace`
2 parents 0e4e8e0 + 92a2884 commit 8152b68

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

changelog/4416.feature.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pdb: support keyword arguments with ``pdb.set_trace``
2+
3+
It handles ``header`` similar to Python 3.7 does it, and forwards any
4+
other keyword arguments to the ``Pdb`` constructor.
5+
6+
This allows for ``__import__("pdb").set_trace(skip=["foo.*"])``.

src/_pytest/debugging.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,21 @@ class pytestPDB(object):
7777
_saved = []
7878

7979
@classmethod
80-
def set_trace(cls, set_break=True):
81-
""" invoke PDB set_trace debugging, dropping any IO capturing. """
80+
def _init_pdb(cls, *args, **kwargs):
81+
""" Initialize PDB debugging, dropping any IO capturing. """
8282
import _pytest.config
8383

84-
frame = sys._getframe().f_back
8584
if cls._pluginmanager is not None:
8685
capman = cls._pluginmanager.getplugin("capturemanager")
8786
if capman:
8887
capman.suspend_global_capture(in_=True)
8988
tw = _pytest.config.create_terminal_writer(cls._config)
9089
tw.line()
91-
if capman and capman.is_globally_capturing():
90+
# Handle header similar to pdb.set_trace in py37+.
91+
header = kwargs.pop("header", None)
92+
if header is not None:
93+
tw.sep(">", header)
94+
elif capman and capman.is_globally_capturing():
9295
tw.sep(">", "PDB set_trace (IO-capturing turned off)")
9396
else:
9497
tw.sep(">", "PDB set_trace")
@@ -129,13 +132,18 @@ def setup(self, f, tb):
129132
self._pytest_capman.suspend_global_capture(in_=True)
130133
return ret
131134

132-
_pdb = _PdbWrapper()
135+
_pdb = _PdbWrapper(**kwargs)
133136
cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config, pdb=_pdb)
134137
else:
135-
_pdb = cls._pdb_cls()
138+
_pdb = cls._pdb_cls(**kwargs)
139+
return _pdb
136140

137-
if set_break:
138-
_pdb.set_trace(frame)
141+
@classmethod
142+
def set_trace(cls, *args, **kwargs):
143+
"""Invoke debugging via ``Pdb.set_trace``, dropping any IO capturing."""
144+
frame = sys._getframe().f_back
145+
_pdb = cls._init_pdb(*args, **kwargs)
146+
_pdb.set_trace(frame)
139147

140148

141149
class PdbInvoke(object):
@@ -161,9 +169,9 @@ def pytest_pyfunc_call(self, pyfuncitem):
161169

162170

163171
def _test_pytest_function(pyfuncitem):
164-
pytestPDB.set_trace(set_break=False)
172+
_pdb = pytestPDB._init_pdb()
165173
testfunction = pyfuncitem.obj
166-
pyfuncitem.obj = pdb.runcall
174+
pyfuncitem.obj = _pdb.runcall
167175
if pyfuncitem._isyieldedfunction():
168176
arg_list = list(pyfuncitem._args)
169177
arg_list.insert(0, testfunction)

testing/test_pdb.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,28 @@ def test_1():
389389
assert "hello17" in rest # out is captured
390390
self.flush(child)
391391

392+
def test_pdb_set_trace_kwargs(self, testdir):
393+
p1 = testdir.makepyfile(
394+
"""
395+
import pytest
396+
def test_1():
397+
i = 0
398+
print("hello17")
399+
pytest.set_trace(header="== my_header ==")
400+
x = 3
401+
"""
402+
)
403+
child = testdir.spawn_pytest(str(p1))
404+
child.expect("== my_header ==")
405+
assert "PDB set_trace" not in child.before.decode()
406+
child.expect("Pdb")
407+
child.sendeof()
408+
rest = child.read().decode("utf-8")
409+
assert "1 failed" in rest
410+
assert "def test_1" in rest
411+
assert "hello17" in rest # out is captured
412+
self.flush(child)
413+
392414
def test_pdb_set_trace_interception(self, testdir):
393415
p1 = testdir.makepyfile(
394416
"""
@@ -633,6 +655,12 @@ def test_pdb_custom_cls_with_settrace(self, testdir, monkeypatch):
633655
testdir.makepyfile(
634656
custom_pdb="""
635657
class CustomPdb(object):
658+
def __init__(self, *args, **kwargs):
659+
skip = kwargs.pop("skip")
660+
assert skip == ["foo.*"]
661+
print("__init__")
662+
super(CustomPdb, self).__init__(*args, **kwargs)
663+
636664
def set_trace(*args, **kwargs):
637665
print('custom set_trace>')
638666
"""
@@ -642,12 +670,13 @@ def set_trace(*args, **kwargs):
642670
import pytest
643671
644672
def test_foo():
645-
pytest.set_trace()
673+
pytest.set_trace(skip=['foo.*'])
646674
"""
647675
)
648676
monkeypatch.setenv("PYTHONPATH", str(testdir.tmpdir))
649677
child = testdir.spawn_pytest("--pdbcls=custom_pdb:CustomPdb %s" % str(p1))
650678

679+
child.expect("__init__")
651680
child.expect("custom set_trace>")
652681
self.flush(child)
653682

0 commit comments

Comments
 (0)