8000 gh-98251: Allow venv to pass along PYTHON* variables to pip and ensur… · python/cpython@22ad9e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 22ad9e5

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 <steve.dower@python.org>
1 parent 595ef03 commit 22ad9e5

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

Lib/test/test_venv.py

Lines changed: 4 additions & 4 deletions
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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,25 @@ def setup_python(self, context):
338338
shutil.copyfile(src, dst)
339339
break
340340

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

350361
def setup_scripts(self, context):
351362
"""
@@ -444,9 +455,8 @@ def upgrade_dependencies(self, context):
444455
logger.debug(
445456
f'Upgrading {CORE_VENV_DEPS} packages in {context.bin_path}'
446457
)
447-
cmd = [context.env_exec_cmd, '-m', 'pip', 'install', '--upgrade']
448-
cmd.extend(CORE_VENV_DEPS)
449-
subprocess.check_call(cmd)
458+
self._call_new_python(context, '-m', 'pip', 'install', '--upgrade',
459+
*CORE_VENV_DEPS)
450460

451461

452462
def create(env_dir, system_site_packages=False, clear=False,
Lines changed: 2 additions & 0 deletions
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)
0