Skip to content

Commit e61adb4

Browse files
committed
Convert reversed, round
1 parent 6e48b13 commit e61adb4

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

vm/src/builtins.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,7 @@ fn builtin_repr(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyStringRef>
665665
vm.to_repr(&obj)
666666
}
667667

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 {
671669
if let Some(reversed_method) = vm.get_method(obj.clone(), "__reversed__") {
672670
vm.invoke(&reversed_method?, PyFuncArgs::default())
673671
} else {
@@ -684,31 +682,28 @@ fn builtin_reversed(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
684682
}
685683
}
686684

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![])?;
701705
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-
)))
707706
}
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()))
712707
}
713708
}
714709

0 commit comments

Comments
 (0)