summaryrefslogtreecommitdiffstats
path: root/Lib/venv
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@python.org>2022-10-14 15:58:54 (GMT)
committerGitHub <noreply@github.com>2022-10-14 15:58:54 (GMT)
commit2fe44f728afa2dd506c304641f0481d6813d1dbd (patch)
tree9d0bd27e55b44fb99d57657881a0abd110e2cab9 /Lib/venv
parentb863b9cd4b8681cf5fff5f121837a1039045783f (diff)
downloadcpython-2fe44f728afa2dd506c304641f0481d6813d1dbd.zip
cpython-2fe44f728afa2dd506c304641f0481d6813d1dbd.tar.gz
cpython-2fe44f728afa2dd506c304641f0481d6813d1dbd.tar.bz2
gh-98251: Allow venv to pass along PYTHON* variables to pip and ensurepip when they do not impact path resolution (GH-98259)
Diffstat (limited to 'Lib/venv')
-rw-r--r--Lib/venv/__init__.py28
1 files changed, 19 insertions, 9 deletions
diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py
index 034e3d4..5e695c7 100644
--- a/Lib/venv/__init__.py
+++ b/Lib/venv/__init__.py
@@ -339,14 +339,25 @@ class EnvBuilder:
shutil.copyfile(src, dst)
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):
"""Installs or upgrades pip in a virtual environment"""
- # We run ensurepip in isolated mode to avoid side effects from
- # environment vars, the current directory and anything else
- # intended for the global Python environment
- cmd = [context.env_exec_cmd, '-Im', 'ensurepip', '--upgrade',
- '--default-pip']
- subprocess.check_output(cmd, stderr=subprocess.STDOUT)
+ self._call_new_python(context, '-m', 'ensurepip', '--upgrade',
+ '--default-pip', stderr=subprocess.STDOUT)
def setup_scripts(self, context):
"""
@@ -445,9 +456,8 @@ class EnvBuilder:
logger.debug(
f'Upgrading {CORE_VENV_DEPS} packages in {context.bin_path}'
)
- cmd = [context.env_exec_cmd, '-m', 'pip', 'install', '--upgrade']
- cmd.extend(CORE_VENV_DEPS)
- subprocess.check_call(cmd)
+ self._call_new_python(context, '-m', 'pip', 'install', '--upgrade',
+ *CORE_VENV_DEPS)
def create(env_dir, system_site_packages=False, clear=False,