Skip to content

Commit 2fe44f7

Browse files
authored
pythongh-98251: Allow venv to pass along PYTHON* variables to pip and ensurepip when they do not impact path resolution (pythonGH-98259)
1 parent b863b9c commit 2fe44f7

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

Lib/test/test_venv.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def test_upgrade_dependencies(self):
216216
if sys.platform == 'win32':
217217
expect_exe = os.path.normcase(os.path.realpath(expect_exe))
218218

219-
def pip_cmd_checker(cmd):
219+
def pip_cmd_checker(cmd, **kwargs):
220220
cmd[0] = os.path.normcase(cmd[0])
221221
self.assertEqual(
222222
cmd,
@@ -232,7 +232,7 @@ def pip_cmd_checker(cmd):
232232
)
233233

234234
fake_context = builder.ensure_directories(fake_env_dir)
235-
with patch('venv.subprocess.check_call', pip_cmd_checker):
235+
with patch('venv.subprocess.check_output', pip_cmd_checker):
236236
builder.upgrade_dependencies(fake_context)
237237

238238
@requireVenvCreate
@@ -659,8 +659,8 @@ def nicer_error(self):
659659
try:
660660
yield
661661
except subprocess.CalledProcessError as exc:
662-
out = exc.output.decode(errors="replace")
663-
err = exc.stderr.decode(errors="replace")
662+
out = (exc.output or b'').decode(errors="replace")
663+
err = (exc.stderr or b'').decode(errors="replace")
664664
self.fail(
665665
f"{exc}\n\n"
666666
f"**Subprocess Output**\n{out}\n\n"

Lib/venv/__init__.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,25 @@ def setup_python(self, context):
339339
shutil.copyfile(src, dst)
340340
break
341341

342+
def _call_new_python(self, context, *py_args, **kwargs):
343+
"""Executes the newly created Python using safe-ish options"""
344+
# gh-98251: We do not want to just use '-I' because that masks
345+
# legitimate user preferences (such as not writing bytecode). All we
346+
# really need is to ensure that the path variables do not overrule
347+
# normal venv handling.
348+
args = [context.env_exec_cmd, *py_args]
349+
kwargs['env'] = env = os.environ.copy()
350+
env['VIRTUAL_ENV'] = context.env_dir
351+
env.pop('PYTHONHOME', None)
352+
env.pop('PYTHONPATH', None)
353+
kwargs['cwd'] = context.env_dir
354+
kwargs['executable'] = context.env_exec_cmd
355+
subprocess.check_output(args, **kwargs)
356+
342357
def _setup_pip(self, context):
343358
"""Installs or upgrades pip in a virtual environment"""
344-
# We run ensurepip in isolated mode to avoid side effects from
345-
# environment vars, the current directory and anything else
346-
# intended for the global Python environment
347-
cmd = [context.env_exec_cmd, '-Im', 'ensurepip', '--upgrade',
348-
'--default-pip']
349-
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
359+
self._call_new_python(context, '-m', 'ensurepip', '--upgrade',
360+
'--default-pip', stderr=subprocess.STDOUT)
350361

351362
def setup_scripts(self, context):
352363
"""
@@ -445,9 +456,8 @@ def upgrade_dependencies(self, context):
445456
logger.debug(
446457
f'Upgrading {CORE_VENV_DEPS} packages in {context.bin_path}'
447458
)
448-
cmd = [context.env_exec_cmd, '-m', 'pip', 'install', '--upgrade']
449-
cmd.extend(CORE_VENV_DEPS)
450-
subprocess.check_call(cmd)
459+
self._call_new_python(context, '-m', 'pip', 'install', '--upgrade',
460+
*CORE_VENV_DEPS)
451461

452462

453463
def create(env_dir, system_site_packages=False, clear=False,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Allow :mod:`venv` to pass along :envvar:`PYTHON*` variables to ``ensurepip`` and ``pip`` when
2+
they do not impact path resolution

0 commit comments

Comments
 (0)