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")
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")
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: