Merge pull request #9078 from xavfernandez/cache_find_best_candidate

This commit is contained in:
Pradyun Gedam 2020-10-31 23:09:15 +05:30 committed by GitHub
commit 2e11f312d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View File

@ -863,6 +863,7 @@ class PackageFinder(object):
hashes=hashes,
)
@lru_cache(maxsize=None)
def find_best_candidate(
self,
project_name, # type: str

View File

@ -39,7 +39,12 @@ class Hashes(object):
:param hashes: A dict of algorithm names pointing to lists of allowed
hex digests
"""
self._allowed = {} if hashes is None else hashes
allowed = {}
if hashes is not None:
for alg, keys in hashes.items():
# Make sure values are always sorted (to ease equality checks)
allowed[alg] = sorted(keys)
self._allowed = allowed
def __and__(self, other):
# type: (Hashes) -> Hashes
@ -128,6 +133,22 @@ class Hashes(object):
# type: () -> bool
return self.__nonzero__()
def __eq__(self, other):
# type: (object) -> bool
if not isinstance(other, Hashes):
return NotImplemented
return self._allowed == other._allowed
def __hash__(self):
# type: () -> int
return hash(
",".join(sorted(
":".join((alg, digest))
for alg, digest_list in self._allowed.items()
for digest in digest_list
))
)
class MissingHashes(Hashes):
"""A workalike for Hashes used when we're missing a hash for a requirement

View File

@ -541,6 +541,16 @@ class TestHashes(object):
assert not Hashes()
assert not Hashes({})
def test_equality(self):
assert Hashes() == Hashes()
assert Hashes({'sha256': ['abcd']}) == Hashes({'sha256': ['abcd']})
assert Hashes({'sha256': ['ab', 'cd']}) == Hashes({'sha256': ['cd', 'ab']})
def test_hash(self):
cache = {}
cache[Hashes({'sha256': ['ab', 'cd']})] = 42
assert cache[Hashes({'sha256': ['ab', 'cd']})] == 42
class TestEncoding(object):
"""Tests for pip._internal.utils.encoding"""