Skip to content

Commit ca5ccee

Browse files
committed
gh-146310: Fix ensurepip to treat empty WHEEL_PKG_DIR as unset
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.
1 parent 95340ef commit ca5ccee

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

Lib/ensurepip/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
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+
if (_pkg_dir := sysconfig.get_config_var('WHEEL_PKG_DIR')) is not None and _pkg_dir:
2020
_WHEEL_PKG_DIR = Path(_pkg_dir).resolve()
2121
else:
2222
_WHEEL_PKG_DIR = None

Lib/test/test_ensurepip.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ def test_version_no_dir(self):
3636
# when the bundled pip wheel is used, we get _PIP_VERSION
3737
self.assertEqual(ensurepip._PIP_VERSION, ensurepip.version())
3838

39+
def test_empty_wheel_pkg_dir_treated_as_none(self):
40+
# GH#146310: empty string WHEEL_PKG_DIR should not search CWD.
41+
# An empty WHEEL_PKG_DIR converts to Path('.') which would
42+
# incorrectly search the current working directory.
43+
with unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', None):
44+
self.assertIsNone(ensurepip._find_wheel_pkg_dir_pip())
45+
3946
def test_selected_wheel_path_no_dir(self):
4047
pip_filename = f'pip-{ensurepip._PIP_VERSION}-py3-none-any.whl'
4148
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+
Fix :mod:`ensurepip` to treat an empty string ``WHEEL_PKG_DIR`` as unset,
2+
preventing it from searching the current working directory for wheel files.

0 commit comments

Comments
 (0)