From 617afdac1162a1552f4c5f4e974f91e3a3cbfe91 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Sat, 11 Oct 2025 14:39:18 +0100 Subject: [PATCH 1/3] Commit --- Lib/ensurepip/__init__.py | 9 +++++++++ Lib/test/test_ensurepip.py | 10 ++++++++++ .../2025-10-11-14-37-42.gh-issue-139823.uGF4oh.rst | 2 ++ 3 files changed, 21 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-10-11-14-37-42.gh-issue-139823.uGF4oh.rst diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py index 4bd85990e8614a4..0552cf55db15f73 100644 --- a/Lib/ensurepip/__init__.py +++ b/Lib/ensurepip/__init__.py @@ -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") diff --git a/Lib/test/test_ensurepip.py b/Lib/test/test_ensurepip.py index 6d3c91b0b6d9f98..4acab3cee82d2fc 100644 --- a/Lib/test/test_ensurepip.py +++ b/Lib/test/test_ensurepip.py @@ -185,6 +185,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: 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 index 000000000000000..43fe05d569964d7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-11-14-37-42.gh-issue-139823.uGF4oh.rst @@ -0,0 +1,2 @@ +:mod:`ensurepip` now fails with a nicer error message when the :mod:`zlib` +module is not available. From 2d7af940ad58b40d9b50a4ba38a52487ad28ccfe Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Sat, 11 Oct 2025 15:07:45 +0100 Subject: [PATCH 2/3] Fix tests when zlib is missing --- Lib/test/test_ensurepip.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/test/test_ensurepip.py b/Lib/test/test_ensurepip.py index 4acab3cee82d2fc..f5fbb464e78a89a 100644 --- a/Lib/test/test_ensurepip.py +++ b/Lib/test/test_ensurepip.py @@ -60,6 +60,11 @@ def setUp(self): self.run_pip.return_value = 0 self.addCleanup(run_pip_patch.stop) + # Ensure zlib is available for tests (unless specifically testing missing zlib) + 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") From da4d0b8cf35797083d17a9efb6088bfb6de1eac8 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Date: Sun, 12 Oct 2025 18:39:37 +0100 Subject: [PATCH 3/3] Make comment clearer --- Lib/test/test_ensurepip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_ensurepip.py b/Lib/test/test_ensurepip.py index f5fbb464e78a89a..f6743d57ca28ddb 100644 --- a/Lib/test/test_ensurepip.py +++ b/Lib/test/test_ensurepip.py @@ -60,7 +60,7 @@ def setUp(self): self.run_pip.return_value = 0 self.addCleanup(run_pip_patch.stop) - # Ensure zlib is available for tests (unless specifically testing missing zlib) + # 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)