Merge pull request #297 from HomerMcMillan/issue_295

Fix panic on out of bounds negative index (addresses #295)
This commit is contained in:
Windel Bouwman
2019-02-04 07:48:17 +01:00
committed by GitHub

View File

@@ -13,7 +13,12 @@ pub trait PySliceableSequence {
fn len(&self) -> usize;
fn get_pos(&self, p: i32) -> usize {
if p < 0 {
self.len() - ((-p) as usize)
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 {