Skip to content

Commit 3a9c036

Browse files
committed
Adding 'int.__bool__' method
1 parent 2b22cd4 commit 3a9c036

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

tests/snippets/numbers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ class A(int):
88
x = A(7)
99
assert x == 7
1010
assert type(x) is A
11+
12+
assert int(2).__bool__() == True

vm/src/obj/objint.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ impl FromPyObjectRef for BigInt {
9090
}
9191
}
9292

93+
94+
fn int_bool(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
95+
arg_check!(
96+
vm,
97+
args,
98+
required=[(zelf, Some(vm.ctx.int_type()))]
99+
);
100+
Ok(vm.ctx.new_bool(true))
101+
}
93102
fn int_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
94103
arg_check!(
95104
vm,
@@ -527,6 +536,7 @@ pub fn init(context: &PyContext) {
527536
context.set_attr(&int_type, "__format__", context.new_rustfunc(int_format));
528537
context.set_attr(&int_type, "__truediv__", context.new_rustfunc(int_truediv));
529538
context.set_attr(&int_type, "__xor__", context.new_rustfunc(int_xor));
539+
context.set_attr(&int_type, "__bool__", context.new_rustfunc(int_bool));
530540
context.set_attr(
531541
&int_type,
532542
"bit_length",

0 commit comments

Comments
 (0)