44
55// use std::ops::Deref;
66use std:: char;
7- use std:: collections:: HashMap ;
87use std:: io:: { self , Write } ;
98
109use super :: compile;
1110use super :: obj:: objbool;
11+ use super :: obj:: objdict;
1212use super :: obj:: objint;
1313use super :: obj:: objiter;
1414use super :: obj:: objstr;
1515use super :: obj:: objtype;
1616use super :: pyobject:: {
17- AttributeProtocol , DictProtocol , IdProtocol , PyContext , PyFuncArgs , PyObject , PyObjectKind ,
18- PyObjectRef , PyResult , Scope , TypeProtocol ,
17+ AttributeProtocol , IdProtocol , PyContext , PyFuncArgs , PyObject , PyObjectKind , PyObjectRef ,
18+ PyResult , Scope , TypeProtocol ,
1919} ;
2020use super :: vm:: VirtualMachine ;
2121use num_bigint:: ToBigInt ;
@@ -25,14 +25,10 @@ fn get_locals(vm: &mut VirtualMachine) -> PyObjectRef {
2525 let d = vm. new_dict ( ) ;
2626 // TODO: implement dict_iter_items?
2727 let locals = vm. get_locals ( ) ;
28- match locals. borrow ( ) . kind {
29- PyObjectKind :: Dict { ref elements } => {
30- for l in elements {
31- d. set_item ( l. 0 , l. 1 . clone ( ) ) ;
32- }
33- }
34- _ => { }
35- } ;
28+ let key_value_pairs = objdict:: get_key_value_pairs ( vm, & locals) ;
29+ for ( key, value) in key_value_pairs {
30+ objdict:: set_item ( & d, & key, & value) ;
31+ }
3632 d
3733}
3834
@@ -662,15 +658,11 @@ pub fn builtin_print(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
662658
663659fn builtin_range ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
664660 arg_check ! ( vm, args, required = [ ( range, Some ( vm. ctx. int_type( ) ) ) ] ) ;
665- match range. borrow ( ) . kind {
666- PyObjectKind :: Integer { ref value } => {
667- let range_elements: Vec < PyObjectRef > = ( 0 ..value. to_i32 ( ) . unwrap ( ) )
668- . map ( |num| vm. context ( ) . new_int ( num. to_bigint ( ) . unwrap ( ) ) )
669- . collect ( ) ;
670- Ok ( vm. context ( ) . new_list ( range_elements) )
671- }
672- _ => panic ! ( "argument checking failure: first argument to range must be an integer" ) ,
673- }
661+ let value = objint:: get_value ( range) ;
662+ let range_elements: Vec < PyObjectRef > = ( 0 ..value. to_i32 ( ) . unwrap ( ) )
663+ . map ( |num| vm. context ( ) . new_int ( num. to_bigint ( ) . unwrap ( ) ) )
664+ . collect ( ) ;
665+ Ok ( vm. context ( ) . new_list ( range_elements) )
674666}
675667
676668fn builtin_repr ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
@@ -687,7 +679,7 @@ fn builtin_setattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
687679 required = [ ( obj, None ) , ( attr, Some ( vm. ctx. str_type( ) ) ) , ( value, None ) ]
688680 ) ;
689681 let name = objstr:: get_value ( attr) ;
690- obj . clone ( ) . set_attr ( & name, value. clone ( ) ) ;
682+ vm . ctx . set_attr ( obj , & name, value. clone ( ) ) ;
691683 Ok ( vm. get_none ( ) )
692684}
693685
@@ -736,122 +728,99 @@ fn builtin_zip(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
736728// builtin___import__
737729
738730pub fn make_module ( ctx : & PyContext ) -> PyObjectRef {
739- // scope[String::from("print")] = print;
740- let mut dict = HashMap :: new ( ) ;
731+ let mod_name = "__builtins__" . to_string ( ) ;
732+ let py_mod = ctx. new_module ( & mod_name, ctx. new_scope ( None ) ) ;
733+
741734 //set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
742- dict. insert (
743- String :: from ( "__name__" ) ,
744- ctx. new_str ( String :: from ( "__main__" ) ) ,
745- ) ;
746- dict. insert ( String :: from ( "abs" ) , ctx. new_rustfunc ( builtin_abs) ) ;
747- dict. insert ( String :: from ( "all" ) , ctx. new_rustfunc ( builtin_all) ) ;
748- dict. insert ( String :: from ( "any" ) , ctx. new_rustfunc ( builtin_any) ) ;
749- dict. insert ( String :: from ( "bin" ) , ctx. new_rustfunc ( builtin_bin) ) ;
750- dict. insert ( String :: from ( "bool" ) , ctx. bool_type ( ) ) ;
751- dict. insert ( String :: from ( "bytearray" ) , ctx. bytearray_type ( ) ) ;
752- dict. insert ( String :: from ( "bytes" ) , ctx. bytes_type ( ) ) ;
753- dict. insert ( String :: from ( "callable" ) , ctx. new_rustfunc ( builtin_callable) ) ;
754- dict. insert ( String :: from ( "chr" ) , ctx. new_rustfunc ( builtin_chr) ) ;
755- dict. insert ( String :: from ( "classmethod" ) , ctx. classmethod_type ( ) ) ;
756- dict. insert ( String :: from ( "compile" ) , ctx. new_rustfunc ( builtin_compile) ) ;
757- dict. insert ( String :: from ( "complex" ) , ctx. complex_type ( ) ) ;
758- dict. insert ( String :: from ( "delattr" ) , ctx. new_rustfunc ( builtin_delattr) ) ;
759- dict. insert ( String :: from ( "dict" ) , ctx. dict_type ( ) ) ;
760- dict. insert ( String :: from ( "divmod" ) , ctx. new_rustfunc ( builtin_divmod) ) ;
761- dict. insert ( String :: from ( "dir" ) , ctx. new_rustfunc ( builtin_dir) ) ;
762- dict. insert (
763- String :: from ( "enumerate" ) ,
764- ctx. new_rustfunc ( builtin_enumerate) ,
765- ) ;
766- dict. insert ( String :: from ( "eval" ) , ctx. new_rustfunc ( builtin_eval) ) ;
767- dict. insert ( String :: from ( "exec" ) , ctx. new_rustfunc ( builtin_exec) ) ;
768- dict. insert ( String :: from ( "float" ) , ctx. float_type ( ) ) ;
769- dict. insert ( String :: from ( "frozenset" ) , ctx. frozenset_type ( ) ) ;
770- dict. insert ( String :: from ( "filter" ) , ctx. new_rustfunc ( builtin_filter) ) ;
771- dict. insert ( String :: from ( "getattr" ) , ctx. new_rustfunc ( builtin_getattr) ) ;
772- dict. insert ( String :: from ( "hasattr" ) , ctx. new_rustfunc ( builtin_hasattr) ) ;
773- dict. insert ( String :: from ( "hash" ) , ctx. new_rustfunc ( builtin_hash) ) ;
774- dict. insert ( String :: from ( "hex" ) , ctx. new_rustfunc ( builtin_hex) ) ;
775- dict. insert ( String :: from ( "id" ) , ctx. new_rustfunc ( builtin_id) ) ;
776- dict. insert ( String :: from ( "int" ) , ctx. int_type ( ) ) ;
777- dict. insert (
778- String :: from ( "isinstance" ) ,
779- ctx. new_rustfunc ( builtin_isinstance) ,
780- ) ;
781- dict. insert (
782- String :: from ( "issubclass" ) ,
783- ctx. new_rustfunc ( builtin_issubclass) ,
784- ) ;
785- dict. insert ( String :: from ( "iter" ) , ctx. new_rustfunc ( builtin_iter) ) ;
786- dict. insert ( String :: from ( "len" ) , ctx. new_rustfunc ( builtin_len) ) ;
787- dict. insert ( String :: from ( "list" ) , ctx. list_type ( ) ) ;
788- dict. insert ( String :: from ( "locals" ) , ctx. new_rustfunc ( builtin_locals) ) ;
789- dict. insert ( String :: from ( "map" ) , ctx. new_rustfunc ( builtin_map) ) ;
790- dict. insert ( String :: from ( "max" ) , ctx. new_rustfunc ( builtin_max) ) ;
791- dict. insert ( String :: from ( "min" ) , ctx. new_rustfunc ( builtin_min) ) ;
792- dict. insert ( String :: from ( "object" ) , ctx. object ( ) ) ;
793- dict. insert ( String :: from ( "oct" ) , ctx. new_rustfunc ( builtin_oct) ) ;
794- dict. insert ( String :: from ( "ord" ) , ctx. new_rustfunc ( builtin_ord) ) ;
795- dict. insert ( String :: from ( "next" ) , ctx. new_rustfunc ( builtin_next) ) ;
796- dict. insert ( String :: from ( "pow" ) , ctx. new_rustfunc ( builtin_pow) ) ;
797- dict. insert ( String :: from ( "print" ) , ctx. new_rustfunc ( builtin_print) ) ;
798- dict. insert ( String :: from ( "property" ) , ctx. property_type ( ) ) ;
799- dict. insert ( String :: from ( "range" ) , ctx. new_rustfunc ( builtin_range) ) ;
800- dict. insert ( String :: from ( "repr" ) , ctx. new_rustfunc ( builtin_repr) ) ;
801- dict. insert ( String :: from ( "set" ) , ctx. set_type ( ) ) ;
802- dict. insert ( String :: from ( "setattr" ) , ctx. new_rustfunc ( builtin_setattr) ) ;
803- dict. insert ( String :: from ( "staticmethod" ) , ctx. staticmethod_type ( ) ) ;
804- dict. insert ( String :: from ( "str" ) , ctx. str_type ( ) ) ;
805- dict. insert ( String :: from ( "sum" ) , ctx. new_rustfunc ( builtin_sum) ) ;
806- dict. insert ( String :: from ( "super" ) , ctx. super_type ( ) ) ;
807- dict. insert ( String :: from ( "tuple" ) , ctx. tuple_type ( ) ) ;
808- dict. insert ( String :: from ( "type" ) , ctx. type_type ( ) ) ;
809- dict. insert ( String :: from ( "zip" ) , ctx. new_rustfunc ( builtin_zip) ) ;
735+ ctx. set_attr ( & py_mod, "__name__" , ctx. new_str ( String :: from ( "__main__" ) ) ) ;
736+
737+ ctx. set_item ( & py_mod, "abs" , ctx. new_rustfunc ( builtin_abs) ) ;
738+ ctx. set_attr ( & py_mod, "all" , ctx. new_rustfunc ( builtin_all) ) ;
739+ ctx. set_attr ( & py_mod, "any" , ctx. new_rustfunc ( builtin_any) ) ;
740+ ctx. set_attr ( & py_mod, "bin" , ctx. new_rustfunc ( builtin_bin) ) ;
741+ ctx. set_attr ( & py_mod, "bool" , ctx. bool_type ( ) ) ;
742+ ctx. set_attr ( & py_mod, "bytearray" , ctx. bytearray_type ( ) ) ;
743+ ctx. set_attr ( & py_mod, "bytes" , ctx. bytes_type ( ) ) ;
744+ ctx. set_attr ( & py_mod, "callable" , ctx. new_rustfunc ( builtin_callable) ) ;
745+ ctx. set_attr ( & py_mod, "chr" , ctx. new_rustfunc ( builtin_chr) ) ;
746+ ctx. set_attr ( & py_mod, "classmethod" , ctx. classmethod_type ( ) ) ;
747+ ctx. set_attr ( & py_mod, "compile" , ctx. new_rustfunc ( builtin_compile) ) ;
748+ ctx. set_attr ( & py_mod, "complex" , ctx. complex_type ( ) ) ;
749+ ctx. set_attr ( & py_mod, "delattr" , ctx. new_rustfunc ( builtin_delattr) ) ;
750+ ctx. set_attr ( & py_mod, "dict" , ctx. dict_type ( ) ) ;
751+ ctx. set_attr ( & py_mod, "divmod" , ctx. new_rustfunc ( builtin_divmod) ) ;
752+ ctx. set_attr ( & py_mod, "dir" , ctx. new_rustfunc ( builtin_dir) ) ;
753+ ctx. set_attr ( & py_mod, "enumerate" , ctx. new_rustfunc ( builtin_enumerate) ) ;
754+ ctx. set_attr ( & py_mod, "eval" , ctx. new_rustfunc ( builtin_eval) ) ;
755+ ctx. set_attr ( & py_mod, "exec" , ctx. new_rustfunc ( builtin_exec) ) ;
756+ ctx. set_attr ( & py_mod, "float" , ctx. float_type ( ) ) ;
757+ ctx. set_attr ( & py_mod, "frozenset" , ctx. frozenset_type ( ) ) ;
758+ ctx. set_attr ( & py_mod, "filter" , ctx. new_rustfunc ( builtin_filter) ) ;
759+ ctx. set_attr ( & py_mod, "getattr" , ctx. new_rustfunc ( builtin_getattr) ) ;
760+ ctx. set_attr ( & py_mod, "hasattr" , ctx. new_rustfunc ( builtin_hasattr) ) ;
761+ ctx. set_attr ( & py_mod, "hash" , ctx. new_rustfunc ( builtin_hash) ) ;
762+ ctx. set_attr ( & py_mod, "hex" , ctx. new_rustfunc ( builtin_hex) ) ;
763+ ctx. set_attr ( & py_mod, "id" , ctx. new_rustfunc ( builtin_id) ) ;
764+ ctx. set_attr ( & py_mod, "int" , ctx. int_type ( ) ) ;
765+ ctx. set_attr ( & py_mod, "isinstance" , ctx. new_rustfunc ( builtin_isinstance) ) ;
766+ ctx. set_attr ( & py_mod, "issubclass" , ctx. new_rustfunc ( builtin_issubclass) ) ;
767+ ctx. set_attr ( & py_mod, "iter" , ctx. new_rustfunc ( builtin_iter) ) ;
768+ ctx. set_attr ( & py_mod, "len" , ctx. new_rustfunc ( builtin_len) ) ;
769+ ctx. set_attr ( & py_mod, "list" , ctx. list_type ( ) ) ;
770+ ctx. set_attr ( & py_mod, "locals" , ctx. new_rustfunc ( builtin_locals) ) ;
771+ ctx. set_attr ( & py_mod, "map" , ctx. new_rustfunc ( builtin_map) ) ;
772+ ctx. set_attr ( & py_mod, "max" , ctx. new_rustfunc ( builtin_max) ) ;
773+ ctx. set_attr ( & py_mod, "min" , ctx. new_rustfunc ( builtin_min) ) ;
774+ ctx. set_attr ( & py_mod, "object" , ctx. object ( ) ) ;
775+ ctx. set_attr ( & py_mod, "oct" , ctx. new_rustfunc ( builtin_oct) ) ;
776+ ctx. set_attr ( & py_mod, "ord" , ctx. new_rustfunc ( builtin_ord) ) ;
777+ ctx. set_attr ( & py_mod, "next" , ctx. new_rustfunc ( builtin_next) ) ;
778+ ctx. set_attr ( & py_mod, "pow" , ctx. new_rustfunc ( builtin_pow) ) ;
779+ ctx. set_attr ( & py_mod, "print" , ctx. new_rustfunc ( builtin_print) ) ;
780+ ctx. set_attr ( & py_mod, "property" , ctx. property_type ( ) ) ;
781+ ctx. set_attr ( & py_mod, "range" , ctx. new_rustfunc ( builtin_range) ) ;
782+ ctx. set_attr ( & py_mod, "repr" , ctx. new_rustfunc ( builtin_repr) ) ;
783+ ctx. set_attr ( & py_mod, "set" , ctx. set_type ( ) ) ;
784+ ctx. set_attr ( & py_mod, "setattr" , ctx. new_rustfunc ( builtin_setattr) ) ;
785+ ctx. set_attr ( & py_mod, "staticmethod" , ctx. staticmethod_type ( ) ) ;
786+ ctx. set_attr ( & py_mod, "str" , ctx. str_type ( ) ) ;
787+ ctx. set_attr ( & py_mod, "sum" , ctx. new_rustfunc ( builtin_sum) ) ;
788+ ctx. set_attr ( & py_mod, "super" , ctx. super_type ( ) ) ;
789+ ctx. set_attr ( & py_mod, "tuple" , ctx. tuple_type ( ) ) ;
790+ ctx. set_attr ( & py_mod, "type" , ctx. type_type ( ) ) ;
791+ ctx. set_attr ( & py_mod, "zip" , ctx. new_rustfunc ( builtin_zip) ) ;
810792
811793 // Exceptions:
812- dict. insert (
813- String :: from ( "BaseException" ) ,
794+ ctx. set_attr (
795+ & py_mod,
796+ "BaseException" ,
814797 ctx. exceptions . base_exception_type . clone ( ) ,
815798 ) ;
816- dict. insert (
817- String :: from ( "Exception" ) ,
818- ctx. exceptions . exception_type . clone ( ) ,
819- ) ;
820- dict. insert (
821- String :: from ( "AssertionError" ) ,
799+ ctx. set_attr ( & py_mod, "Exception" , ctx. exceptions . exception_type . clone ( ) ) ;
800+ ctx. set_attr (
801+ & py_mod,
802+ "AssertionError" ,
822803 ctx. exceptions . assertion_error . clone ( ) ,
823804 ) ;
824- dict. insert (
825- String :: from ( "AttributeError" ) ,
805+ ctx. set_attr (
806+ & py_mod,
807+ "AttributeError" ,
826808 ctx. exceptions . attribute_error . clone ( ) ,
827809 ) ;
828- dict. insert ( String :: from ( "NameError" ) , ctx. exceptions . name_error . clone ( ) ) ;
829- dict. insert (
830- String :: from ( "RuntimeError" ) ,
810+ ctx. set_attr ( & py_mod, "NameError" , ctx. exceptions . name_error . clone ( ) ) ;
811+ ctx. set_attr (
812+ & py_mod,
813+ "RuntimeError" ,
831814 ctx. exceptions . runtime_error . clone ( ) ,
832815 ) ;
833- dict. insert (
834- String :: from ( "NotImplementedError" ) ,
816+ ctx. set_attr (
817+ & py_mod,
818+ "NotImplementedError" ,
835819 ctx. exceptions . not_implemented_error . clone ( ) ,
836820 ) ;
837- dict. insert ( String :: from ( "TypeError" ) , ctx. exceptions . type_error . clone ( ) ) ;
838- dict. insert (
839- String :: from ( "ValueError" ) ,
840- ctx. exceptions . value_error . clone ( ) ,
841- ) ;
821+ ctx. set_attr ( & py_mod, "TypeError" , ctx. exceptions . type_error . clone ( ) ) ;
822+ ctx. set_attr ( & py_mod, "ValueError" , ctx. exceptions . value_error . clone ( ) ) ;
842823
843- let d2 = PyObject :: new ( PyObjectKind :: Dict { elements : dict } , ctx. type_type ( ) ) ;
844- let scope = PyObject :: new (
845- PyObjectKind :: Scope {
846- scope : Scope {
847- locals : d2,
848- parent : None ,
849- } ,
850- } ,
851- ctx. type_type ( ) ,
852- ) ;
853- let mod_name = "__builtins__" . to_string ( ) ;
854- let py_mod = ctx. new_module ( & mod_name, scope) ;
855824 py_mod
856825}
857826
0 commit comments