Merge pull request #2601 from ishigoya/test_slice

slice: error on negative integers in indices method
This commit is contained in:
Jeong YunWon
2021-05-08 03:57:55 +09:00
committed by GitHub
3 changed files with 7 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
// sliceobject.{h,c} in CPython
use super::int::PyInt;
use super::int::{PyInt, PyIntRef};
use super::pytype::PyTypeRef;
use crate::function::{FuncArgs, OptionalArg};
use crate::pyobject::{
@@ -205,16 +205,17 @@ impl PySlice {
}
#[pymethod(name = "indices")]
fn indices(&self, length: PyObjectRef, vm: &VirtualMachine) -> PyResult {
if let Some(length) = length.payload::<PyInt>() {
let (start, stop, step) = self.inner_indices(length.borrow_value(), vm)?;
fn indices(&self, length: PyIntRef, vm: &VirtualMachine) -> PyResult {
let length = length.borrow_value();
if length.is_negative() {
Err(vm.new_value_error("length should not be negative.".to_owned()))
} else {
let (start, stop, step) = self.inner_indices(length, vm)?;
Ok(vm.ctx.new_tuple(vec![
vm.ctx.new_int(start),
vm.ctx.new_int(stop),
vm.ctx.new_int(step),
]))
} else {
Ok(vm.ctx.not_implemented())
}
}
}