diff --git a/vm/src/builtins/genericalias.rs b/vm/src/builtins/genericalias.rs index 7b017df166..889ebdd932 100644 --- a/vm/src/builtins/genericalias.rs +++ b/vm/src/builtins/genericalias.rs @@ -167,7 +167,7 @@ fn make_parameters(args: &PyTupleRef, vm: &VirtualMachine) -> PyTupleRef { impl Hashable for PyGenericAlias { #[inline] fn hash(zelf: &PyRef, vm: &VirtualMachine) -> PyResult { - Ok(vm._hash(zelf.origin.as_object())? ^ vm._hash(zelf.args.as_object())?) + Ok(zelf.origin.as_object().hash(vm)? ^ zelf.args.as_object().hash(vm)?) } } diff --git a/vm/src/builtins/weakref.rs b/vm/src/builtins/weakref.rs index f3c84e2ba4..1323a10764 100644 --- a/vm/src/builtins/weakref.rs +++ b/vm/src/builtins/weakref.rs @@ -93,7 +93,7 @@ impl Hashable for PyWeak { let obj = zelf .upgrade() .ok_or_else(|| vm.new_type_error("weak object has gone away".to_owned()))?; - let hash = vm._hash(&obj)?; + let hash = obj.hash(vm)?; zelf.hash.store(Some(hash)); Ok(hash) } diff --git a/vm/src/dictdatatype.rs b/vm/src/dictdatatype.rs index 217a54c3a7..46fc5560b2 100644 --- a/vm/src/dictdatatype.rs +++ b/vm/src/dictdatatype.rs @@ -652,7 +652,7 @@ pub trait DictKey: IntoPyObject { /// to index dictionaries. impl DictKey for PyObjectRef { fn key_hash(&self, vm: &VirtualMachine) -> PyResult { - vm._hash(self) + self.hash(vm) } fn key_is(&self, other: &PyObjectRef) -> bool { diff --git a/vm/src/protocol/object.rs b/vm/src/protocol/object.rs index 72dae06aa7..f9230c8b4f 100644 --- a/vm/src/protocol/object.rs +++ b/vm/src/protocol/object.rs @@ -133,7 +133,11 @@ impl PyObjectRef { } pub fn hash(&self, vm: &VirtualMachine) -> PyResult { - vm._hash(self) + let hash = self + .class() + .mro_find_map(|cls| cls.slots.hash.load()) + .unwrap(); // hash always exist + hash(self, vm) } // const hash_not_implemented: fn(&PyObjectRef, &VirtualMachine) ->PyResult = crate::types::Unhashable::slot_hash; diff --git a/vm/src/stdlib/builtins.rs b/vm/src/stdlib/builtins.rs index 43a4d912f0..c2dd496f02 100644 --- a/vm/src/stdlib/builtins.rs +++ b/vm/src/stdlib/builtins.rs @@ -337,7 +337,7 @@ mod builtins { #[pyfunction] fn hash(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { - vm._hash(&obj) + obj.hash(vm) } // builtin_help diff --git a/vm/src/stdlib/sre.rs b/vm/src/stdlib/sre.rs index b39421c9d0..e46989d544 100644 --- a/vm/src/stdlib/sre.rs +++ b/vm/src/stdlib/sre.rs @@ -517,7 +517,7 @@ mod _sre { impl Hashable for Pattern { fn hash(zelf: &PyRef, vm: &VirtualMachine) -> PyResult { - let hash = vm._hash(&zelf.pattern)?; + let hash = zelf.pattern.hash(vm)?; let (_, code, _) = unsafe { zelf.code.align_to::() }; let hash = hash ^ vm.state.hash_secret.hash_bytes(code); let hash = hash ^ (zelf.flags.bits() as PyHash); diff --git a/vm/src/utils.rs b/vm/src/utils.rs index 081211ec28..01fa29bb47 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -75,14 +75,14 @@ pub fn hash_iter<'a, I: IntoIterator>( iter: I, vm: &VirtualMachine, ) -> PyResult { - vm.state.hash_secret.hash_iter(iter, |obj| vm._hash(obj)) + vm.state.hash_secret.hash_iter(iter, |obj| obj.hash(vm)) } pub fn hash_iter_unordered<'a, I: IntoIterator>( iter: I, vm: &VirtualMachine, ) -> PyResult { - rustpython_common::hash::hash_iter_unordered(iter, |obj| vm._hash(obj)) + rustpython_common::hash::hash_iter_unordered(iter, |obj| obj.hash(vm)) } // TODO: find a better place to put this impl diff --git a/vm/src/vm.rs b/vm/src/vm.rs index ffa6f6ac1e..4492b5374a 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -1880,14 +1880,6 @@ impl VirtualMachine { self._cmp(&a, &b, op).map(|res| res.into_pyobject(self)) } - pub fn _hash(&self, obj: &PyObjectRef) -> PyResult { - let hash = obj - .class() - .mro_find_map(|cls| cls.slots.hash.load()) - .unwrap(); // hash always exist - hash(obj, self) - } - pub fn obj_len_opt(&self, obj: &PyObjectRef) -> Option> { self.get_special_method(obj.clone(), "__len__") .map(Result::ok)