File tree Expand file tree Collapse file tree 3 files changed +27
-21
lines changed
Expand file tree Collapse file tree 3 files changed +27
-21
lines changed Original file line number Diff line number Diff line change 11use crate :: obj:: objbool;
2+ use crate :: obj:: objstr:: PyString ;
23use crate :: pyhash;
34use crate :: pyobject:: { IdProtocol , PyObjectRef , PyResult } ;
45use crate :: vm:: VirtualMachine ;
@@ -308,6 +309,7 @@ impl DictKey for PyObjectRef {
308309/// to index dictionaries.
309310impl 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
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ use crate::vm::{ReprGuard, VirtualMachine};
1111use super :: objbool;
1212use super :: objiter;
1313use super :: objstr;
14+ use super :: objtype;
1415use crate :: dictdatatype;
1516use crate :: obj:: objtype:: PyClassRef ;
1617use 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
321339impl ItemProtocol for PyDictRef {
Original file line number Diff line number Diff 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
10671049impl ItemProtocol for PyObjectRef {
You can’t perform that action at this time.
0 commit comments