Skip to content

Commit 21821b7

Browse files
committed
Throw ValueError when __len__() is a negative value
1 parent 7b98092 commit 21821b7

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

vm/src/obj/objbool.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use num_bigint::Sign;
12
use num_traits::Zero;
23

34
use crate::function::PyFuncArgs;
@@ -51,7 +52,16 @@ pub fn boolval(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<bool> {
5152
let method = method_or_err?;
5253
let bool_obj = vm.invoke(&method, PyFuncArgs::default())?;
5354
match bool_obj.payload::<PyInt>() {
54-
Some(int_obj) => !int_obj.as_bigint().is_zero(),
55+
Some(int_obj) => {
56+
let len_val = int_obj.as_bigint();
57+
if len_val.sign() == Sign::Minus {
58+
return Err(
59+
vm.new_value_error("__len__() should return >= 0".to_string())
60+
);
61+
}
62+
63+
!len_val.is_zero()
64+
}
5565
None => {
5666
return Err(vm.new_type_error(format!(
5767
"{} object cannot be interpreted as integer",

0 commit comments

Comments
 (0)