Skip to content

Commit 02d9975

Browse files
committed
round() for int
1 parent 3d07ecd commit 02d9975

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

tests/snippets/numbers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ class A(int):
1515
assert int(2).__floor__() == 2
1616
assert int(2).__round__() == 2
1717
assert int(2).__round__(3) == 2
18+
assert int(-2).__index__() == -2
19+
assert int(-2).__trunc__() == -2
20+
assert int(-2).__ceil__() == -2
21+
assert int(-2).__floor__() == -2
22+
assert int(-2).__round__() == -2
23+
assert int(-2).__round__(3) == -2
24+
25+
assert round(10) == 10
26+
assert round(10, 2) == 10
27+
assert round(10, -1) == 10
1828

1929
assert int(2).__bool__() == True
2030
assert int(0.5).__bool__() == False

vm/src/builtins.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,24 @@ fn builtin_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
664664
vm.to_repr(obj)
665665
}
666666
// builtin_reversed
667-
// builtin_round
667+
668+
fn builtin_round(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
669+
arg_check!(
670+
vm,
671+
args,
672+
required = [(number, Some(vm.ctx.object()))],
673+
optional = [(ndigits, None)]
674+
);
675+
if let Some(ndigits) = ndigits {
676+
let ndigits = vm.call_method(ndigits, "__int__", vec![])?;
677+
let rounded = vm.call_method(number, "__round__", vec![ndigits])?;
678+
Ok(rounded)
679+
} else {
680+
// without a parameter, the result type is coerced to int
681+
let rounded = &vm.call_method(number, "__round__", vec![])?;
682+
Ok(vm.ctx.new_int(objint::get_value(rounded)))
683+
}
684+
}
668685

669686
fn builtin_setattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
670687
arg_check!(
@@ -777,6 +794,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
777794
ctx.set_attr(&py_mod, "property", ctx.property_type());
778795
ctx.set_attr(&py_mod, "range", ctx.range_type());
779796
ctx.set_attr(&py_mod, "repr", ctx.new_rustfunc(builtin_repr));
797+
ctx.set_attr(&py_mod, "round", ctx.new_rustfunc(builtin_round));
780798
ctx.set_attr(&py_mod, "set", ctx.set_type());
781799
ctx.set_attr(&py_mod, "setattr", ctx.new_rustfunc(builtin_setattr));
782800
ctx.set_attr(&py_mod, "staticmethod", ctx.staticmethod_type());

vm/src/obj/objint.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ pub fn init(context: &PyContext) {
532532
context.set_attr(&int_type, "__floor__", context.new_rustfunc(int_pass_value));
533533
context.set_attr(&int_type, "__index__", context.new_rustfunc(int_pass_value));
534534
context.set_attr(&int_type, "__trunc__", context.new_rustfunc(int_pass_value));
535+
context.set_attr(&int_type, "__int__", context.new_rustfunc(int_pass_value));
535536
context.set_attr(
536537
&int_type,
537538
"__floordiv__",

0 commit comments

Comments
 (0)