mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
bpo-47239: Fixes py.exe output when run in a virtual environment. (GH-32364)
This commit is contained in:
parent
ca219f6dfc
commit
2390b2236d
3 changed files with 281 additions and 104 deletions
|
@ -2,6 +2,7 @@ import contextlib
|
|||
import itertools
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import sysconfig
|
||||
|
@ -59,12 +60,18 @@ TEST_DATA = {
|
|||
}
|
||||
}
|
||||
|
||||
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):
|
||||
|
@ -185,8 +192,13 @@ class RunPyMixin:
|
|||
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,
|
||||
|
@ -410,6 +422,60 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
|
|||
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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue