gh-98251: Allow venv to pass along PYTHON* variables to pip and ensurepip when they do not impact path resolution (GH-98259)

This commit is contained in:
Steve Dower 2022-10-14 16:58:54 +01:00 committed by GitHub
parent b863b9cd4b
commit 2fe44f728a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 13 deletions

View file

@ -216,7 +216,7 @@ def test_upgrade_dependencies(self):
if sys.platform == 'win32': if sys.platform == 'win32':
expect_exe = os.path.normcase(os.path.realpath(expect_exe)) expect_exe = os.path.normcase(os.path.realpath(expect_exe))
def pip_cmd_checker(cmd): def pip_cmd_checker(cmd, **kwargs):
cmd[0] = os.path.normcase(cmd[0]) cmd[0] = os.path.normcase(cmd[0])
self.assertEqual( self.assertEqual(
cmd, cmd,
@ -232,7 +232,7 @@ def pip_cmd_checker(cmd):
) )
fake_context = builder.ensure_directories(fake_env_dir) fake_context = builder.ensure_directories(fake_env_dir)
with patch('venv.subprocess.check_call', pip_cmd_checker): with patch('venv.subprocess.check_output', pip_cmd_checker):
builder.upgrade_dependencies(fake_context) builder.upgrade_dependencies(fake_context)
@requireVenvCreate @requireVenvCreate
@ -659,8 +659,8 @@ def nicer_error(self):
try: try:
yield yield
except subprocess.CalledProcessError as exc: except subprocess.CalledProcessError as exc:
out = exc.output.decode(errors="replace") out = (exc.output or b'').decode(errors="replace")
err = exc.stderr.decode(errors="replace") err = (exc.stderr or b'').decode(errors="replace")
self.fail( self.fail(
f"{exc}\n\n" f"{exc}\n\n"
f"**Subprocess Output**\n{out}\n\n" f"**Subprocess Output**\n{out}\n\n"

View file

@ -339,14 +339,25 @@ def setup_python(self, context):
shutil.copyfile(src, dst) shutil.copyfile(src, dst)
break break
def _call_new_python(self, context, *py_args, **kwargs):
"""Executes the newly created Python using safe-ish options"""
# gh-98251: We do not want to just use '-I' because that masks
# legitimate user preferences (such as not writing bytecode). All we
# really need is to ensure that the path variables do not overrule
# normal venv handling.
args = [context.env_exec_cmd, *py_args]
kwargs['env'] = env = os.environ.copy()
env['VIRTUAL_ENV'] = context.env_dir
env.pop('PYTHONHOME', None)
env.pop('PYTHONPATH', None)
kwargs['cwd'] = context.env_dir
kwargs['executable'] = context.env_exec_cmd
subprocess.check_output(args, **kwargs)
def _setup_pip(self, context): def _setup_pip(self, context):
"""Installs or upgrades pip in a virtual environment""" """Installs or upgrades pip in a virtual environment"""
# We run ensurepip in isolated mode to avoid side effects from self._call_new_python(context, '-m', 'ensurepip', '--upgrade',
# environment vars, the current directory and anything else '--default-pip', stderr=subprocess.STDOUT)
# intended for the global Python environment
cmd = [context.env_exec_cmd, '-Im', 'ensurepip', '--upgrade',
'--default-pip']
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
def setup_scripts(self, context): def setup_scripts(self, context):
""" """
@ -445,9 +456,8 @@ def upgrade_dependencies(self, context):
logger.debug( logger.debug(
f'Upgrading {CORE_VENV_DEPS} packages in {context.bin_path}' f'Upgrading {CORE_VENV_DEPS} packages in {context.bin_path}'
) )
cmd = [context.env_exec_cmd, '-m', 'pip', 'install', '--upgrade'] self._call_new_python(context, '-m', 'pip', 'install', '--upgrade',
cmd.extend(CORE_VENV_DEPS) *CORE_VENV_DEPS)
subprocess.check_call(cmd)
def create(env_dir, system_site_packages=False, clear=False, def create(env_dir, system_site_packages=False, clear=False,

View file

@ -0,0 +1,2 @@
Allow :mod:`venv` to pass along :envvar:`PYTHON*` variables to ``ensurepip`` and ``pip`` when
they do not impact path resolution