Skip to content

Commit bc44fea

Browse files
authored
Merge pull request #415 from jirikuncar/numprocesses-auto-with-pdb
Improve behavior of --numprocesses=auto and --pdb
2 parents ca03269 + 6d544ce commit bc44fea

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

changelog/415.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve behavior of ``--numprocesses=auto`` to work well with ``--pdb`` option.

testing/test_plugin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def test_dist_options(testdir):
3636

3737
def test_auto_detect_cpus(testdir, monkeypatch):
3838
import os
39+
from xdist.plugin import pytest_cmdline_main as check_options
3940

4041
if hasattr(os, "sched_getaffinity"):
4142
monkeypatch.setattr(os, "sched_getaffinity", lambda _pid: set(range(99)))
@@ -52,6 +53,11 @@ def test_auto_detect_cpus(testdir, monkeypatch):
5253
config = testdir.parseconfigure("-nauto")
5354
assert config.getoption("numprocesses") == 99
5455

56+
config = testdir.parseconfigure("-nauto", "--pdb")
57+
check_options(config)
58+
assert config.getoption("usepdb")
59+
assert config.getoption("numprocesses") == 0
60+
5561
monkeypatch.delattr(os, "sched_getaffinity", raising=False)
5662
monkeypatch.setenv("TRAVIS", "true")
5763
config = testdir.parseconfigure("-nauto")

testing/test_remote.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,4 @@ def test(get_config_parser, request):
457457

458458
result = testdir.runpytest_subprocess("-n1")
459459
assert result.ret == 1
460-
result.stdout.fnmatch_lines(
461-
["usage: pytest.py *", "pytest.py: error: my_usage_error"]
462-
)
460+
result.stdout.fnmatch_lines(["*usage: *", "*error: my_usage_error"])

xdist/plugin.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ def cpu_count():
2727
return n if n else 1
2828

2929

30+
class AutoInt(int):
31+
"""Mark value as auto-detected."""
32+
33+
3034
def parse_numprocesses(s):
3135
if s == "auto":
32-
return auto_detect_cpus()
33-
else:
36+
return AutoInt(auto_detect_cpus())
37+
elif s is not None:
3438
return int(s)
3539

3640

@@ -45,7 +49,7 @@ def pytest_addoption(parser):
4549
type=parse_numprocesses,
4650
help="shortcut for '--dist=load --tx=NUM*popen', "
4751
"you can use 'auto' here for auto detection CPUs number on "
48-
"host system",
52+
"host system and it will be 0 when used with --pdb",
4953
)
5054
group.addoption(
5155
"--maxprocesses",
@@ -177,6 +181,10 @@ def pytest_configure(config):
177181

178182
@pytest.mark.tryfirst
179183
def pytest_cmdline_main(config):
184+
usepdb = config.getoption("usepdb") # a core option
185+
if isinstance(config.option.numprocesses, AutoInt):
186+
config.option.numprocesses = 0 if usepdb else int(config.option.numprocesses)
187+
180188
if config.option.numprocesses:
181189
if config.option.dist == "no":
182190
config.option.dist = "load"
@@ -188,11 +196,10 @@ def pytest_cmdline_main(config):
188196
config.option.dist = "load"
189197
val = config.getvalue
190198
if not val("collectonly"):
191-
usepdb = config.getoption("usepdb") # a core option
192199
if val("dist") != "no":
193200
if usepdb:
194201
raise pytest.UsageError(
195-
"--pdb is incompatible with distributing tests; try using -n0."
202+
"--pdb is incompatible with distributing tests; try using -n0 or -nauto."
196203
) # noqa: E501
197204

198205

0 commit comments

Comments
 (0)