Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix usize not using the same hash as PyInt when used as key into a di… #5756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix usize not using the same hash as PyInt when used as key into a di…
…ctionary
  • Loading branch information
hbina committed Apr 30, 2025
commit aa275bafcc34d73482f5fccde169403d18f83013
5 changes: 5 additions & 0 deletions 5 common/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ pub fn hash_pointer(value: usize) -> PyHash {
hash as _
}

#[inline]
pub fn hash_integer<T: num_traits::PrimInt>(data: T) -> PyHash {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid unnecessarily complicated error handlings, pub fn hash_usize(data: usize) will be enough.
Let's place this function beside hash_bigint to find each other easier

fix_sentinel(mod_int(data.to_i64().unwrap()))
hbina marked this conversation as resolved.
Show resolved Hide resolved
}

#[inline]
pub fn hash_float(value: f64) -> Option<PyHash> {
// cpython _Py_HashDouble
Expand Down
8 changes: 8 additions & 0 deletions 8 extra_tests/snippets/builtin_object.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class MyObject:
pass


hbina marked this conversation as resolved.
Show resolved Hide resolved
assert not MyObject() == MyObject()
assert MyObject() != MyObject()
myobj = MyObject()
Expand All @@ -24,3 +25,10 @@ class MyObject:
assert not hasattr(obj, 'a')
obj.__dict__ = {'a': 1}
assert obj.a == 1

# Value inside the formatter goes through a different path of resolution.
# Check that it still works all the same
d = {
0: "ab",
}
assert "ab ab" == "{k[0]} {vv}".format(k=d, vv=d[0])
5 changes: 3 additions & 2 deletions 5 vm/src/dict_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{
object::{Traverse, TraverseFn},
};
use num_traits::ToPrimitive;
use rustpython_common::hash::hash_integer;
use std::{fmt, mem::size_of, ops::ControlFlow};

// HashIndex is intended to be same size with hash::PyHash
Expand Down Expand Up @@ -993,8 +994,8 @@ impl DictKey for usize {
*self
}

fn key_hash(&self, vm: &VirtualMachine) -> PyResult<HashValue> {
Ok(vm.state.hash_secret.hash_value(self))
fn key_hash(&self, _vm: &VirtualMachine) -> PyResult<HashValue> {
Ok(hash_integer(*self))
youknowone marked this conversation as resolved.
Show resolved Hide resolved
}

fn key_is(&self, _other: &PyObject) -> bool {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.