Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.7] bpo-31351: Set return code in ensurepip when pip fails (GH-3626) #3734

Merged
merged 3 commits into from
Sep 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Doc/library/ensurepip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ options:
* ``--no-default-pip``: if a non-default installation is request, the ``pip``
script will *not* be installed.

.. versionchanged:: 2.7.15
The exit status is non-zero if the command fails.


Module API
----------
Expand Down
24 changes: 19 additions & 5 deletions Lib/ensurepip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _run_pip(args, additional_paths=None):

# Install the bundled software
import pip
pip.main(args)
return pip.main(args)


def version():
Expand Down Expand Up @@ -58,6 +58,21 @@ def bootstrap(root=None, upgrade=False, user=False,
Bootstrap pip into the current Python installation (or the given root
directory).

Note that calling this function will alter both sys.path and os.environ.
"""
# Discard the return value
_bootstrap(root=root, upgrade=upgrade, user=user,
altinstall=altinstall, default_pip=default_pip,
verbosity=verbosity)


def _bootstrap(root=None, upgrade=False, user=False,
altinstall=False, default_pip=True,
verbosity=0):
"""
Bootstrap pip into the current Python installation (or the given root
directory). Returns pip command status code.

Note that calling this function will alter both sys.path and os.environ.
"""
if altinstall and default_pip:
Expand Down Expand Up @@ -105,11 +120,10 @@ def bootstrap(root=None, upgrade=False, user=False,
if verbosity:
args += ["-" + "v" * verbosity]

_run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
finally:
shutil.rmtree(tmpdir, ignore_errors=True)


def _uninstall_helper(verbosity=0):
"""Helper to support a clean default uninstall process on Windows

Expand All @@ -135,7 +149,7 @@ def _uninstall_helper(verbosity=0):
if verbosity:
args += ["-" + "v" * verbosity]

_run_pip(args + [p[0] for p in reversed(_PROJECTS)])
return _run_pip(args + [p[0] for p in reversed(_PROJECTS)])


def _main(argv=None):
Expand Down Expand Up @@ -196,7 +210,7 @@ def _main(argv=None):

args = parser.parse_args(argv)

bootstrap(
return _bootstrap(
root=args.root,
upgrade=args.upgrade,
user=args.user,
Expand Down
3 changes: 2 additions & 1 deletion Lib/ensurepip/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ensurepip
import sys

if __name__ == "__main__":
ensurepip._main()
sys.exit(ensurepip._main())
5 changes: 3 additions & 2 deletions Lib/ensurepip/_uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import argparse
import ensurepip
import sys


def _main(argv=None):
Expand All @@ -23,8 +24,8 @@ def _main(argv=None):

args = parser.parse_args(argv)

ensurepip._uninstall_helper(verbosity=args.verbosity)
return ensurepip._uninstall_helper(verbosity=args.verbosity)


if __name__ == "__main__":
_main()
sys.exit(_main())
19 changes: 17 additions & 2 deletions Lib/test/test_ensurepip.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class EnsurepipMixin:
def setUp(self):
run_pip_patch = mock.patch("ensurepip._run_pip")
self.run_pip = run_pip_patch.start()
self.run_pip.return_value = 0
self.addCleanup(run_pip_patch.stop)

# Avoid side effects on the actual os module
Expand Down Expand Up @@ -258,7 +259,7 @@ def test_bootstrap_version(self):
self.assertFalse(self.run_pip.called)

def test_basic_bootstrapping(self):
ensurepip._main([])
exit_code = ensurepip._main([])

self.run_pip.assert_called_once_with(
[
Expand All @@ -270,6 +271,13 @@ def test_basic_bootstrapping(self):

additional_paths = self.run_pip.call_args[0][1]
self.assertEqual(len(additional_paths), 2)
self.assertEqual(exit_code, 0)

def test_bootstrapping_error_code(self):
self.run_pip.return_value = 2
exit_code = ensurepip._main([])
self.assertEqual(exit_code, 2)



class TestUninstallationMainFunction(EnsurepipMixin, unittest.TestCase):
Expand All @@ -284,7 +292,7 @@ def test_uninstall_version(self):

def test_basic_uninstall(self):
with fake_pip():
ensurepip._uninstall._main([])
exit_code = ensurepip._uninstall._main([])

self.run_pip.assert_called_once_with(
[
Expand All @@ -293,6 +301,13 @@ def test_basic_uninstall(self):
]
)

self.assertEqual(exit_code, 0)

def test_uninstall_error_code(self):
with fake_pip():
self.run_pip.return_value = 2
exit_code = ensurepip._uninstall._main([])
self.assertEqual(exit_code, 2)

if __name__ == "__main__":
test.test_support.run_unittest(__name__)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
python -m ensurepip now exits with non-zero exit code if pip bootstrapping
has failed.