Skip to content

Commit d32e550

Browse files
committed
Add optional arguments to list.index()
More or less directly translated from the CPython implementation.
1 parent 28ea92c commit d32e550

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

vm/src/builtins/list.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,35 @@ impl PyList {
271271
}
272272

273273
#[pymethod]
274-
fn index(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
275-
for (index, element) in self.borrow_value().clone().iter().enumerate() {
274+
fn index(
275+
&self,
276+
needle: PyObjectRef,
277+
start: OptionalArg<isize>,
278+
stop: OptionalArg<isize>,
279+
vm: &VirtualMachine,
280+
) -> PyResult<usize> {
281+
let mut start = start.into_option().unwrap_or(0);
282+
if start < 0 {
283+
start += self.borrow_value().len() as isize;
284+
if start < 0 {
285+
start = 0;
286+
}
287+
}
288+
let mut stop = stop.into_option().unwrap_or(isize::MAX);
289+
if stop < 0 {
290+
stop += self.borrow_value().len() as isize;
291+
if stop < 0 {
292+
stop = 0;
293+
}
294+
}
295+
for (index, element) in self
296+
.borrow_value()
297+
.clone()
298+
.iter()
299+
.enumerate()
300+
.take(stop as usize)
301+
.skip(start as usize)
302+
{
276303
if vm.identical_or_equal(element, &needle)? {
277304
return Ok(index);
278305
}

0 commit comments

Comments
 (0)