Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d76cdc1

Browse files
committed
Close #19694: venv now runs ensurepip in isolated mode
1 parent fd66cc5 commit d76cdc1

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

Lib/test/test_venv.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import sys
1313
import tempfile
1414
from test.support import (captured_stdout, captured_stderr, run_unittest,
15-
can_symlink)
15+
can_symlink, EnvironmentVarGuard)
1616
import unittest
1717
import venv
1818

@@ -280,7 +280,12 @@ def test_explicit_no_pip(self):
280280

281281
def test_with_pip(self):
282282
shutil.rmtree(self.env_dir)
283-
self.run_with_capture(venv.create, self.env_dir, with_pip=True)
283+
with EnvironmentVarGuard() as envvars:
284+
# pip's cross-version compatibility may trigger deprecation
285+
# warnings in current versions of Python. Ensure related
286+
# environment settings don't cause venv to fail.
287+
envvars["PYTHONWARNINGS"] = "e"
288+
self.run_with_capture(venv.create, self.env_dir, with_pip=True)
284289
envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
285290
cmd = [envpy, '-m', 'pip', '--version']
286291
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,

Lib/venv/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,11 @@ def setup_python(self, context):
234234

235235
def _setup_pip(self, context):
236236
"""Installs or upgrades pip in a virtual environment"""
237-
cmd = [context.env_exe, '-m', 'ensurepip', '--upgrade',
238-
'--default-pip']
237+
# We run ensurepip in isolated mode to avoid side effects from
238+
# environment vars, the current directory and anything else
239+
# intended for the global Python environment
240+
cmd = [context.env_exe, '-Im', 'ensurepip', '--upgrade',
241+
'--default-pip']
239242
subprocess.check_output(cmd)
240243

241244
def setup_scripts(self, context):

0 commit comments

Comments
 (0)