Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/ensurepip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ def _bootstrap(*, root=None, upgrade=False, user=False,
Note that calling this function will alter both sys.path and os.environ.
"""

try:
import zlib
except ImportError:
raise ModuleNotFoundError(
"ensurepip requires the standard library module 'zlib' "
"to install pip."
) from None

if altinstall and default_pip:
raise ValueError("Cannot use altinstall and default_pip together")

Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_ensurepip.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ def setUp(self):
self.run_pip.return_value = 0
self.addCleanup(run_pip_patch.stop)

# Allow testing on zlib-less platforms by avoiding the check for zlib in _bootstrap()
zlib_patch = unittest.mock.patch.dict('sys.modules', {'zlib': unittest.mock.MagicMock()})
zlib_patch.start()
self.addCleanup(zlib_patch.stop)

# Avoid side effects on the actual os module
real_devnull = os.devnull
os_patch = unittest.mock.patch("ensurepip.os")
Expand Down Expand Up @@ -185,6 +190,16 @@ def test_pip_config_file_disabled(self):
ensurepip.bootstrap()
self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)

def test_missing_zlib(self):
with unittest.mock.patch.dict('sys.modules', {'zlib': None}):
with self.assertRaises(ModuleNotFoundError) as cm:
ensurepip.bootstrap()

error_msg = str(cm.exception)
self.assertIn("ensurepip requires the standard library module 'zlib'", error_msg)

self.assertFalse(self.run_pip.called)

@contextlib.contextmanager
def fake_pip(version=ensurepip.version()):
if version is None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`ensurepip` now fails with a nicer error message when the :mod:`zlib`
module is not available.
Loading