Skip to content

Commit 2390b22

Browse files
authored
bpo-47239: Fixes py.exe output when run in a virtual environment. (GH-32364)
1 parent ca219f6 commit 2390b22

File tree

3 files changed

+281
-104
lines changed

3 files changed

+281
-104
lines changed

Lib/test/test_launcher.py

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import itertools
33
import os
44
import re
5+
import shutil
56
import subprocess
67
import sys
78
import sysconfig
@@ -59,12 +60,18 @@
5960
}
6061
}
6162

62-
TEST_PY_COMMANDS = textwrap.dedent("""
63-
[defaults]
64-
py_python=PythonTestSuite/3.100
65-
py_python2=PythonTestSuite/3.100-32
66-
py_python3=PythonTestSuite/3.100-arm64
67-
""")
63+
64+
TEST_PY_ENV = dict(
65+
PY_PYTHON="PythonTestSuite/3.100",
66+
PY_PYTHON2="PythonTestSuite/3.100-32",
67+
PY_PYTHON3="PythonTestSuite/3.100-arm64",
68+
)
69+
70+
71+
TEST_PY_COMMANDS = "\n".join([
72+
"[defaults]",
73+
*[f"{k.lower()}={v}" for k, v in TEST_PY_ENV.items()]
74+
])
6875

6976

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

188-
env = {**os.environ, **(env or {}), "PYLAUNCHER_DEBUG": "1", "PYLAUNCHER_DRYRUN": "1"}
189-
env.pop("VIRTUAL_ENV", None)
195+
ignore = {"VIRTUAL_ENV", "PY_PYTHON", "PY_PYTHON2", "PY_PYTHON3"}
196+
env = {
197+
**{k.upper(): v for k, v in os.environ.items() if k.upper() not in ignore},
198+
**{k.upper(): v for k, v in (env or {}).items()},
199+
"PYLAUNCHER_DEBUG": "1",
200+
"PYLAUNCHER_DRYRUN": "1",
201+
}
190202
with subprocess.Popen(
191203
[self.py_exe, *args],
192204
env=env,
@@ -410,6 +422,60 @@ def test_py3_default(self):
410422
self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
411423
self.assertEqual("X.Y-arm64.exe -X fake_arg_for_test -arg", data["stdout"].strip())
412424

425+
def test_py_default_env(self):
426+
data = self.run_py(["-arg"], env=TEST_PY_ENV)
427+
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
428+
self.assertEqual("3.100", data["SearchInfo.tag"])
429+
self.assertEqual("X.Y.exe -arg", data["stdout"].strip())
430+
431+
def test_py2_default_env(self):
432+
data = self.run_py(["-2", "-arg"], env=TEST_PY_ENV)
433+
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
434+
self.assertEqual("3.100-32", data["SearchInfo.tag"])
435+
self.assertEqual("X.Y-32.exe -arg", data["stdout"].strip())
436+
437+
def test_py3_default_env(self):
438+
data = self.run_py(["-3", "-arg"], env=TEST_PY_ENV)
439+
self.assertEqual("PythonTestSuite", data["SearchInfo.company"])
440+
self.assertEqual("3.100-arm64", data["SearchInfo.tag"])
441+
self.assertEqual("X.Y-arm64.exe -X fake_arg_for_test -arg", data["stdout"].strip())
442+
443+
def test_py_default_in_list(self):
444+
data = self.run_py(["-0"], env=TEST_PY_ENV)
445+
default = None
446+
for line in data["stdout"].splitlines():
447+
m = re.match(r"\s*-V:(.+?)\s+?\*\s+(.+)$", line)
448+
if m:
449+
default = m.group(1)
450+
break
451+
self.assertEqual("PythonTestSuite/3.100", default)
452+
453+
def test_virtualenv_in_list(self):
454+
venv = Path.cwd() / "Scripts"
455+
venv.mkdir(exist_ok=True, parents=True)
456+
venv_exe = (venv / Path(sys.executable).name)
457+
venv_exe.touch()
458+
try:
459+
data = self.run_py(["-0p"], env={"VIRTUAL_ENV": str(venv.parent)})
460+
for line in data["stdout"].splitlines():
461+
m = re.match(r"\s*\*\s+(.+)$", line)
462+
if m:
463+
self.assertEqual(str(venv_exe), m.group(1))
464+
break
465+
else:
466+
self.fail("did not find active venv path")
467+
468+
data = self.run_py(["-0"], env={"VIRTUAL_ENV": str(venv.parent)})
469+
for line in data["stdout"].splitlines():
470+
m = re.match(r"\s*\*\s+(.+)$", line)
471+
if m:
472+
self.assertEqual("Active venv", m.group(1))
473+
break
474+
else:
475+
self.fail("did not find active venv entry")
476+
finally:
477+
shutil.rmtree(venv)
478+
413479
def test_py_shebang(self):
414480
with self.py_ini(TEST_PY_COMMANDS):
415481
with self.script("#! /usr/bin/env python -prearg") as script:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed --list and --list-paths output for :ref:`launcher` when used in an
2+
active virtual environment.

0 commit comments

Comments
 (0)