Skip to content

bpo-47239: Fixes py.exe output when run in a virtual environment. #32364

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
Apr 6, 2022
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
82 changes: 74 additions & 8 deletions Lib/test/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import itertools
import os
import re
import shutil
import subprocess
import sys
import sysconfig
Expand Down Expand Up @@ -59,12 +60,18 @@
}
}

TEST_PY_COMMANDS = textwrap.dedent("""
[defaults]
py_python=PythonTestSuite/3.100
py_python2=PythonTestSuite/3.100-32
py_python3=PythonTestSuite/3.100-arm64
""")

TEST_PY_ENV = dict(
PY_PYTHON="PythonTestSuite/3.100",
PY_PYTHON2="PythonTestSuite/3.100-32",
PY_PYTHON3="PythonTestSuite/3.100-arm64",
)


TEST_PY_COMMANDS = "\n".join([
"[defaults]",
*[f"{k.lower()}={v}" for k, v in TEST_PY_ENV.items()]
])


def create_registry_data(root, data):
Expand Down Expand Up @@ -185,8 +192,13 @@ def run_py(self, args, env=None, allow_fail=False, expect_returncode=0):
if not self.py_exe:
self.py_exe = self.find_py()

env = {**os.environ, **(env or {}), "PYLAUNCHER_DEBUG": "1", "PYLAUNCHER_DRYRUN": "1"}
env.pop("VIRTUAL_ENV", None)
ignore = {"VIRTUAL_ENV", "PY_PYTHON", "PY_PYTHON2", "PY_PYTHON3"}
env = {
**{k.upper(): v for k, v in os.environ.items() if k.upper() not in ignore},
**{k.upper(): v for k, v in (env or {}).items()},
"PYLAUNCHER_DEBUG": "1",
"PYLAUNCHER_DRYRUN": "1",
}
with subprocess.Popen(
[self.py_exe, *args],
env=env,
Expand Down Expand Up @@ -410,6 +422,60 @@ def test_py3_default(self):
self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
self.assertEqual("X.Y-arm64.exe -X fake_arg_for_test -arg", data["stdout"].strip())

def test_py_default_env(self):
data = self.run_py(["-arg"], env=TEST_PY_ENV)
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
self.assertEqual("3.100", data["SearchInfo.tag"])
self.assertEqual("X.Y.exe -arg", data["stdout"].strip())

def test_py2_default_env(self):
data = self.run_py(["-2", "-arg"], env=TEST_PY_ENV)
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
self.assertEqual("3.100-32", data["SearchInfo.tag"])
self.assertEqual("X.Y-32.exe -arg", data["stdout"].strip())

def test_py3_default_env(self):
data = self.run_py(["-3", "-arg"], env=TEST_PY_ENV)
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
self.assertEqual("X.Y-arm64.exe -X fake_arg_for_test -arg", data["stdout"].strip())

def test_py_default_in_list(self):
data = self.run_py(["-0"], env=TEST_PY_ENV)
default = None
for line in data["stdout"].splitlines():
m = re.match(r"\s*-V:(.+?)\s+?\*\s+(.+)$", line)
if m:
default = m.group(1)
break
self.assertEqual("PythonTestSuite/3.100", default)

def test_virtualenv_in_list(self):
venv = Path.cwd() / "Scripts"
venv.mkdir(exist_ok=True, parents=True)
venv_exe = (venv / Path(sys.executable).name)
venv_exe.touch()
try:
data = self.run_py(["-0p"], env={"VIRTUAL_ENV": str(venv.parent)})
for line in data["stdout"].splitlines():
m = re.match(r"\s*\*\s+(.+)$", line)
if m:
self.assertEqual(str(venv_exe), m.group(1))
break
else:
self.fail("did not find active venv path")

data = self.run_py(["-0"], env={"VIRTUAL_ENV": str(venv.parent)})
for line in data["stdout"].splitlines():
m = re.match(r"\s*\*\s+(.+)$", line)
if m:
self.assertEqual("Active venv", m.group(1))
break
else:
self.fail("did not find active venv entry")
finally:
shutil.rmtree(venv)

def test_py_shebang(self):
with self.py_ini(TEST_PY_COMMANDS):
with self.script("#! /usr/bin/env python -prearg") as script:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed --list and --list-paths output for :ref:`launcher` when used in an
active virtual environment.
Loading