File tree 3 files changed +33
-1
lines changed
3 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -863,6 +863,7 @@ def make_candidate_evaluator(
863
863
hashes = hashes ,
864
864
)
865
865
866
+ @lru_cache (maxsize = None )
866
867
def find_best_candidate (
867
868
self ,
868
869
project_name , # type: str
Original file line number Diff line number Diff line change @@ -39,7 +39,12 @@ def __init__(self, hashes=None):
39
39
:param hashes: A dict of algorithm names pointing to lists of allowed
40
40
hex digests
41
41
"""
42
- self ._allowed = {} if hashes is None else hashes
42
+ allowed = {}
43
+ if hashes is not None :
44
+ for alg , keys in hashes .items ():
45
+ # Make sure values are always sorted (to ease equality checks)
46
+ allowed [alg ] = sorted (keys )
47
+ self ._allowed = allowed
43
48
44
49
def __and__ (self , other ):
45
50
# type: (Hashes) -> Hashes
@@ -128,6 +133,22 @@ def __bool__(self):
128
133
# type: () -> bool
129
134
return self .__nonzero__ ()
130
135
136
+ def __eq__ (self , other ):
137
+ # type: (object) -> bool
138
+ if not isinstance (other , Hashes ):
139
+ return NotImplemented
140
+ return self ._allowed == other ._allowed
141
+
142
+ def __hash__ (self ):
143
+ # type: () -> int
144
+ return hash (
145
+ "," .join (sorted (
146
+ ":" .join ((alg , digest ))
147
+ for alg , digest_list in self ._allowed .items ()
148
+ for digest in digest_list
149
+ ))
150
+ )
151
+
131
152
132
153
class MissingHashes (Hashes ):
133
154
"""A workalike for Hashes used when we're missing a hash for a requirement
Original file line number Diff line number Diff line change @@ -541,6 +541,16 @@ def test_non_zero(self):
541
541
assert not Hashes ()
542
542
assert not Hashes ({})
543
543
544
+ def test_equality (self ):
545
+ assert Hashes () == Hashes ()
546
+ assert Hashes ({'sha256' : ['abcd' ]}) == Hashes ({'sha256' : ['abcd' ]})
547
+ assert Hashes ({'sha256' : ['ab' , 'cd' ]}) == Hashes ({'sha256' : ['cd' , 'ab' ]})
548
+
549
+ def test_hash (self ):
550
+ cache = {}
551
+ cache [Hashes ({'sha256' : ['ab' , 'cd' ]})] = 42
552
+ assert cache [Hashes ({'sha256' : ['ab' , 'cd' ]})] == 42
553
+
544
554
545
555
class TestEncoding (object ):
546
556
"""Tests for pip._internal.utils.encoding"""
You can’t perform that action at this time.
0 commit comments