File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -90,12 +90,14 @@ pub unsafe extern "C" fn PyList_Insert(
9090 let list = unsafe { & * list } . try_downcast_ref :: < PyList > ( vm) ?;
9191 let item = unsafe { & * item } . to_owned ( ) ;
9292 let mut vec = list. borrow_vec_mut ( ) ;
93- if index as usize > vec . len ( ) {
94- Err ( vm . new_index_error ( format ! ( "list index out of range: {index}" ) ) )
93+ let index = if index < 0 {
94+ index + vec . len ( ) as isize
9595 } else {
96- vec. insert ( index as _ , item) ;
97- Ok ( ( ) )
96+ index
9897 }
98+ . clamp ( 0 , vec. len ( ) as isize ) as usize ;
99+ vec. insert ( index, item) ;
100+ Ok ( ( ) )
99101 } )
100102}
101103
@@ -167,7 +169,8 @@ mod tests {
167169 assert_eq ! ( list. len( ) , 0 ) ;
168170 list. insert ( 0 , 1 ) . unwrap ( ) ;
169171 assert_eq ! ( list. len( ) , 1 ) ;
170- assert ! ( list. insert( 2 , 3 ) . is_err( ) ) ;
172+ list. insert ( 2 , 3 ) . unwrap ( ) ;
173+ assert_eq ! ( list. get_item( 1 ) . unwrap( ) . extract:: <u32 >( ) . unwrap( ) , 3 ) ;
171174 } )
172175 }
173176
You can’t perform that action at this time.
0 commit comments