Skip to content

Commit 4e8d34d

Browse files
committed
Fix usize not using the same hash as PyInt when used as key into a dictionary
1 parent 1807464 commit 4e8d34d

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

common/src/hash.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ impl HashSecret {
5353
fix_sentinel(mod_int(self.hash_one(data) as _))
5454
}
5555

56+
pub fn hash_integer<T>(&self, data: T) -> PyHash
57+
where
58+
T: Into<BigInt>,
59+
{
60+
let bigint = data.into();
61+
let result = hash_bigint(&bigint);
62+
result
63+
}
64+
5665
pub fn hash_iter<'a, T: 'a, I, F, E>(&self, iter: I, hash_func: F) -> Result<PyHash, E>
5766
where
5867
I: IntoIterator<Item = &'a T>,

extra_tests/snippets/builtin_object.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class MyObject:
22
pass
33

4+
45
assert not MyObject() == MyObject()
56
assert MyObject() != MyObject()
67
myobj = MyObject()
@@ -24,3 +25,10 @@ class MyObject:
2425
assert not hasattr(obj, 'a')
2526
obj.__dict__ = {'a': 1}
2627
assert obj.a == 1
28+
29+
# Value inside the formatter goes through a different path of resolution.
30+
# Check that it still works all the same
31+
d = {
32+
0: "ab",
33+
}
34+
assert "ab ab" == "{k[0]} {vv}".format(k=d, vv=d[0])

vm/src/dict_inner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ impl DictKey for usize {
994994
}
995995

996996
fn key_hash(&self, vm: &VirtualMachine) -> PyResult<HashValue> {
997-
Ok(vm.state.hash_secret.hash_value(self))
997+
Ok(vm.state.hash_secret.hash_integer(*self))
998998
}
999999

10001000
fn key_is(&self, _other: &PyObject) -> bool {

0 commit comments

Comments
 (0)