]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-139823: Check if `zlib` is available in `ensurepip` (GH-139954)
authorStan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Mon, 13 Oct 2025 14:01:06 +0000 (15:01 +0100)
committerGitHub <noreply@github.com>
Mon, 13 Oct 2025 14:01:06 +0000 (16:01 +0200)
Lib/ensurepip/__init__.py
Lib/test/test_ensurepip.py
Misc/NEWS.d/next/Library/2025-10-11-14-37-42.gh-issue-139823.uGF4oh.rst [new file with mode: 0644]

index 4bd85990e8614a492f3488087d6e9e3d35802890..0552cf55db15f734d2e77e848c21db896e3e3dfd 100644 (file)
@@ -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")
 
index 6d3c91b0b6d9f985e4567f592558f85bb68e2a2c..f6743d57ca28ddb0c44adeb9fb7fd699a3018aee 100644 (file)
@@ -60,6 +60,11 @@ class EnsurepipMixin:
         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")
@@ -185,6 +190,16 @@ class TestBootstrap(EnsurepipMixin, unittest.TestCase):
         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:
diff --git a/Misc/NEWS.d/next/Library/2025-10-11-14-37-42.gh-issue-139823.uGF4oh.rst b/Misc/NEWS.d/next/Library/2025-10-11-14-37-42.gh-issue-139823.uGF4oh.rst
new file mode 100644 (file)
index 0000000..43fe05d
--- /dev/null
@@ -0,0 +1,2 @@
+:mod:`ensurepip` now fails with a nicer error message when the :mod:`zlib`
+module is not available.