Merge pull request #3342 from ChJR/feature/relocate_hash

Relocate vm._hash to obj.hash
This commit is contained in:
Jim Fasarakis-Hilliard
2021-10-19 22:04:23 +03:00
committed by GitHub
8 changed files with 12 additions and 16 deletions

View File

@@ -167,7 +167,7 @@ fn make_parameters(args: &PyTupleRef, vm: &VirtualMachine) -> PyTupleRef {
impl Hashable for PyGenericAlias {
#[inline]
fn hash(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<hash::PyHash> {
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)?)
}
}

View File

@@ -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)
}

View File

@@ -652,7 +652,7 @@ pub trait DictKey: IntoPyObject {
/// to index dictionaries.
impl DictKey for PyObjectRef {
fn key_hash(&self, vm: &VirtualMachine) -> PyResult<HashValue> {
vm._hash(self)
self.hash(vm)
}
fn key_is(&self, other: &PyObjectRef) -> bool {

View File

@@ -133,7 +133,11 @@ impl PyObjectRef {
}
pub fn hash(&self, vm: &VirtualMachine) -> PyResult<PyHash> {
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<PyHash> = crate::types::Unhashable::slot_hash;

View File

@@ -337,7 +337,7 @@ mod builtins {
#[pyfunction]
fn hash(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyHash> {
vm._hash(&obj)
obj.hash(vm)
}
// builtin_help

View File

@@ -517,7 +517,7 @@ mod _sre {
impl Hashable for Pattern {
fn hash(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
let hash = vm._hash(&zelf.pattern)?;
let hash = zelf.pattern.hash(vm)?;
let (_, code, _) = unsafe { zelf.code.align_to::<u8>() };
let hash = hash ^ vm.state.hash_secret.hash_bytes(code);
let hash = hash ^ (zelf.flags.bits() as PyHash);

View File

@@ -75,14 +75,14 @@ pub fn hash_iter<'a, I: IntoIterator<Item = &'a PyObjectRef>>(
iter: I,
vm: &VirtualMachine,
) -> PyResult<rustpython_common::hash::PyHash> {
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<Item = &'a PyObjectRef>>(
iter: I,
vm: &VirtualMachine,
) -> PyResult<rustpython_common::hash::PyHash> {
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

View File

@@ -1880,14 +1880,6 @@ impl VirtualMachine {
self._cmp(&a, &b, op).map(|res| res.into_pyobject(self))
}
pub fn _hash(&self, obj: &PyObjectRef) -> PyResult<rustpython_common::hash::PyHash> {
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<PyResult<usize>> {
self.get_special_method(obj.clone(), "__len__")
.map(Result::ok)