Skip to content

Commit 617afda

Browse files
Commit
1 parent 897a36b commit 617afda

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Lib/ensurepip/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ def _bootstrap(*, root=None, upgrade=False, user=False,
130130
131131
Note that calling this function will alter both sys.path and os.environ.
132132
"""
133+
134+
try:
135+
import zlib
136+
except ImportError:
137+
raise ModuleNotFoundError(
138+
"ensurepip requires the standard library module 'zlib' "
139+
"to install pip."
140+
) from None
141+
133142
if altinstall and default_pip:
134143
raise ValueError("Cannot use altinstall and default_pip together")
135144

Lib/test/test_ensurepip.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ def test_pip_config_file_disabled(self):
185185
ensurepip.bootstrap()
186186
self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
187187

188+
def test_missing_zlib(self):
189+
with unittest.mock.patch.dict('sys.modules', {'zlib': None}):
190+
with self.assertRaises(ModuleNotFoundError) as cm:
191+
ensurepip.bootstrap()
192+
193+
error_msg = str(cm.exception)
194+
self.assertIn("ensurepip requires the standard library module 'zlib'", error_msg)
195+
196+
self.assertFalse(self.run_pip.called)
197+
188198
@contextlib.contextmanager
189199
def fake_pip(version=ensurepip.version()):
190200
if version is None:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`ensurepip` now fails with a nicer error message when the :mod:`zlib`
2+
module is not available.

0 commit comments

Comments
 (0)