Skip to content

Commit d388a13

Browse files
committed
Fix hash_complex's hash function
1 parent 8a97c0b commit d388a13

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

common/src/hash.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ pub fn hash_float(value: f64) -> Option<PyHash> {
140140
Some(fix_sentinel(x as PyHash * value.signum() as PyHash))
141141
}
142142

143-
pub fn hash_complex(value: &Complex64) -> PyHash {
144-
let re_hash = hash_float(value.re).unwrap();
145-
let im_hash = hash_float(value.im).unwrap();
143+
pub fn hash_complex(value: &Complex64) -> Option<PyHash> {
144+
let re_hash = hash_float(value.re)?;
145+
let im_hash = hash_float(value.im)?;
146146
let Wrapping(ret) = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(IMAG);
147-
fix_sentinel(ret)
147+
Some(fix_sentinel(ret))
148148
}
149149

150150
pub fn hash_iter_unordered<'a, T: 'a, I, F, E>(iter: I, hashf: F) -> Result<PyHash, E>

vm/src/builtins/complex.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,12 @@ impl Comparable for PyComplex {
417417
impl Hashable for PyComplex {
418418
#[inline]
419419
fn hash(zelf: &crate::Py<Self>, _vm: &VirtualMachine) -> PyResult<hash::PyHash> {
420-
Ok(hash::hash_complex(&zelf.value))
420+
match hash::hash_complex(&zelf.value) {
421+
Some(value) => Ok(value),
422+
None => Ok(hash::hash_pointer(
423+
zelf as *const _ as *const std::ffi::c_void,
424+
)),
425+
}
421426
}
422427
}
423428

vm/src/builtins/float.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,9 @@ impl Hashable for PyFloat {
541541
fn hash(zelf: &crate::Py<Self>, _vm: &VirtualMachine) -> PyResult<hash::PyHash> {
542542
match hash::hash_float(zelf.to_f64()) {
543543
Some(value) => Ok(value),
544-
None => Ok(hash::hash_pointer(zelf as *const _ as *const std::ffi::c_void)),
544+
None => Ok(hash::hash_pointer(
545+
zelf as *const _ as *const std::ffi::c_void,
546+
)),
545547
}
546548
}
547549
}

0 commit comments

Comments
 (0)