Skip to content

Commit c9da063

Browse files
gh-98251: Allow venv to pass along PYTHON* variables to pip and ensurepip when they do not impact path resolution (GH-98259)
(cherry picked from commit 2fe44f7) Co-authored-by: Steve Dower <[email protected]>
1 parent fa9f65e commit c9da063

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
@@ -159,7 +159,7 @@ def test_upgrade_dependencies(self):
159159
if sys.platform == 'win32':
160160
expect_exe = os.path.normcase(os.path.realpath(expect_exe))
161161

162-
def pip_cmd_checker(cmd):
162+
def pip_cmd_checker(cmd, **kwargs):
163163
cmd[0] = os.path.normcase(cmd[0])
164164
self.assertEqual(
165165
cmd,
@@ -175,7 +175,7 @@ def pip_cmd_checker(cmd):
175175
)
176176

177177
fake_context = builder.ensure_directories(fake_env_dir)
178-
with patch('venv.subprocess.check_call', pip_cmd_checker):
178+
with patch('venv.subprocess.check_output', pip_cmd_checker):
179179
builder.upgrade_dependencies(fake_context)
180180

181181
@requireVenvCreate
@@ -547,8 +547,8 @@ def nicer_error(self):
547547
try:
548548
yield
549549
except subprocess.CalledProcessError as exc:
550-
out = exc.output.decode(errors="replace")
551-
err = exc.stderr.decode(errors="replace")
550+
out = (exc.output or b'').decode(errors="replace")
551+
err = (exc.stderr or b'').decode(errors="replace")
552552
self.fail(
553553
f"{exc}\n\n"
554554
f"**Subprocess Output**\n{out}\n\n"

Lib/venv/__init__.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,25 @@ def setup_python(self, context):
308308
shutil.copyfile(src, dst)
309309
break
310310

311+
def _call_new_python(self, context, *py_args, **kwargs):
312+
"""Executes the newly created Python using safe-ish options"""
313+
# gh-98251: We do not want to just use '-I' because that masks
314+
# legitimate user preferences (such as not writing bytecode). All we
315+
# really need is to ensure that the path variables do not overrule
316+
# normal venv handling.
317+
args = [context.env_exec_cmd, *py_args]
318+
kwargs['env'] = env = os.environ.copy()
319+
env['VIRTUAL_ENV'] = context.env_dir
320+
env.pop('PYTHONHOME', None)
321+
env.pop('PYTHONPATH', None)
322+
kwargs['cwd'] = context.env_dir
323+
kwargs['executable'] = context.env_exec_cmd
324+
subprocess.check_output(args, **kwargs)
325+
311326
def _setup_pip(self, context):
312327
"""Installs or upgrades pip in a virtual environment"""
313-
# We run ensurepip in isolated mode to avoid side effects from
314-
# environment vars, the current directory and anything else
315-
# intended for the global Python environment
316-
cmd = [context.env_exec_cmd, '-Im', 'ensurepip', '--upgrade',
317-
'--default-pip']
318-
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
328+
self._call_new_python(context, '-m', 'ensurepip', '--upgrade',
329+
'--default-pip', stderr=subprocess.STDOUT)
319330

320331
def setup_scripts(self, context):
321332
"""
@@ -414,9 +425,8 @@ def upgrade_dependencies(self, context):
414425
logger.debug(
415426
f'Upgrading {CORE_VENV_DEPS} packages in {context.bin_path}'
416427
)
417-
cmd = [context.env_exec_cmd, '-m', 'pip', 'install', '--upgrade']
418-
cmd.extend(CORE_VENV_DEPS)
419-
subprocess.check_call(cmd)
428+
self._call_new_python(context, '-m', 'pip', 'install', '--upgrade',
429+
*CORE_VENV_DEPS)
420430

421431

422432
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)