@@ -15,11 +15,13 @@ use crate::obj::objdict;
1515use crate :: obj:: objint;
1616use crate :: obj:: objiter;
1717use crate :: obj:: objstr:: { self , PyStringRef } ;
18- use crate :: obj:: objtype;
18+ use crate :: obj:: objtype:: { self , PyClassRef } ;
1919
2020use crate :: frame:: Scope ;
2121use crate :: function:: { Args , OptionalArg , PyFuncArgs } ;
22- use crate :: pyobject:: { DictProtocol , IdProtocol , PyContext , PyObjectRef , PyResult , TypeProtocol } ;
22+ use crate :: pyobject:: {
23+ DictProtocol , IdProtocol , PyContext , PyObjectRef , PyResult , TryFromObject , TypeProtocol ,
24+ } ;
2325use crate :: vm:: VirtualMachine ;
2426
2527#[ cfg( not( target_arch = "wasm32" ) ) ]
@@ -295,7 +297,7 @@ fn builtin_format(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
295297}
296298
297299fn catch_attr_exception < T > ( ex : PyObjectRef , default : T , vm : & VirtualMachine ) -> PyResult < T > {
298- if objtype:: isinstance ( & ex, vm. ctx . exceptions . attribute_error . as_object ( ) ) {
300+ if objtype:: isinstance ( & ex, & vm. ctx . exceptions . attribute_error ) {
299301 Ok ( default)
300302 } else {
301303 Err ( ex)
@@ -357,15 +359,8 @@ fn builtin_id(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
357359
358360// builtin_input
359361
360- fn builtin_isinstance ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
361- arg_check ! (
362- vm,
363- args,
364- required = [ ( obj, None ) , ( typ, Some ( vm. get_type( ) ) ) ]
365- ) ;
366-
367- let isinstance = vm. isinstance ( obj, typ) ?;
368- Ok ( vm. new_bool ( isinstance) )
362+ fn builtin_isinstance ( obj : PyObjectRef , typ : PyClassRef , vm : & VirtualMachine ) -> PyResult < bool > {
363+ vm. isinstance ( & obj, & typ)
369364}
370365
371366fn builtin_issubclass ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
@@ -510,7 +505,7 @@ fn builtin_next(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
510505 match vm. call_method ( iterator, "__next__" , vec ! [ ] ) {
511506 Ok ( value) => Ok ( value) ,
512507 Err ( value) => {
513- if objtype:: isinstance ( & value, vm. ctx . exceptions . stop_iteration . as_object ( ) ) {
508+ if objtype:: isinstance ( & value, & vm. ctx . exceptions . stop_iteration ) {
514509 match default_value {
515510 None => Err ( value) ,
516511 Some ( value) => Ok ( value. clone ( ) ) ,
@@ -719,64 +714,64 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
719714 "all" => ctx. new_rustfunc( builtin_all) ,
720715 "any" => ctx. new_rustfunc( builtin_any) ,
721716 "bin" => ctx. new_rustfunc( builtin_bin) ,
722- "bool" => ctx. bool_type( ) ,
723- "bytearray" => ctx. bytearray_type( ) ,
724- "bytes" => ctx. bytes_type( ) ,
717+ "bool" => ctx. bool_type( ) . into_object ( ) ,
718+ "bytearray" => ctx. bytearray_type( ) . into_object ( ) ,
719+ "bytes" => ctx. bytes_type( ) . into_object ( ) ,
725720 "callable" => ctx. new_rustfunc( builtin_callable) ,
726721 "chr" => ctx. new_rustfunc( builtin_chr) ,
727- "classmethod" => ctx. classmethod_type( ) ,
722+ "classmethod" => ctx. classmethod_type( ) . into_object ( ) ,
728723 "compile" => ctx. new_rustfunc( builtin_compile) ,
729- "complex" => ctx. complex_type( ) ,
724+ "complex" => ctx. complex_type( ) . into_object ( ) ,
730725 "delattr" => ctx. new_rustfunc( builtin_delattr) ,
731- "dict" => ctx. dict_type( ) ,
726+ "dict" => ctx. dict_type( ) . into_object ( ) ,
732727 "divmod" => ctx. new_rustfunc( builtin_divmod) ,
733728 "dir" => ctx. new_rustfunc( builtin_dir) ,
734- "enumerate" => ctx. enumerate_type( ) ,
729+ "enumerate" => ctx. enumerate_type( ) . into_object ( ) ,
735730 "eval" => ctx. new_rustfunc( builtin_eval) ,
736731 "exec" => ctx. new_rustfunc( builtin_exec) ,
737- "float" => ctx. float_type( ) ,
738- "frozenset" => ctx. frozenset_type( ) ,
739- "filter" => ctx. filter_type( ) ,
732+ "float" => ctx. float_type( ) . into_object ( ) ,
733+ "frozenset" => ctx. frozenset_type( ) . into_object ( ) ,
734+ "filter" => ctx. filter_type( ) . into_object ( ) ,
740735 "format" => ctx. new_rustfunc( builtin_format) ,
741736 "getattr" => ctx. new_rustfunc( builtin_getattr) ,
742737 "globals" => ctx. new_rustfunc( builtin_globals) ,
743738 "hasattr" => ctx. new_rustfunc( builtin_hasattr) ,
744739 "hash" => ctx. new_rustfunc( builtin_hash) ,
745740 "hex" => ctx. new_rustfunc( builtin_hex) ,
746741 "id" => ctx. new_rustfunc( builtin_id) ,
747- "int" => ctx. int_type( ) ,
742+ "int" => ctx. int_type( ) . into_object ( ) ,
748743 "isinstance" => ctx. new_rustfunc( builtin_isinstance) ,
749744 "issubclass" => ctx. new_rustfunc( builtin_issubclass) ,
750745 "iter" => ctx. new_rustfunc( builtin_iter) ,
751746 "len" => ctx. new_rustfunc( builtin_len) ,
752- "list" => ctx. list_type( ) ,
747+ "list" => ctx. list_type( ) . into_object ( ) ,
753748 "locals" => ctx. new_rustfunc( builtin_locals) ,
754- "map" => ctx. map_type( ) ,
749+ "map" => ctx. map_type( ) . into_object ( ) ,
755750 "max" => ctx. new_rustfunc( builtin_max) ,
756- "memoryview" => ctx. memoryview_type( ) ,
751+ "memoryview" => ctx. memoryview_type( ) . into_object ( ) ,
757752 "min" => ctx. new_rustfunc( builtin_min) ,
758- "object" => ctx. object( ) ,
753+ "object" => ctx. object( ) . into_object ( ) ,
759754 "oct" => ctx. new_rustfunc( builtin_oct) ,
760755 "ord" => ctx. new_rustfunc( builtin_ord) ,
761756 "next" => ctx. new_rustfunc( builtin_next) ,
762757 "pow" => ctx. new_rustfunc( builtin_pow) ,
763758 "print" => ctx. new_rustfunc( builtin_print) ,
764- "property" => ctx. property_type( ) ,
765- "range" => ctx. range_type( ) ,
759+ "property" => ctx. property_type( ) . into_object ( ) ,
760+ "range" => ctx. range_type( ) . into_object ( ) ,
766761 "repr" => ctx. new_rustfunc( builtin_repr) ,
767762 "reversed" => ctx. new_rustfunc( builtin_reversed) ,
768763 "round" => ctx. new_rustfunc( builtin_round) ,
769- "set" => ctx. set_type( ) ,
764+ "set" => ctx. set_type( ) . into_object ( ) ,
770765 "setattr" => ctx. new_rustfunc( builtin_setattr) ,
771766 "sorted" => ctx. new_rustfunc( builtin_sorted) ,
772- "slice" => ctx. slice_type( ) ,
773- "staticmethod" => ctx. staticmethod_type( ) ,
774- "str" => ctx. str_type( ) ,
767+ "slice" => ctx. slice_type( ) . into_object ( ) ,
768+ "staticmethod" => ctx. staticmethod_type( ) . into_object ( ) ,
769+ "str" => ctx. str_type( ) . into_object ( ) ,
775770 "sum" => ctx. new_rustfunc( builtin_sum) ,
776- "super" => ctx. super_type( ) ,
777- "tuple" => ctx. tuple_type( ) ,
778- "type" => ctx. type_type( ) ,
779- "zip" => ctx. zip_type( ) ,
771+ "super" => ctx. super_type( ) . into_object ( ) ,
772+ "tuple" => ctx. tuple_type( ) . into_object ( ) ,
773+ "type" => ctx. type_type( ) . into_object ( ) ,
774+ "zip" => ctx. zip_type( ) . into_object ( ) ,
780775 "__import__" => ctx. new_rustfunc( builtin_import) ,
781776
782777 // Constants
@@ -813,26 +808,34 @@ pub fn builtin_build_class_(vm: &VirtualMachine, mut args: PyFuncArgs) -> PyResu
813808 let function = args. shift ( ) ;
814809 let name_arg = args. shift ( ) ;
815810 let bases = args. args . clone ( ) ;
816- let mut metaclass = args. get_kwarg ( "metaclass" , vm. get_type ( ) ) ;
811+ let mut metaclass = if let Some ( metaclass) = args. get_optional_kwarg ( "metaclass" ) {
812+ PyClassRef :: try_from_object ( vm, metaclass) ?
813+ } else {
814+ vm. get_type ( )
815+ } ;
817816
818817 for base in bases. clone ( ) {
819- if objtype:: issubclass ( & base. typ ( ) , & metaclass) {
820- metaclass = base. typ ( ) ;
821- } else if !objtype:: issubclass ( & metaclass, & base. typ ( ) ) {
818+ if objtype:: issubclass ( & base. type_pyref ( ) , & metaclass) {
819+ metaclass = base. type_pyref ( ) ;
820+ } else if !objtype:: issubclass ( & metaclass, & base. type_pyref ( ) ) {
822821 return Err ( vm. new_type_error ( "metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases" . to_string ( ) ) ) ;
823822 }
824823 }
825824
826825 let bases = vm. context ( ) . new_tuple ( bases) ;
827826
828827 // Prepare uses full __getattribute__ resolution chain.
829- let prepare = vm. get_attribute ( metaclass. clone ( ) , "__prepare__" ) ?;
828+ let prepare = vm. get_attribute ( metaclass. clone ( ) . into_object ( ) , "__prepare__" ) ?;
830829 let namespace = vm. invoke ( prepare, vec ! [ name_arg. clone( ) , bases. clone( ) ] ) ?;
831830
832831 let cells = vm. new_dict ( ) ;
833832
834833 vm. invoke_with_locals ( function, cells. clone ( ) , namespace. clone ( ) ) ?;
835- let class = vm. call_method ( & metaclass, "__call__" , vec ! [ name_arg, bases, namespace] ) ?;
834+ let class = vm. call_method (
835+ metaclass. as_object ( ) ,
836+ "__call__" ,
837+ vec ! [ name_arg, bases, namespace] ,
838+ ) ?;
836839 cells. set_item ( & vm. ctx , "__class__" , class. clone ( ) ) ;
837840 Ok ( class)
838841}
0 commit comments