@@ -230,12 +230,18 @@ mod array {
230
230
}
231
231
}
232
232
233
- fn index( & self , obj: PyObjectRef , vm: & VirtualMachine ) -> PyResult <usize > {
233
+ fn index(
234
+ & self ,
235
+ obj: PyObjectRef ,
236
+ start: usize ,
237
+ stop: usize ,
238
+ vm: & VirtualMachine
239
+ ) -> PyResult <usize > {
234
240
match self {
235
241
$( ArrayContentType :: $n( v) => {
236
242
if let Ok ( val) = <$t>:: try_into_from_object( vm, obj) {
237
- if let Some ( pos) = v. iter( ) . position( |& a| a == val) {
238
- return Ok ( pos) ;
243
+ if let Some ( pos) = v. iter( ) . take ( stop as _ ) . skip ( start as _ ) . position( |& elem| elem == val) {
244
+ return Ok ( pos + start ) ;
239
245
}
240
246
}
241
247
Err ( vm. new_value_error( "array.index(x): x not in array" . to_owned( ) ) )
@@ -858,8 +864,21 @@ mod array {
858
864
}
859
865
860
866
#[ pymethod]
861
- fn index ( & self , x : PyObjectRef , vm : & VirtualMachine ) -> PyResult < usize > {
862
- self . read ( ) . index ( x, vm)
867
+ fn index (
868
+ & self ,
869
+ x : PyObjectRef ,
870
+ start : OptionalArg < PyObjectRef > ,
871
+ stop : OptionalArg < PyObjectRef > ,
872
+ vm : & VirtualMachine ,
873
+ ) -> PyResult < usize > {
874
+ let len = self . len ( ) ;
875
+ let saturate = |obj : PyObjectRef , len| -> PyResult < _ > {
876
+ obj. try_into_value ( vm)
877
+ . map ( |int : PyIntRef | int. as_bigint ( ) . saturated_at ( len) )
878
+ } ;
879
+ let start = start. map_or ( Ok ( 0 ) , |obj| saturate ( obj, len) ) ?;
880
+ let stop = stop. map_or ( Ok ( len) , |obj| saturate ( obj, len) ) ?;
881
+ self . read ( ) . index ( x, start, stop, vm)
863
882
}
864
883
865
884
#[ pymethod]
0 commit comments