Skip to content

Commit 36ff884

Browse files
committed
Fix WheelCache.get in presence of legacy cache keys
1 parent 80bfba3 commit 36ff884

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

news/7490.trivial

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix unrelease bug from #7319.

src/pip/_internal/cache.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,15 @@ def _get_candidates(self, link, canonical_package_name):
138138
candidates = []
139139
path = self.get_path_for_link(link)
140140
if os.path.isdir(path):
141-
candidates.extend(os.listdir(path))
141+
for candidate in os.listdir(path):
142+
candidates.append((candidate, os.path.join(path, candidate)))
142143
# TODO remove legacy path lookup in pip>=21
143144
legacy_path = self.get_path_for_link_legacy(link)
144145
if os.path.isdir(legacy_path):
145-
candidates.extend(os.listdir(legacy_path))
146+
for candidate in os.listdir(legacy_path):
147+
candidates.append(
148+
(candidate, os.path.join(legacy_path, candidate))
149+
)
146150
return candidates
147151

148152
def get_path_for_link_legacy(self, link):
@@ -167,13 +171,6 @@ def get(
167171
"""
168172
raise NotImplementedError()
169173

170-
def _link_for_candidate(self, link, candidate):
171-
# type: (Link, str) -> Link
172-
root = self.get_path_for_link(link)
173-
path = os.path.join(root, candidate)
174-
175-
return Link(path_to_url(path))
176-
177174
def cleanup(self):
178175
# type: () -> None
179176
pass
@@ -228,7 +225,9 @@ def get(
228225
return link
229226

230227
canonical_package_name = canonicalize_name(package_name)
231-
for wheel_name in self._get_candidates(link, canonical_package_name):
228+
for wheel_name, wheel_path in self._get_candidates(
229+
link, canonical_package_name
230+
):
232231
try:
233232
wheel = Wheel(wheel_name)
234233
except InvalidWheelFilename:
@@ -245,13 +244,17 @@ def get(
245244
# Built for a different python/arch/etc
246245
continue
247246
candidates.append(
248-
(wheel.support_index_min(supported_tags), wheel_name)
247+
(
248+
wheel.support_index_min(supported_tags),
249+
wheel_name,
250+
wheel_path,
251+
)
249252
)
250253

251254
if not candidates:
252255
return link
253256

254-
return self._link_for_candidate(link, min(candidates)[1])
257+
return Link(path_to_url(min(candidates)[2]))
255258

256259

257260
class EphemWheelCache(SimpleWheelCache):

tests/unit/test_cache.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,24 @@ def test_cache_hash():
5858
def test_get_path_for_link_legacy(tmpdir):
5959
"""
6060
Test that an existing cache entry that was created with the legacy hashing
61-
mechanism is used.
61+
mechanism is returned by WheelCache._get_candidates().
6262
"""
6363
wc = WheelCache(tmpdir, FormatControl())
6464
link = Link("https://g.c/o/r")
6565
path = wc.get_path_for_link(link)
6666
legacy_path = wc.get_path_for_link_legacy(link)
6767
assert path != legacy_path
6868
ensure_dir(path)
69-
with open(os.path.join(path, "test-pyz-none-any.whl"), "w"):
69+
with open(os.path.join(path, "test-1.0.0-pyz-none-any.whl"), "w"):
7070
pass
7171
ensure_dir(legacy_path)
72-
with open(os.path.join(legacy_path, "test-pyx-none-any.whl"), "w"):
72+
with open(os.path.join(legacy_path, "test-1.0.0-pyx-none-any.whl"), "w"):
7373
pass
74-
expected_candidates = {"test-pyx-none-any.whl", "test-pyz-none-any.whl"}
75-
assert set(wc._get_candidates(link, "test")) == expected_candidates
74+
expected_candidates = {
75+
"test-1.0.0-pyx-none-any.whl", "test-1.0.0-pyz-none-any.whl"
76+
}
77+
candidates = {c[0] for c in wc._get_candidates(link, "test")}
78+
assert candidates == expected_candidates
7679

7780

7881
def test_get_with_legacy_entry_only(tmpdir):

0 commit comments

Comments
 (0)