Skip to content

Commit 6f9ccfc

Browse files
committed
Fix link hashes
1 parent d67cac3 commit 6f9ccfc

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

src/pip/_internal/index/collector.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from pip._vendor.requests.exceptions import RetryError, SSLError
3535

3636
from pip._internal.exceptions import NetworkConnectionError
37-
from pip._internal.models.link import SUPPORTED_HASHES, Link
37+
from pip._internal.models.link import Link
3838
from pip._internal.models.search_scope import SearchScope
3939
from pip._internal.network.session import PipSession
4040
from pip._internal.network.utils import raise_for_status
@@ -274,11 +274,6 @@ def _clean_link(url: str) -> str:
274274
return urllib.parse.urlunparse(result._replace(path=path))
275275

276276

277-
_HASH_RE = re.compile(
278-
r"({choices})=([a-f0-9]+)".format(choices="|".join(SUPPORTED_HASHES))
279-
)
280-
281-
282277
def _create_link_from_element(
283278
element_attribs: Dict[str, Optional[str]],
284279
page_url: str,
@@ -295,17 +290,11 @@ def _create_link_from_element(
295290
pyrequire = element_attribs.get("data-requires-python")
296291
yanked_reason = element_attribs.get("data-yanked")
297292

298-
hashes = {}
299-
hm = _HASH_RE.search(url)
300-
if hm is not None:
301-
hashes[hm.group(1).lower()] = hm.group(2)
302-
303293
link = Link(
304294
url,
305295
comes_from=page_url,
306296
requires_python=pyrequire,
307297
yanked_reason=yanked_reason,
308-
hashes=hashes,
309298
)
310299

311300
return link

src/pip/_internal/models/link.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
# Order matters, earlier hashes have a precedence over later hashes for what
3535
# we will pick to use.
36-
SUPPORTED_HASHES = ("sha512", "sha384", "sha256", "sha224", "sha1", "md5")
36+
_SUPPORTED_HASHES = ("sha512", "sha384", "sha256", "sha224", "sha1", "md5")
3737

3838

3939
class Link(KeyBasedCompareMixin):
@@ -179,18 +179,32 @@ def subdirectory_fragment(self) -> Optional[str]:
179179
return None
180180
return match.group(1)
181181

182+
_hash_re = re.compile(
183+
r"({choices})=([a-f0-9]+)".format(choices="|".join(_SUPPORTED_HASHES))
184+
)
185+
182186
@property
183187
def hash(self) -> Optional[str]:
184-
for hashname in SUPPORTED_HASHES:
188+
for hashname in _SUPPORTED_HASHES:
185189
if hashname in self._hashes:
186190
return self._hashes[hashname]
191+
192+
match = self._hash_re.search(self._url)
193+
if match:
194+
return match.group(2)
195+
187196
return None
188197

189198
@property
190199
def hash_name(self) -> Optional[str]:
191-
for hashname in SUPPORTED_HASHES:
200+
for hashname in _SUPPORTED_HASHES:
192201
if hashname in self._hashes:
193202
return hashname
203+
204+
match = self._hash_re.search(self._url)
205+
if match:
206+
return match.group(1)
207+
194208
return None
195209

196210
@property
@@ -284,7 +298,7 @@ def _clean_link(link: Link) -> _CleanResult:
284298
subdirectory = ""
285299
# If there are multiple hash values under the same algorithm, use the
286300
# first one. This matches the behavior of Link.hash_value.
287-
hashes = {k: fragment[k][0] for k in SUPPORTED_HASHES if k in fragment}
301+
hashes = {k: fragment[k][0] for k in _SUPPORTED_HASHES if k in fragment}
288302
return _CleanResult(
289303
parsed=parsed._replace(netloc=netloc, query="", fragment=""),
290304
query=urllib.parse.parse_qs(parsed.query),

tests/unit/test_collector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def test_get_simple_response_no_head(
201201
"Cache-Control": "max-age=0",
202202
},
203203
),
204-
mock.call().headers.get("Content-Type", ""),
204+
mock.call().headers.get("Content-Type", "Unknown"),
205205
]
206206
mock_raise_for_status.assert_called_once_with(resp)
207207

@@ -667,7 +667,7 @@ def test_get_index_content_invalid_content_type(
667667
assert (
668668
"pip._internal.index.collector",
669669
logging.WARNING,
670-
"Skipping page {} because the GET request got Content-Type: {}."
670+
"Skipping page {} because the GET request got Content-Type: {}. "
671671
"The only supported Content-Types are application/vnd.pypi.simple.v1+json, "
672672
"application/vnd.pypi.simple.v1+html, and text/html'".format(url, content_type),
673673
) in caplog.record_tuples

0 commit comments

Comments
 (0)