Skip to content

Commit 6e48b13

Browse files
committed
Convert pow
1 parent 5e3d22a commit 6e48b13

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

vm/src/builtins.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -530,38 +530,38 @@ fn builtin_ord(string: Either<PyByteInner, PyStringRef>, vm: &VirtualMachine) ->
530530
}
531531
}
532532

533-
fn builtin_pow(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
534-
arg_check!(
535-
vm,
536-
args,
537-
required = [(x, None), (y, None)],
538-
optional = [(mod_value, Some(vm.ctx.int_type()))]
539-
);
540-
533+
fn builtin_pow(
534+
x: PyObjectRef,
535+
y: PyObjectRef,
536+
mod_value: OptionalArg<PyIntRef>,
537+
vm: &VirtualMachine,
538+
) -> PyResult {
541539
match mod_value {
542-
None => vm.call_or_reflection(x.clone(), y.clone(), "__pow__", "__rpow__", |vm, x, y| {
543-
Err(vm.new_unsupported_operand_error(x, y, "pow"))
544-
}),
545-
Some(m) => {
540+
OptionalArg::Missing => {
541+
vm.call_or_reflection(x.clone(), y.clone(), "__pow__", "__rpow__", |vm, x, y| {
542+
Err(vm.new_unsupported_operand_error(x, y, "pow"))
543+
})
544+
}
545+
OptionalArg::Present(m) => {
546546
// Check if the 3rd argument is defined and perform modulus on the result
547-
if !(objtype::isinstance(x, &vm.ctx.int_type())
548-
&& objtype::isinstance(y, &vm.ctx.int_type()))
547+
if !(objtype::isinstance(&x, &vm.ctx.int_type())
548+
&& objtype::isinstance(&y, &vm.ctx.int_type()))
549549
{
550550
return Err(vm.new_type_error(
551551
"pow() 3rd argument not allowed unless all arguments are integers".to_string(),
552552
));
553553
}
554-
let y = objint::get_value(y);
554+
let y = objint::get_value(&y);
555555
if y.sign() == Sign::Minus {
556556
return Err(vm.new_value_error(
557557
"pow() 2nd argument cannot be negative when 3rd argument specified".to_string(),
558558
));
559559
}
560-
let m = objint::get_value(m);
560+
let m = m.as_bigint();
561561
if m.is_zero() {
562562
return Err(vm.new_value_error("pow() 3rd argument cannot be 0".to_string()));
563563
}
564-
let x = objint::get_value(x);
564+
let x = objint::get_value(&x);
565565
Ok(vm.new_int(x.modpow(&y, &m)))
566566
}
567567
}

0 commit comments

Comments
 (0)