Skip to content

Commit fcb6551

Browse files
authored
Merge pull request RustPython#3342 from ChJR/feature/relocate_hash
Relocate vm._hash to obj.hash
2 parents c406b54 + 83c3644 commit fcb6551

File tree

8 files changed

+12
-16
lines changed

8 files changed

+12
-16
lines changed

vm/src/builtins/genericalias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn make_parameters(args: &PyTupleRef, vm: &VirtualMachine) -> PyTupleRef {
167167
impl Hashable for PyGenericAlias {
168168
#[inline]
169169
fn hash(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<hash::PyHash> {
170-
Ok(vm._hash(zelf.origin.as_object())? ^ vm._hash(zelf.args.as_object())?)
170+
Ok(zelf.origin.as_object().hash(vm)? ^ zelf.args.as_object().hash(vm)?)
171171
}
172172
}
173173

vm/src/builtins/weakref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl Hashable for PyWeak {
9393
let obj = zelf
9494
.upgrade()
9595
.ok_or_else(|| vm.new_type_error("weak object has gone away".to_owned()))?;
96-
let hash = vm._hash(&obj)?;
96+
let hash = obj.hash(vm)?;
9797
zelf.hash.store(Some(hash));
9898
Ok(hash)
9999
}

vm/src/dictdatatype.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ pub trait DictKey: IntoPyObject {
652652
/// to index dictionaries.
653653
impl DictKey for PyObjectRef {
654654
fn key_hash(&self, vm: &VirtualMachine) -> PyResult<HashValue> {
655-
vm._hash(self)
655+
self.hash(vm)
656656
}
657657

658658
fn key_is(&self, other: &PyObjectRef) -> bool {

vm/src/protocol/object.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ impl PyObjectRef {
133133
}
134134

135135
pub fn hash(&self, vm: &VirtualMachine) -> PyResult<PyHash> {
136-
vm._hash(self)
136+
let hash = self
137+
.class()
138+
.mro_find_map(|cls| cls.slots.hash.load())
139+
.unwrap(); // hash always exist
140+
hash(self, vm)
137141
}
138142

139143
// const hash_not_implemented: fn(&PyObjectRef, &VirtualMachine) ->PyResult<PyHash> = crate::types::Unhashable::slot_hash;

vm/src/stdlib/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ mod builtins {
337337

338338
#[pyfunction]
339339
fn hash(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyHash> {
340-
vm._hash(&obj)
340+
obj.hash(vm)
341341
}
342342

343343
// builtin_help

vm/src/stdlib/sre.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ mod _sre {
517517

518518
impl Hashable for Pattern {
519519
fn hash(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
520-
let hash = vm._hash(&zelf.pattern)?;
520+
let hash = zelf.pattern.hash(vm)?;
521521
let (_, code, _) = unsafe { zelf.code.align_to::<u8>() };
522522
let hash = hash ^ vm.state.hash_secret.hash_bytes(code);
523523
let hash = hash ^ (zelf.flags.bits() as PyHash);

vm/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ pub fn hash_iter<'a, I: IntoIterator<Item = &'a PyObjectRef>>(
7575
iter: I,
7676
vm: &VirtualMachine,
7777
) -> PyResult<rustpython_common::hash::PyHash> {
78-
vm.state.hash_secret.hash_iter(iter, |obj| vm._hash(obj))
78+
vm.state.hash_secret.hash_iter(iter, |obj| obj.hash(vm))
7979
}
8080

8181
pub fn hash_iter_unordered<'a, I: IntoIterator<Item = &'a PyObjectRef>>(
8282
iter: I,
8383
vm: &VirtualMachine,
8484
) -> PyResult<rustpython_common::hash::PyHash> {
85-
rustpython_common::hash::hash_iter_unordered(iter, |obj| vm._hash(obj))
85+
rustpython_common::hash::hash_iter_unordered(iter, |obj| obj.hash(vm))
8686
}
8787

8888
// TODO: find a better place to put this impl

vm/src/vm.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,14 +1880,6 @@ impl VirtualMachine {
18801880
self._cmp(&a, &b, op).map(|res| res.into_pyobject(self))
18811881
}
18821882

1883-
pub fn _hash(&self, obj: &PyObjectRef) -> PyResult<rustpython_common::hash::PyHash> {
1884-
let hash = obj
1885-
.class()
1886-
.mro_find_map(|cls| cls.slots.hash.load())
1887-
.unwrap(); // hash always exist
1888-
hash(obj, self)
1889-
}
1890-
18911883
pub fn obj_len_opt(&self, obj: &PyObjectRef) -> Option<PyResult<usize>> {
18921884
self.get_special_method(obj.clone(), "__len__")
18931885
.map(Result::ok)

0 commit comments

Comments
 (0)