Skip to content

Commit 73cc1fd

Browse files
kimimgovstinner
andauthored
gh-146310: Fix ensurepip to treat empty WHEEL_PKG_DIR as unset (#146357)
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>
1 parent 1384f02 commit 73cc1fd

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Lib/ensurepip/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
# policies recommend against bundling dependencies. For example, Fedora
1717
# installs wheel packages in the /usr/share/python-wheels/ directory and don't
1818
# install the ensurepip._bundled package.
19-
if (_pkg_dir := sysconfig.get_config_var('WHEEL_PKG_DIR')) is not None:
19+
_pkg_dir = sysconfig.get_config_var('WHEEL_PKG_DIR')
20+
if _pkg_dir:
2021
_WHEEL_PKG_DIR = Path(_pkg_dir).resolve()
2122
else:
2223
_WHEEL_PKG_DIR = None

Lib/test/test_ensurepip.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import unittest
88
import unittest.mock
99
from pathlib import Path
10+
from test.support import import_helper
1011

1112
import ensurepip
1213
import ensurepip._uninstall
@@ -36,6 +37,15 @@ def test_version_no_dir(self):
3637
# when the bundled pip wheel is used, we get _PIP_VERSION
3738
self.assertEqual(ensurepip._PIP_VERSION, ensurepip.version())
3839

40+
def test_wheel_pkg_dir_none(self):
41+
# gh-146310: empty or None WHEEL_PKG_DIR should not search CWD
42+
for value in ('', None):
43+
with unittest.mock.patch('sysconfig.get_config_var',
44+
return_value=value) as get_config_var:
45+
module = import_helper.import_fresh_module('ensurepip')
46+
self.assertIsNone(module._WHEEL_PKG_DIR)
47+
get_config_var.assert_called_once_with('WHEEL_PKG_DIR')
48+
3949
def test_selected_wheel_path_no_dir(self):
4050
pip_filename = f'pip-{ensurepip._PIP_VERSION}-py3-none-any.whl'
4151
with unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', None):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The :mod:`ensurepip` module no longer looks for ``pip-*.whl`` wheel packages
2+
in the current directory.

0 commit comments

Comments
 (0)