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

Skip to content

Commit ed9af52

Browse files
committed
Issue #19734: ignore pip env vars in ensurepip._uninstall
1 parent 6256fcb commit ed9af52

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

Lib/ensurepip/__init__.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ def version():
3636
"""
3737
return _PIP_VERSION
3838

39+
def _clear_pip_environment_variables():
40+
# We deliberately ignore all pip environment variables
41+
# when invoking pip
42+
# See http://bugs.python.org/issue19734 for details
43+
keys_to_remove = [k for k in os.environ if k.startswith("PIP_")]
44+
for k in keys_to_remove:
45+
del os.environ[k]
46+
3947

4048
def bootstrap(*, root=None, upgrade=False, user=False,
4149
altinstall=False, default_pip=False,
@@ -49,11 +57,7 @@ def bootstrap(*, root=None, upgrade=False, user=False,
4957
if altinstall and default_pip:
5058
raise ValueError("Cannot use altinstall and default_pip together")
5159

52-
# We deliberately ignore all pip environment variables
53-
# See http://bugs.python.org/issue19734 for details
54-
keys_to_remove = [k for k in os.environ if k.startswith("PIP_")]
55-
for k in keys_to_remove:
56-
del os.environ[k]
60+
_clear_pip_environment_variables()
5761

5862
# By default, installing pip and setuptools installs all of the
5963
# following scripts (X.Y == running Python version):
@@ -101,7 +105,10 @@ def bootstrap(*, root=None, upgrade=False, user=False,
101105
_run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
102106

103107
def _uninstall(*, verbosity=0):
104-
"""Helper to support a clean default uninstall process on Windows"""
108+
"""Helper to support a clean default uninstall process on Windows
109+
110+
Note that calling this function may alter os.environ.
111+
"""
105112
# Nothing to do if pip was never installed, or has been removed
106113
try:
107114
import pip
@@ -114,6 +121,8 @@ def _uninstall(*, verbosity=0):
114121
"({!r} installed, {!r} bundled)")
115122
raise RuntimeError(msg.format(pip.__version__, _PIP_VERSION))
116123

124+
_clear_pip_environment_variables()
125+
117126
# Construct the arguments to be passed to the pip command
118127
args = ["uninstall", "-y"]
119128
if verbosity:

Lib/test/test_ensurepip.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ def setUp(self):
160160
self.run_pip = run_pip_patch.start()
161161
self.addCleanup(run_pip_patch.stop)
162162

163+
# Avoid side effects on the actual os module
164+
os_patch = unittest.mock.patch("ensurepip.os")
165+
patched_os = os_patch.start()
166+
self.addCleanup(os_patch.stop)
167+
patched_os.path = os.path
168+
self.os_environ = patched_os.environ = os.environ.copy()
169+
163170
def test_uninstall_skipped_when_not_installed(self):
164171
with fake_pip(None):
165172
ensurepip._uninstall()
@@ -204,6 +211,13 @@ def test_uninstall_with_verbosity_3(self):
204211
["uninstall", "-y", "-vvv", "pip", "setuptools"]
205212
)
206213

214+
def test_pip_environment_variables_removed(self):
215+
# ensurepip deliberately ignores all pip environment variables
216+
# See http://bugs.python.org/issue19734 for details
217+
self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder"
218+
with fake_pip():
219+
ensurepip._uninstall()
220+
self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
207221

208222

209223

0 commit comments

Comments
 (0)