Skip to content

Commit 6ed8ebf

Browse files
committed
Address review: simplify condition, use import_fresh_module, update NEWS
Per @vstinner: - Simplify to 'if _pkg_dir:' instead of walrus + is not None - Replace mock-based test with import_fresh_module for both '' and None - Reword NEWS to describe old behavior instead of fix
1 parent ca5ccee commit 6ed8ebf

3 files changed

Lines changed: 13 additions & 9 deletions

File tree

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 and _pkg_dir:
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: 9 additions & 6 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,12 +37,14 @@ 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

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())
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')
4548

4649
def test_selected_wheel_path_no_dir(self):
4750
pip_filename = f'pip-{ensurepip._PIP_VERSION}-py3-none-any.whl'
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +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.
1+
The :mod:`ensurepip` module no longer looks for ``pip-*.whl`` wheel packages
2+
in the current directory.

0 commit comments

Comments
 (0)