https://github.com/python/cpython/commit/1a82568568a302d372690b07c7d9c45719e89bd4
commit: 1a82568568a302d372690b07c7d9c45719e89bd4
branch: main
author: Stan Ulbrych <[email protected]>
committer: encukou <[email protected]>
date: 2025-10-13T16:01:06+02:00
summary:
gh-139823: Check if `zlib` is available in `ensurepip` (GH-139954)
files:
A Misc/NEWS.d/next/Library/2025-10-11-14-37-42.gh-issue-139823.uGF4oh.rst
M Lib/ensurepip/__init__.py
M Lib/test/test_ensurepip.py
diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py
index 4bd85990e8614a..0552cf55db15f7 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 6d3c91b0b6d9f9..f6743d57ca28dd 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)
+ # 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 @@ 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 00000000000000..43fe05d569964d
--- /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.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]