Skip to content

Commit ced829f

Browse files
committed
Fix problem that cannot check overflow in len function.
1 parent 5c88280 commit ced829f

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Lib/test/test_builtin.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,6 @@ class E:
774774
self.assertRaises(TypeError, issubclass, E, 'foo')
775775
self.assertRaises(TypeError, issubclass)
776776

777-
# TODO: RUSTPYTHON
778-
@unittest.expectedFailure
779777
def test_len(self):
780778
self.assertEqual(len('123'), 3)
781779
self.assertEqual(len(()), 0)

vm/src/obj/objsequence.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,14 @@ pub fn opt_len(obj: &PyObjectRef, vm: &VirtualMachine) -> Option<PyResult<usize>
291291
if len.is_negative() {
292292
return Err(vm.new_value_error("__len__() should return >= 0".to_owned()));
293293
}
294-
len.to_usize().ok_or_else(|| {
295-
vm.new_overflow_error("cannot fit __len__() result into usize".to_owned())
296-
})
294+
let len = if let Some(len) = len.to_isize() {
295+
len
296+
} else {
297+
return Err(
298+
vm.new_overflow_error("cannot fit 'int' into an index-sized integer".to_owned())
299+
);
300+
};
301+
Ok(len as usize)
297302
})
298303
}
299304

0 commit comments

Comments
 (0)