8000 Simplify WHEEL_PKG_DIR search · python/cpython@96b8b9e · GitHub
[go: up one dir, main page]

Skip to content

Commit 96b8b9e

Browse files
AA-Turnerwebknjaz
authored andcommitted
Simplify WHEEL_PKG_DIR search
- Exit early if there are no files in the directory - Return a match early by iterating in reverse order - Merge filename checks - Inline the path variable - Remove type annotations
1 parent 5c3bd26 commit 96b8b9e

File tree

2 files changed

+13
-27
lines changed

2 files changed

+13
-27
lines changed

Lib/ensurepip/__init__.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,29 @@
2525
_WHEEL_PKG_DIR = sysconfig.get_config_var('WHEEL_PKG_DIR')
2626

2727

28-
def _get_replacement_pip_package(path: str | None) -> _Package:
29-
if path is None:
30-
raise LookupError(
31-
'The compile-time `WHEEL_PKG_DIR` is unset so there is '
32-
8000 'no place for looking up the wheels.',
33-
)
34-
28+
def _find_wheel_pkg_dir_pip():
29+
if _WHEEL_PKG_DIR is None:
30+
return None
3531
try:
36-
filenames = os.listdir(path)
32+
filenames = os.listdir(_WHEEL_PKG_DIR)
3733
except OSError:
3834
# Ignore: path doesn't exist or permission error
39-
filenames = ()
35+
return None
4036
# Make the code deterministic if a directory contains multiple wheel files
4137
# of the same package, but don't attempt to implement correct version
4238
# comparison since this case should not happen.
43-
filenames = sorted(filenames)
44-
pip_pkg = None
39+
filenames = sorted(filenames, reverse=True)
4540
for filename in filenames:
4641
# filename is like 'pip-21.2.4-py3-none-any.whl'
47-
if not filename.endswith(".whl"):
48-
continue
49-
if not filename.startswith('pip-'):
42+
if not filename.startswith("pip-") or not filename.endswith(".whl"):
5043
continue
5144

5245
# Extract '21.2.4' from 'pip-21.2.4-py3-none-any.whl'
53-
discovered_pip_pkg_version = filename.removeprefix(
54-
'pip-',
55-
).partition('-')[0]
56-
wheel_path = os.path.join(path, filename)
57-
pip_pkg = _Package(discovered_pip_pkg_version, None, wheel_path)
46+
version = filename.removeprefix("pip-").partition("-")[0]
47+
wheel_path = os.path.join(_WHEEL_PKG_DIR, filename)
48+
return _Package(version, None, wheel_path)
5849

59-
if pip_pkg is None:
60-
raise LookupError(
61-
'`WHEEL_PKG_DIR` does not contain any wheel files for `pip`.',
62-
)
63-
64-
return pip_pkg
50+
return None
6551

6652

6753
@cache
@@ -71,7 +57,7 @@ def _get_usable_pip_package() -> _Package:
7157

7258
with suppress(LookupError):
7359
# only use the wheel package directory if pip wheel is found there
74-
pip_pkg = _get_replacement_pip_package(_WHEEL_PKG_DIR)
60+
pip_pkg = _find_wheel_pkg_dir_pip(_WHEEL_PKG_DIR)
7561

7662
return pip_pkg
7763

Lib/test/test_ensurepip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def setUp(self):
7878
real_devnull = os.devnull
7979
os_patch = unittest.mock.patch("ensurepip.os")
8080
patched_os = os_patch.start()
81-
# But expose os.listdir() used by _get_replacement_pip_package()
81+
# But expose os.listdir() used by _find_wheel_pkg_dir_pip()
8282
patched_os.listdir = os.listdir
8383
self.addCleanup(os_patch.stop)
8484
patched_os.devnull = real_devnull

0 commit comments

Comments
 (0)
0