Skip to content
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

Fix crash on Python 2 when reusing environments #450

Merged
merged 4 commits into from
Jun 12, 2021
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
26 changes: 23 additions & 3 deletions nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,35 @@ def _check_reused_environment_type(self) -> bool:

def _check_reused_environment_interpreter(self) -> bool:
"""Check if reused environment interpreter is the same."""
program = "import sys; print(getattr(sys, 'real_prefix', sys.base_prefix))"
original = nox.command.run(
[self._resolved_interpreter, "-c", program], silent=True, log=False
original = self._read_base_prefix_from_pyvenv_cfg()
program = (
"import sys; sys.stdout.write(getattr(sys, 'real_prefix', sys.base_prefix))"
)

if original is None:
output = nox.command.run(
[self._resolved_interpreter, "-c", program], silent=True, log=False
)
assert isinstance(output, str)
original = output

created = nox.command.run(
["python", "-c", program], silent=True, log=False, paths=self.bin_paths
)

return original == created

def _read_base_prefix_from_pyvenv_cfg(self) -> Optional[str]:
"""Return the base-prefix entry from pyvenv.cfg, if present."""
path = os.path.join(self.location, "pyvenv.cfg")
if os.path.isfile(path):
with open(path) as io:
for line in io:
key, _, value = line.partition("=")
if key.strip() == "base-prefix":
return value.strip()
return None

@property
def _resolved_interpreter(self) -> str:
"""Return the interpreter, appropriately resolved for the platform.
Expand Down
14 changes: 13 additions & 1 deletion tests/test_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ def test_create(monkeypatch, make_one):
dir_.ensure("test.txt")
assert dir_.join("test.txt").check()
venv.reuse_existing = True
monkeypatch.setattr(nox.virtualenv.nox.command, "run", mock.MagicMock())

venv.create()

Expand Down Expand Up @@ -443,6 +442,19 @@ def test_create_reuse_oldstyle_virtualenv_environment(make_one):
assert reused


def test_create_reuse_python2_environment(make_one):
venv, location = make_one(reuse_existing=True, interpreter="2.7")

try:
venv.create()
except nox.virtualenv.InterpreterNotFound:
pytest.skip("Requires Python 2.7 installation.")

reused = not venv.create()

assert reused


def test_create_venv_backend(make_one):
venv, dir_ = make_one(venv=True)
venv.create()
Expand Down