From 56e631b4bbeedf7ef6da94d3c5eab00c20a07d12 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 3 Jun 2021 21:07:01 +0100 Subject: [PATCH 1/3] fix sys.path for local workers Fixes #421 --- src/xdist/plugin.py | 3 +++ src/xdist/remote.py | 4 +++- src/xdist/workermanage.py | 4 +++- testing/test_remote.py | 16 +++++++++++++++- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/xdist/plugin.py b/src/xdist/plugin.py index 1eba32b8..d5693861 100644 --- a/src/xdist/plugin.py +++ b/src/xdist/plugin.py @@ -1,9 +1,12 @@ import os import uuid +import sys import py import pytest +_sys_path = list(sys.path) # freeze a copy of sys.path at interpreter startup + def pytest_xdist_auto_num_workers(): try: diff --git a/src/xdist/remote.py b/src/xdist/remote.py index 7f95b5cc..d79f0b38 100644 --- a/src/xdist/remote.py +++ b/src/xdist/remote.py @@ -219,12 +219,14 @@ def setup_config(config, basetemp): channel = channel # noqa workerinput, args, option_dict, change_sys_path = channel.receive() - if change_sys_path: + if change_sys_path is None: importpath = os.getcwd() sys.path.insert(0, importpath) os.environ["PYTHONPATH"] = ( importpath + os.pathsep + os.environ.get("PYTHONPATH", "") ) + else: + sys.path = change_sys_path os.environ["PYTEST_XDIST_TESTRUNUID"] = workerinput["testrunuid"] os.environ["PYTEST_XDIST_WORKER"] = workerinput["workerid"] diff --git a/src/xdist/workermanage.py b/src/xdist/workermanage.py index 8fed077e..2c4f1a68 100644 --- a/src/xdist/workermanage.py +++ b/src/xdist/workermanage.py @@ -9,6 +9,7 @@ import execnet import xdist.remote +from xdist.plugin import _sys_path def parse_spec_config(config): @@ -261,7 +262,8 @@ def setup(self): remote_module = self.config.hook.pytest_xdist_getremotemodule() self.channel = self.gateway.remote_exec(remote_module) # change sys.path only for remote workers - change_sys_path = not self.gateway.spec.popen + # restore sys.path from a frozen copy for local workers + change_sys_path = _sys_path if self.gateway.spec.popen else None self.channel.send((self.workerinput, args, option_dict, change_sys_path)) if self.putevent: diff --git a/testing/test_remote.py b/testing/test_remote.py index 2f6e2221..9339fe9f 100644 --- a/testing/test_remote.py +++ b/testing/test_remote.py @@ -234,7 +234,7 @@ def test(): def test_remote_inner_argv(testdir): - """Test/document the behavior due to execnet using `python -c`.""" + """Work around sys.path differences due to execnet using `python -c`.""" testdir.makepyfile( """ import sys @@ -292,3 +292,17 @@ def test(get_config_parser, request): result = testdir.runpytest_subprocess("-n1") assert result.ret == 1 result.stdout.fnmatch_lines(["*usage: *", "*error: my_usage_error"]) + + +def test_remote_sys_path(testdir): + """Test/document the behavior due to execnet using `python -c`.""" + testdir.makepyfile( + """ + import sys + + def test_sys_path(): + assert "" not in sys.path + """ + ) + result = testdir.runpytest("-n1") + assert result.ret == 0 From 02f971d45ff40313d4b31c58396bbd546a120a2b Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 3 Jun 2021 21:46:21 +0100 Subject: [PATCH 2/3] swap docstring --- testing/test_remote.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testing/test_remote.py b/testing/test_remote.py index 9339fe9f..31a6a2ae 100644 --- a/testing/test_remote.py +++ b/testing/test_remote.py @@ -234,7 +234,7 @@ def test(): def test_remote_inner_argv(testdir): - """Work around sys.path differences due to execnet using `python -c`.""" + """Test/document the behavior due to execnet using `python -c`.""" testdir.makepyfile( """ import sys @@ -295,7 +295,7 @@ def test(get_config_parser, request): def test_remote_sys_path(testdir): - """Test/document the behavior due to execnet using `python -c`.""" + """Work around sys.path differences due to execnet using `python -c`.""" testdir.makepyfile( """ import sys From b0722675e033b346382475b1ab2e0e32cdb07ae6 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 16 Jun 2021 10:33:45 +0100 Subject: [PATCH 3/3] add newsfile --- changelog/421.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/421.bugfix.rst diff --git a/changelog/421.bugfix.rst b/changelog/421.bugfix.rst new file mode 100644 index 00000000..121c69c8 --- /dev/null +++ b/changelog/421.bugfix.rst @@ -0,0 +1 @@ +Copy the parent process sys.path into local workers, to work around execnet's python -c adding the current directory to sys.path.