1
1
use crossbeam_utils:: atomic:: AtomicCell ;
2
2
use std:: fmt;
3
+ use std:: iter:: FromIterator ;
3
4
use std:: mem:: size_of;
4
5
5
6
use super :: pystr:: PyStrRef ;
6
7
use super :: pytype:: PyTypeRef ;
7
8
use super :: set:: PySet ;
8
9
use super :: IterStatus ;
10
+ use crate :: builtins:: list:: PyList ;
9
11
use crate :: dictdatatype:: { self , DictKey } ;
10
12
use crate :: exceptions:: PyBaseExceptionRef ;
11
13
use crate :: function:: { ArgIterable , FuncArgs , KwArgs , OptionalArg } ;
@@ -15,7 +17,7 @@ use crate::vm::{ReprGuard, VirtualMachine};
15
17
use crate :: {
16
18
IdProtocol , IntoPyObject , ItemProtocol , PyArithmaticValue :: * , PyAttributes , PyClassDef ,
17
19
PyClassImpl , PyComparisonValue , PyContext , PyObjectRef , PyRef , PyResult , PyValue ,
18
- TryFromBorrowedObject , TypeProtocol ,
20
+ TryFromBorrowedObject , TryFromObject , TypeProtocol ,
19
21
} ;
20
22
21
23
pub type DictContentType = dictdatatype:: Dict ;
@@ -957,7 +959,47 @@ pub struct PyMapping {
957
959
Option < fn ( PyObjectRef , PyObjectRef , Option < PyObjectRef > , & VirtualMachine ) -> PyResult < ( ) > > ,
958
960
}
959
961
960
- impl PyMapping { }
962
+ impl PyMapping {
963
+ fn method_output_as_list ( obj : PyObjectRef , method_name : & str , vm : & VirtualMachine ) -> PyResult {
964
+ let meth_output = vm. call_method ( & obj, method_name, ( ) ) ?;
965
+ if meth_output. is ( & vm. ctx . types . list_type ) {
966
+ return Ok ( meth_output) ;
967
+ }
968
+
969
+ iterator:: get_iter ( vm, meth_output)
970
+ // TODO : iterator to pylist
971
+ }
972
+
973
+ #[ inline]
974
+ pub fn check ( cls : PyTypeRef , vm : & VirtualMachine ) -> bool {
975
+ // TODO : do check mapping_protocol and subscript
976
+ true
977
+ }
978
+
979
+ fn items ( obj : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyDictItems > {
980
+ if obj. is ( & vm. ctx . types . dict_type ) {
981
+ Ok ( PyDict :: items ( PyDictRef :: try_from_object ( vm, obj) ?) )
982
+ } else {
983
+ method_output_as_list ( obj, "items" , vm)
984
+ }
985
+ }
986
+
987
+ fn keys ( obj : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyDictKeys > {
988
+ if obj. is ( & vm. ctx . types . dict_type ) {
989
+ Ok ( PyDict :: keys ( PyDictRef :: try_from_object ( vm, obj) ?) )
990
+ } else {
991
+ method_output_as_list ( obj, "keys" , vm)
992
+ }
993
+ }
994
+
995
+ fn values ( obj : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyDictValues > {
996
+ if obj. is ( & vm. ctx . types . dict_type ) {
997
+ Ok ( PyDict :: values ( PyDictRef :: try_from_object ( vm, obj) ?) )
998
+ } else {
999
+ method_output_as_list ( obj, "values" , vm)
1000
+ }
1001
+ }
1002
+ }
961
1003
962
1004
impl TryFromBorrowedObject for PyMapping {
963
1005
fn try_from_borrowed_object ( vm : & VirtualMachine , obj : & PyObjectRef ) -> PyResult < Self > {
0 commit comments