Skip to content

Commit 42dca44

Browse files
committed
Implement do_eq for String impl of DictKey. Also move get_item_option to dict type.
1 parent 38d9f40 commit 42dca44

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

vm/src/dictdatatype.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::obj::objbool;
2+
use crate::obj::objstr::PyString;
23
use crate::pyhash;
34
use crate::pyobject::{IdProtocol, PyObjectRef, PyResult};
45
use crate::vm::VirtualMachine;
@@ -308,6 +309,7 @@ impl DictKey for PyObjectRef {
308309
/// to index dictionaries.
309310
impl DictKey for String {
310311
fn do_hash(&self, _vm: &VirtualMachine) -> PyResult<HashValue> {
312+
// follow a similar route as the hashing of PyStringRef
311313
let raw_hash = pyhash::hash_value(self).to_bigint().unwrap();
312314
let raw_hash = pyhash::hash_bigint(&raw_hash);
313315
let mut hasher = DefaultHasher::new();
@@ -322,9 +324,13 @@ impl DictKey for String {
322324
}
323325

324326
fn do_eq(&self, vm: &VirtualMachine, other_key: &PyObjectRef) -> PyResult<bool> {
325-
// Fall back to PyString implementation.
326-
let s = vm.new_str(self.to_string());
327-
s.do_eq(vm, other_key)
327+
if let Some(py_str_value) = other_key.payload::<PyString>() {
328+
Ok(&py_str_value.value == self)
329+
} else {
330+
// Fall back to PyString implementation.
331+
let s = vm.new_str(self.to_string());
332+
s.do_eq(vm, other_key)
333+
}
328334
}
329335
}
330336

vm/src/obj/objdict.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::vm::{ReprGuard, VirtualMachine};
1111
use super::objbool;
1212
use super::objiter;
1313
use super::objstr;
14+
use super::objtype;
1415
use crate::dictdatatype;
1516
use crate::obj::objtype::PyClassRef;
1617
use crate::pyobject::PyClassImpl;
@@ -316,6 +317,23 @@ impl PyDictRef {
316317
pub fn size(&self) -> dictdatatype::DictSize {
317318
self.entries.borrow().size()
318319
}
320+
321+
pub fn get_item_option<T: IntoPyObject>(
322+
&self,
323+
key: T,
324+
vm: &VirtualMachine,
325+
) -> PyResult<Option<PyObjectRef>> {
326+
match self.get_item(key, vm) {
327+
Ok(value) => Ok(Some(value)),
328+
Err(exc) => {
329+
if objtype::isinstance(&exc, &vm.ctx.exceptions.key_error) {
330+
Ok(None)
331+
} else {
332+
Err(exc)
333+
}
334+
}
335+
}
336+
}
319337
}
320338

321339
impl ItemProtocol for PyDictRef {

vm/src/obj/objsuper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::obj::objfunction::PyMethod;
1111
use crate::obj::objstr;
1212
use crate::obj::objtype::{PyClass, PyClassRef};
1313
use crate::pyobject::{
14-
ItemProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
14+
PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
1515
};
1616
use crate::scope::NameProtocol;
1717
use crate::vm::VirtualMachine;

vm/src/pyobject.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,24 +1044,6 @@ pub trait ItemProtocol {
10441044
vm: &VirtualMachine,
10451045
) -> PyResult;
10461046
fn del_item<T: IntoPyObject>(&self, key: T, vm: &VirtualMachine) -> PyResult;
1047-
1048-
#[cfg_attr(feature = "flame-it", flame("ItemProtocol"))]
1049-
fn get_item_option<T: IntoPyObject>(
1050-
&self,
1051-
key: T,
1052-
vm: &VirtualMachine,
1053-
) -> PyResult<Option<PyObjectRef>> {
1054-
match self.get_item(key, vm) {
1055-
Ok(value) => Ok(Some(value)),
1056-
Err(exc) => {
1057-
if objtype::isinstance(&exc, &vm.ctx.exceptions.key_error) {
1058-
Ok(None)
1059-
} else {
1060-
Err(exc)
1061-
}
1062-
}
1063-
}
1064-
}
10651047
}
10661048

10671049
impl ItemProtocol for PyObjectRef {

0 commit comments

Comments
 (0)