]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-146310: Fix ensurepip to treat empty WHEEL_PKG_DIR as unset (#146357)
authorImgyu Kim <kimimgo@gmail.com>
Fri, 27 Mar 2026 20:48:07 +0000 (05:48 +0900)
committerGitHub <noreply@github.com>
Fri, 27 Mar 2026 20:48:07 +0000 (20:48 +0000)
Path('') resolves to CWD, so an empty WHEEL_PKG_DIR string caused
ensurepip to search the current working directory for wheel files.
Add a truthiness check to treat empty strings the same as None.

Co-authored-by: Victor Stinner <vstinner@python.org>
Lib/ensurepip/__init__.py
Lib/test/test_ensurepip.py
Misc/NEWS.d/next/Library/2026-03-24-03-49-50.gh-issue-146310.WhlDir.rst [new file with mode: 0644]

index 6164ea62324cced9e6a20036e0824aee1d154748..93b4e7a820f3ad2b539fdb3043b8d6fdefd5ab91 100644 (file)
@@ -16,7 +16,8 @@ _PIP_VERSION = "26.0.1"
 # policies recommend against bundling dependencies. For example, Fedora
 # installs wheel packages in the /usr/share/python-wheels/ directory and don't
 # install the ensurepip._bundled package.
-if (_pkg_dir := sysconfig.get_config_var('WHEEL_PKG_DIR')) is not None:
+_pkg_dir = sysconfig.get_config_var('WHEEL_PKG_DIR')
+if _pkg_dir:
     _WHEEL_PKG_DIR = Path(_pkg_dir).resolve()
 else:
     _WHEEL_PKG_DIR = None
index c62b340f6a340f1f388323fbb0ea2cf533b13b69..20a56ed715d8abd6da16a092a1faca95f0d53376 100644 (file)
@@ -7,6 +7,7 @@ import test.support
 import unittest
 import unittest.mock
 from pathlib import Path
+from test.support import import_helper
 
 import ensurepip
 import ensurepip._uninstall
@@ -36,6 +37,15 @@ class TestPackages(unittest.TestCase):
             # when the bundled pip wheel is used, we get _PIP_VERSION
             self.assertEqual(ensurepip._PIP_VERSION, ensurepip.version())
 
+    def test_wheel_pkg_dir_none(self):
+        # gh-146310: empty or None WHEEL_PKG_DIR should not search CWD
+        for value in ('', None):
+            with unittest.mock.patch('sysconfig.get_config_var',
+                                     return_value=value) as get_config_var:
+                module = import_helper.import_fresh_module('ensurepip')
+                self.assertIsNone(module._WHEEL_PKG_DIR)
+                get_config_var.assert_called_once_with('WHEEL_PKG_DIR')
+
     def test_selected_wheel_path_no_dir(self):
         pip_filename = f'pip-{ensurepip._PIP_VERSION}-py3-none-any.whl'
         with unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', None):
diff --git a/Misc/NEWS.d/next/Library/2026-03-24-03-49-50.gh-issue-146310.WhlDir.rst b/Misc/NEWS.d/next/Library/2026-03-24-03-49-50.gh-issue-146310.WhlDir.rst
new file mode 100644 (file)
index 0000000..b712595
--- /dev/null
@@ -0,0 +1,2 @@
+The :mod:`ensurepip` module no longer looks for ``pip-*.whl`` wheel packages
+in the current directory.