forked from Rust-related/RustPython
Merge pull request #2601 from ishigoya/test_slice
slice: error on negative integers in indices method
This commit is contained in:
@@ -22,8 +22,6 @@ class BaseTestCase(unittest.TestCase):
|
||||
self.assertEqual(operator.index(self.o), -2)
|
||||
self.assertEqual(operator.index(self.n), 2)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_slice(self):
|
||||
self.o.ind = 1
|
||||
self.n.ind = 2
|
||||
|
||||
@@ -155,8 +155,6 @@ class SliceTest(unittest.TestCase):
|
||||
expected = range(length)[slice]
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_indices(self):
|
||||
self.assertEqual(slice(None ).indices(10), (0, 10, 1))
|
||||
self.assertEqual(slice(None, None, 2).indices(10), (0, 10, 2))
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user