Fix panic on out of bounds negative index (addresses
Also an out of bounds index now raises an IndexError rather than a ValueError
@@ -13,7 +13,12 @@ pub trait PySliceableSequence {
fn len(&self) -> usize;
fn get_pos(&self, p: i32) -> usize {
if p < 0 {
+ if -p as usize > self.len() {
+ // return something that is out of bounds so get_item raises an IndexError
+ self.len() + 1
+ } else {
self.len() - ((-p) as usize)
+ }
} else if p as usize > self.len() {
// This is for the slicing case where the end element is greater than the length of the
// sequence
@@ -78,8 +83,8 @@ pub fn get_item(
let obj = elements[pos_index].clone();
Ok(obj)
} else {
- let value_error = vm.context().exceptions.value_error.clone();
- Err(vm.new_exception(value_error, "Index out of bounds!".to_string()))
+ let index_error = vm.context().exceptions.index_error.clone();
+ Err(vm.new_exception(index_error, "Index out of bounds!".to_string()))
}
}
PyObjectPayload::Slice {
