@@ -665,9 +665,7 @@ fn builtin_repr(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyStringRef>
665
665
vm. to_repr ( & obj)
666
666
}
667
667
668
- fn builtin_reversed ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
669
- arg_check ! ( vm, args, required = [ ( obj, None ) ] ) ;
670
-
668
+ fn builtin_reversed ( obj : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
671
669
if let Some ( reversed_method) = vm. get_method ( obj. clone ( ) , "__reversed__" ) {
672
670
vm. invoke ( & reversed_method?, PyFuncArgs :: default ( ) )
673
671
} else {
@@ -684,31 +682,28 @@ fn builtin_reversed(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
684
682
}
685
683
}
686
684
687
- fn builtin_round ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
688
- arg_check ! (
689
- vm,
690
- args,
691
- required = [ ( number, Some ( vm. ctx. object( ) ) ) ] ,
692
- optional = [ ( ndigits, None ) ]
693
- ) ;
694
- if let Some ( ndigits) = ndigits {
695
- if objtype:: isinstance ( ndigits, & vm. ctx . int_type ( ) ) {
696
- let ndigits = vm. call_method ( ndigits, "__int__" , vec ! [ ] ) ?;
697
- let rounded = vm. call_method ( number, "__round__" , vec ! [ ndigits] ) ?;
698
- Ok ( rounded)
699
- } else if vm. ctx . none ( ) . is ( ndigits) {
700
- let rounded = & vm. call_method ( number, "__round__" , vec ! [ ] ) ?;
685
+ fn builtin_round (
686
+ number : PyObjectRef ,
687
+ ndigits : OptionalArg < Option < PyIntRef > > ,
688
+ vm : & VirtualMachine ,
689
+ ) -> PyResult {
690
+ match ndigits {
691
+ OptionalArg :: Present ( ndigits) => match ndigits {
692
+ Some ( int) => {
693
+ let ndigits = vm. call_method ( int. as_object ( ) , "__int__" , vec ! [ ] ) ?;
694
+ let rounded = vm. call_method ( & number, "__round__" , vec ! [ ndigits] ) ?;
695
+ Ok ( rounded)
696
+ }
697
+ None => {
698
+ let rounded = & vm. call_method ( & number, "__round__" , vec ! [ ] ) ?;
699
+ Ok ( vm. ctx . new_int ( objint:: get_value ( rounded) . clone ( ) ) )
700
+ }
701
+ } ,
702
+ OptionalArg :: Missing => {
703
+ // without a parameter, the result type is coerced to int
704
+ let rounded = & vm. call_method ( & number, "__round__" , vec ! [ ] ) ?;
701
705
Ok ( vm. ctx . new_int ( objint:: get_value ( rounded) . clone ( ) ) )
702
- } else {
703
- Err ( vm. new_type_error ( format ! (
704
- "'{}' object cannot be interpreted as an integer" ,
705
- ndigits. class( ) . name
706
- ) ) )
707
706
}
708
- } else {
709
- // without a parameter, the result type is coerced to int
710
- let rounded = & vm. call_method ( number, "__round__" , vec ! [ ] ) ?;
711
- Ok ( vm. ctx . new_int ( objint:: get_value ( rounded) . clone ( ) ) )
712
707
}
713
708
}
714
709
0 commit comments