Skip to content

Commit 17f5aed

Browse files
authored
Merge pull request #4548 from youknowone/hash-tuple
Prepare to implement hash for tuple
2 parents fb44ded + 3886f88 commit 17f5aed

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

vm/src/builtins/range.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use super::{PyInt, PyIntRef, PySlice, PyTupleRef, PyType, PyTypeRef};
2-
use crate::atomic_func;
3-
use crate::common::hash::PyHash;
1+
use super::{
2+
builtins_iter, tuple::tuple_hash, PyInt, PyIntRef, PySlice, PyTupleRef, PyType, PyTypeRef,
3+
};
44
use crate::{
5-
builtins::builtins_iter,
5+
atomic_func,
66
class::PyClassImpl,
7+
common::hash::PyHash,
78
function::{FuncArgs, OptionalArg, PyComparisonValue},
89
protocol::{PyIterReturn, PyMappingMethods, PySequenceMethods},
910
types::{
@@ -442,7 +443,7 @@ impl Hashable for PyRange {
442443
zelf.step().into(),
443444
]
444445
};
445-
crate::utils::hash_iter(elements.iter(), vm)
446+
tuple_hash(&elements, vm)
446447
}
447448
}
448449

vm/src/builtins/tuple.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use once_cell::sync::Lazy;
22

33
use super::{PositionIterInternal, PyGenericAlias, PyType, PyTypeRef};
4-
use crate::atomic_func;
54
use crate::common::{hash::PyHash, lock::PyMutex};
65
use crate::{
6+
atomic_func,
77
class::PyClassImpl,
88
convert::{ToPyObject, TransmuteFromObject},
99
function::{OptionalArg, PyArithmeticValue, PyComparisonValue},
@@ -372,7 +372,7 @@ impl AsSequence for PyTuple {
372372
impl Hashable for PyTuple {
373373
#[inline]
374374
fn hash(zelf: &crate::Py<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
375-
crate::utils::hash_iter(zelf.elements.iter(), vm)
375+
tuple_hash(zelf.as_slice(), vm)
376376
}
377377
}
378378

@@ -523,3 +523,9 @@ impl<T: TransmuteFromObject> ToPyObject for PyTupleTyped<T> {
523523
self.tuple.into()
524524
}
525525
}
526+
527+
pub(super) fn tuple_hash(elements: &[PyObjectRef], vm: &VirtualMachine) -> PyResult<PyHash> {
528+
// TODO: See #3460 for the correct implementation.
529+
// https://github.com/RustPython/RustPython/pull/3460
530+
crate::utils::hash_iter(elements.iter(), vm)
531+
}

0 commit comments

Comments
 (0)