Skip to content

Commit 8ad95c4

Browse files
authored
Merge pull request #326 from lausek/master
int type: rounding, index, trunc, int (#304)
2 parents b87fcdc + 02d9975 commit 8ad95c4

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

tests/snippets/numbers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ class A(int):
1111
assert x == 7
1212
assert type(x) is A
1313

14+
assert int(2).__index__() == 2
15+
assert int(2).__trunc__() == 2
16+
assert int(2).__ceil__() == 2
17+
assert int(2).__floor__() == 2
18+
assert int(2).__round__() == 2
19+
assert int(2).__round__(3) == 2
20+
assert int(-2).__index__() == -2
21+
assert int(-2).__trunc__() == -2
22+
assert int(-2).__ceil__() == -2
23+
assert int(-2).__floor__() == -2
24+
assert int(-2).__round__() == -2
25+
assert int(-2).__round__(3) == -2
26+
27+
assert round(10) == 10
28+
assert round(10, 2) == 10
29+
assert round(10, -1) == 10
30+
1431
assert int(2).__bool__() == True
1532
assert int(0.5).__bool__() == False
1633
assert int(-1).__bool__() == True

vm/src/builtins.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,24 @@ fn builtin_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
588588
vm.to_repr(obj)
589589
}
590590
// builtin_reversed
591-
// builtin_round
591+
592+
fn builtin_round(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
593+
arg_check!(
594+
vm,
595+
args,
596+
required = [(number, Some(vm.ctx.object()))],
597+
optional = [(ndigits, None)]
598+
);
599+
if let Some(ndigits) = ndigits {
600+
let ndigits = vm.call_method(ndigits, "__int__", vec![])?;
601+
let rounded = vm.call_method(number, "__round__", vec![ndigits])?;
602+
Ok(rounded)
603+
} else {
604+
// without a parameter, the result type is coerced to int
605+
let rounded = &vm.call_method(number, "__round__", vec![])?;
606+
Ok(vm.ctx.new_int(objint::get_value(rounded)))
607+
}
608+
}
592609

593610
fn builtin_setattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
594611
arg_check!(
@@ -675,6 +692,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
675692
ctx.set_attr(&py_mod, "property", ctx.property_type());
676693
ctx.set_attr(&py_mod, "range", ctx.range_type());
677694
ctx.set_attr(&py_mod, "repr", ctx.new_rustfunc(builtin_repr));
695+
ctx.set_attr(&py_mod, "round", ctx.new_rustfunc(builtin_round));
678696
ctx.set_attr(&py_mod, "set", ctx.set_type());
679697
ctx.set_attr(&py_mod, "setattr", ctx.new_rustfunc(builtin_setattr));
680698
ctx.set_attr(&py_mod, "slice", ctx.slice_type());

vm/src/obj/objint.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,21 @@ fn int_floordiv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
299299
}
300300
}
301301

302+
fn int_round(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
303+
arg_check!(
304+
vm,
305+
args,
306+
required = [(i, Some(vm.ctx.int_type()))],
307+
optional = [(_precision, None)]
308+
);
309+
Ok(vm.ctx.new_int(get_value(i)))
310+
}
311+
312+
fn int_pass_value(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
313+
arg_check!(vm, args, required = [(i, Some(vm.ctx.int_type()))]);
314+
Ok(vm.ctx.new_int(get_value(i)))
315+
}
316+
302317
fn int_format(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
303318
arg_check!(
304319
vm,
@@ -563,6 +578,12 @@ Base 0 means to interpret the base from the string as an integer literal.
563578
context.set_attr(&int_type, "__and__", context.new_rustfunc(int_and));
564579
context.set_attr(&int_type, "__divmod__", context.new_rustfunc(int_divmod));
565580
context.set_attr(&int_type, "__float__", context.new_rustfunc(int_float));
581+
context.set_attr(&int_type, "__round__", context.new_rustfunc(int_round));
582+
context.set_attr(&int_type, "__ceil__", context.new_rustfunc(int_pass_value));
583+
context.set_attr(&int_type, "__floor__", context.new_rustfunc(int_pass_value));
584+
context.set_attr(&int_type, "__index__", context.new_rustfunc(int_pass_value));
585+
context.set_attr(&int_type, "__trunc__", context.new_rustfunc(int_pass_value));
586+
context.set_attr(&int_type, "__int__", context.new_rustfunc(int_pass_value));
566587
context.set_attr(
567588
&int_type,
568589
"__floordiv__",

0 commit comments

Comments
 (0)