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

Skip to content

Commit 6edd82a

Browse files
committed
Close #20053: ignore default pip config settings
ensurepip now sets PIP_CONFIG_FILE to os.devnull before import pip from the wheel file. This also ensures venv ignores the default settings when bootstrapping pip.
1 parent a9b1524 commit 6edd82a

4 files changed

Lines changed: 53 additions & 12 deletions

File tree

Lib/ensurepip/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ def version():
4747
"""
4848
return _PIP_VERSION
4949

50-
def _clear_pip_environment_variables():
50+
def _disable_pip_configuration_settings():
5151
# We deliberately ignore all pip environment variables
5252
# when invoking pip
5353
# See http://bugs.python.org/issue19734 for details
5454
keys_to_remove = [k for k in os.environ if k.startswith("PIP_")]
5555
for k in keys_to_remove:
5656
del os.environ[k]
57+
# We also ignore the settings in the default pip configuration file
58+
# See http://bugs.python.org/issue20053 for details
59+
os.environ['PIP_CONFIG_FILE'] = os.devnull
5760

5861

5962
def bootstrap(*, root=None, upgrade=False, user=False,
@@ -69,7 +72,7 @@ def bootstrap(*, root=None, upgrade=False, user=False,
6972
raise ValueError("Cannot use altinstall and default_pip together")
7073

7174
_require_ssl_for_pip()
72-
_clear_pip_environment_variables()
75+
_disable_pip_configuration_settings()
7376

7477
# By default, installing pip and setuptools installs all of the
7578
# following scripts (X.Y == running Python version):
@@ -130,7 +133,7 @@ def _uninstall_helper(*, verbosity=0):
130133
raise RuntimeError(msg.format(pip.__version__, _PIP_VERSION))
131134

132135
_require_ssl_for_pip()
133-
_clear_pip_environment_variables()
136+
_disable_pip_configuration_settings()
134137

135138
# Construct the arguments to be passed to the pip command
136139
args = ["uninstall", "-y"]

Lib/test/test_ensurepip.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ def setUp(self):
3636
self.addCleanup(run_pip_patch.stop)
3737

3838
# Avoid side effects on the actual os module
39+
real_devnull = os.devnull
3940
os_patch = unittest.mock.patch("ensurepip.os")
4041
patched_os = os_patch.start()
4142
self.addCleanup(os_patch.stop)
43+
patched_os.devnull = real_devnull
4244
patched_os.path = os.path
4345
self.os_environ = patched_os.environ = os.environ.copy()
4446

@@ -161,6 +163,12 @@ def test_pip_environment_variables_removed(self):
161163
ensurepip.bootstrap()
162164
self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
163165

166+
@requires_usable_pip
167+
def test_pip_config_file_disabled(self):
168+
# ensurepip deliberately ignores the pip config file
169+
# See http://bugs.python.org/issue20053 for details
170+
ensurepip.bootstrap()
171+
self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
164172

165173
@contextlib.contextmanager
166174
def fake_pip(version=ensurepip._PIP_VERSION):
@@ -240,6 +248,14 @@ def test_pip_environment_variables_removed(self):
240248
ensurepip._uninstall_helper()
241249
self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
242250

251+
@requires_usable_pip
252+
def test_pip_config_file_disabled(self):
253+
# ensurepip deliberately ignores the pip config file
254+
# See http://bugs.python.org/issue20053 for details
255+
with fake_pip():
256+
ensurepip._uninstall_helper()
257+
self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
258+
243259

244260
class TestMissingSSL(EnsurepipMixin, unittest.TestCase):
245261

Lib/test/test_venv.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,16 +301,35 @@ def test_with_pip(self):
301301
# that we want to ensure it ignores the normal pip environment
302302
# variable settings. We set PIP_NO_INSTALL here specifically
303303
# to check that ensurepip (and hence venv) ignores it.
304-
# See http://bugs.python.org/issue19734 for details
304+
# See http://bugs.python.org/issue19734
305305
envvars["PIP_NO_INSTALL"] = "1"
306-
try:
307-
self.run_with_capture(venv.create, self.env_dir, with_pip=True)
308-
except subprocess.CalledProcessError as exc:
309-
# The output this produces can be a little hard to read, but
310-
# least it has all the details
311-
details = exc.output.decode(errors="replace")
312-
msg = "{}\n\n**Subprocess Output**\n{}".format(exc, details)
313-
self.fail(msg)
306+
# Also check that we ignore the pip configuration file
307+
# See http://bugs.python.org/issue20053
308+
with tempfile.TemporaryDirectory() as home_dir:
309+
envvars["HOME"] = home_dir
310+
bad_config = "[global]\nno-install=1"
311+
# Write to both config file names on all platforms to reduce
312+
# cross-platform variation in test code behaviour
313+
win_location = ("pip", "pip.ini")
314+
posix_location = (".pip", "pip.conf")
315+
for dirname, fname in (win_location, posix_location):
316+
dirpath = os.path.join(home_dir, dirname)
317+
os.mkdir(dirpath)
318+
fpath = os.path.join(dirpath, fname)
319+
with open(fpath, 'w') as f:
320+
f.write(bad_config)
321+
322+
# Actually run the create command with all that unhelpful
323+
# config in place to ensure we ignore it
324+
try:
325+
self.run_with_capture(venv.create, self.env_dir,
326+
with_pip=True)
327+
except subprocess.CalledProcessError as exc:
328+
# The output this produces can be a little hard to read,
329+
# but at least it has all the details
330+
details = exc.output.decode(errors="replace")
331+
msg = "{}\n\n**Subprocess Output**\n{}"
332+
self.fail(msg.format(exc, details))
314333
# Ensure pip is available in the virtual environment
315334
envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
316335
cmd = [envpy, '-Im', 'pip', '--version']

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- Issue #20053: ensurepip (and hence venv) are no longer affected by the
28+
settings in the default pip configuration file.
29+
2730
- Issue #20426: When passing the re.DEBUG flag, re.compile() displays the
2831
debug output every time it is called, regardless of the compilation cache.
2932

0 commit comments

Comments
 (0)