Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement do_eq for String impl of DictKey. Also move get_item_option…
… to dict type.
  • Loading branch information
windelbouwman committed Jul 28, 2019
commit 42dca4433aef7d76538c0d1e89376b5cf64069e9
12 changes: 9 additions & 3 deletions vm/src/dictdatatype.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::obj::objbool;
use crate::obj::objstr::PyString;
use crate::pyhash;
use crate::pyobject::{IdProtocol, PyObjectRef, PyResult};
use crate::vm::VirtualMachine;
Expand Down Expand Up @@ -308,6 +309,7 @@ impl DictKey for PyObjectRef {
/// to index dictionaries.
impl DictKey for String {
fn do_hash(&self, _vm: &VirtualMachine) -> PyResult<HashValue> {
// follow a similar route as the hashing of PyStringRef
let raw_hash = pyhash::hash_value(self).to_bigint().unwrap();
let raw_hash = pyhash::hash_bigint(&raw_hash);
let mut hasher = DefaultHasher::new();
Expand All @@ -322,9 +324,13 @@ impl DictKey for String {
}

fn do_eq(&self, vm: &VirtualMachine, other_key: &PyObjectRef) -> PyResult<bool> {
// Fall back to PyString implementation.
let s = vm.new_str(self.to_string());
s.do_eq(vm, other_key)
if let Some(py_str_value) = other_key.payload::<PyString>() {
Ok(&py_str_value.value == self)
} else {
// Fall back to PyString implementation.
let s = vm.new_str(self.to_string());
s.do_eq(vm, other_key)
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions vm/src/obj/objdict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::vm::{ReprGuard, VirtualMachine};
use super::objbool;
use super::objiter;
use super::objstr;
use super::objtype;
use crate::dictdatatype;
use crate::obj::objtype::PyClassRef;
use crate::pyobject::PyClassImpl;
Expand Down Expand Up @@ -316,6 +317,23 @@ impl PyDictRef {
pub fn size(&self) -> dictdatatype::DictSize {
self.entries.borrow().size()
}

pub fn get_item_option<T: IntoPyObject>(
&self,
key: T,
vm: &VirtualMachine,
) -> PyResult<Option<PyObjectRef>> {
match self.get_item(key, vm) {
Ok(value) => Ok(Some(value)),
Err(exc) => {
if objtype::isinstance(&exc, &vm.ctx.exceptions.key_error) {
Ok(None)
} else {
Err(exc)
}
}
}
}
}

impl ItemProtocol for PyDictRef {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/obj/objsuper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::obj::objfunction::PyMethod;
use crate::obj::objstr;
use crate::obj::objtype::{PyClass, PyClassRef};
use crate::pyobject::{
ItemProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
};
use crate::scope::NameProtocol;
use crate::vm::VirtualMachine;
Expand Down
18 changes: 0 additions & 18 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,24 +1044,6 @@ pub trait ItemProtocol {
vm: &VirtualMachine,
) -> PyResult;
fn del_item<T: IntoPyObject>(&self, key: T, vm: &VirtualMachine) -> PyResult;

#[cfg_attr(feature = "flame-it", flame("ItemProtocol"))]
fn get_item_option<T: IntoPyObject>(
&self,
key: T,
vm: &VirtualMachine,
) -> PyResult<Option<PyObjectRef>> {
match self.get_item(key, vm) {
Ok(value) => Ok(Some(value)),
Err(exc) => {
if objtype::isinstance(&exc, &vm.ctx.exceptions.key_error) {
Ok(None)
} else {
Err(exc)
}
}
}
}
}

impl ItemProtocol for PyObjectRef {
Expand Down