Skip to content

Commit e671005

Browse files
authored
Merge pull request #5312 from pradyunsg/fix/windows-pip-blocker-message
Move Windows protection check to specific commands
2 parents b8eb704 + 2a061c2 commit e671005

File tree

7 files changed

+51
-24
lines changed

7 files changed

+51
-24
lines changed

news/5311.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix "pip wheel pip" being blocked by the "don't use pip to modify itself" check

news/5312.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix "pip wheel pip" being blocked by the "don't use pip to modify itself" check

src/pip/_internal/basecommand.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from pip._internal.baseparser import (
1212
ConfigOptionParser, UpdatingDefaultsHelpFormatter,
1313
)
14-
from pip._internal.compat import WINDOWS
1514
from pip._internal.download import PipSession
1615
from pip._internal.exceptions import (
1716
BadCommand, CommandError, InstallationError, PreviousBuildDirError,
@@ -321,23 +320,6 @@ def populate_requirement_set(requirement_set, args, options, finder,
321320
'You must give at least one requirement to %(name)s '
322321
'(see "pip help %(name)s")' % opts)
323322

324-
# On Windows, any operation modifying pip should be run as:
325-
# python -m pip ...
326-
# See https://github.com/pypa/pip/issues/1299 for more discussion
327-
should_show_use_python_msg = (
328-
WINDOWS and
329-
requirement_set.has_requirement("pip") and
330-
os.path.basename(sys.argv[0]).startswith("pip")
331-
)
332-
if should_show_use_python_msg:
333-
new_command = [
334-
sys.executable, "-m", "pip"
335-
] + sys.argv[1:]
336-
raise CommandError(
337-
'To modify pip, please run the following command:\n{}'
338-
.format(" ".join(new_command))
339-
)
340-
341323
def _build_package_finder(self, options, session,
342324
platform=None, python_versions=None,
343325
abi=None, implementation=None):

src/pip/_internal/commands/install.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
from pip._internal.resolve import Resolver
2121
from pip._internal.status_codes import ERROR
2222
from pip._internal.utils.filesystem import check_path_owner
23-
from pip._internal.utils.misc import ensure_dir, get_installed_version
23+
from pip._internal.utils.misc import (
24+
ensure_dir, get_installed_version,
25+
protect_pip_from_modification_on_windows,
26+
)
2427
from pip._internal.utils.temp_dir import TempDirectory
2528
from pip._internal.wheel import WheelBuilder
2629

@@ -291,6 +294,10 @@ def run(self, options, args):
291294
)
292295
resolver.resolve(requirement_set)
293296

297+
protect_pip_from_modification_on_windows(
298+
modifying_pip=requirement_set.has_requirement("pip")
299+
)
300+
294301
# If caching is disabled or wheel is not installed don't
295302
# try to build wheels.
296303
if wheel and options.cache_dir:

src/pip/_internal/commands/uninstall.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pip._internal.basecommand import Command
66
from pip._internal.exceptions import InstallationError
77
from pip._internal.req import InstallRequirement, parse_requirements
8+
from pip._internal.utils.misc import protect_pip_from_modification_on_windows
89

910

1011
class UninstallCommand(Command):
@@ -63,6 +64,11 @@ def run(self, options, args):
6364
'You must give at least one requirement to %(name)s (see '
6465
'"pip help %(name)s")' % dict(name=self.name)
6566
)
67+
68+
protect_pip_from_modification_on_windows(
69+
modifying_pip="pip" in reqs_to_uninstall
70+
)
71+
6672
for req in reqs_to_uninstall.values():
6773
uninstall_pathset = req.uninstall(
6874
auto_confirm=options.yes, verbose=self.verbosity > 0,

src/pip/_internal/utils/misc.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
from pip._vendor.six.moves import input
2727
from pip._vendor.six.moves.urllib import parse as urllib_parse
2828

29-
from pip._internal.compat import console_to_str, expanduser, stdlib_pkgs
30-
from pip._internal.exceptions import InstallationError
29+
from pip._internal.compat import (
30+
WINDOWS, console_to_str, expanduser, stdlib_pkgs,
31+
)
32+
from pip._internal.exceptions import CommandError, InstallationError
3133
from pip._internal.locations import (
3234
running_under_virtualenv, site_packages, user_site, virtualenv_no_global,
3335
write_delete_marker_file,
@@ -868,3 +870,32 @@ def remove_auth_from_url(url):
868870
)
869871
surl = urllib_parse.urlunsplit(url_pieces)
870872
return surl
873+
874+
875+
def protect_pip_from_modification_on_windows(modifying_pip):
876+
"""Protection of pip.exe from modification on Windows
877+
878+
On Windows, any operation modifying pip should be run as:
879+
python -m pip ...
880+
"""
881+
pip_names = [
882+
"pip.exe",
883+
"pip{}.exe".format(sys.version_info[0]),
884+
"pip{}.{}.exe".format(*sys.version_info[:2])
885+
]
886+
887+
# See https://github.com/pypa/pip/issues/1299 for more discussion
888+
should_show_use_python_msg = (
889+
modifying_pip and
890+
WINDOWS and
891+
os.path.basename(sys.argv[0]) in pip_names
892+
)
893+
894+
if should_show_use_python_msg:
895+
new_command = [
896+
sys.executable, "-m", "pip"
897+
] + sys.argv[1:]
898+
raise CommandError(
899+
'To modify pip, please run the following command:\n{}'
900+
.format(" ".join(new_command))
901+
)

tests/functional/test_install.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ def test_pep518_uses_build_env(script, data, common_wheels, command, variant):
4040
def test_pep518_with_user_pip(script, virtualenv, pip_src,
4141
data, common_wheels):
4242
virtualenv.system_site_packages = True
43-
script.pip_install_local("--ignore-installed",
44-
"-f", common_wheels,
45-
"--user", pip_src)
43+
script.pip("install", "--ignore-installed", "--user", pip_src,
44+
use_module=True)
4645
system_pip_dir = script.site_packages_path / 'pip'
4746
system_pip_dir.rmtree()
4847
system_pip_dir.mkdir()

0 commit comments

Comments
 (0)