Skip to content

Commit 1101b65

Browse files
committed
fix error related to RustPython#746 but for list.__delitem__
1 parent e7e126e commit 1101b65

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

tests/snippets/list.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,11 @@ def bad_del_1():
201201
def bad_del_2():
202202
del ['a', 'b'][2]
203203
assert_raises(IndexError, bad_del_2)
204+
205+
# is step != 1 and start or stop of slice == -1
206+
x = list(range(10))
207+
del x[-1:-5:-1]
208+
assert x == [0, 1, 2, 3, 4, 5]
209+
x = list(range(10))
210+
del x[-5:-1:-1]
211+
assert x == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

vm/src/obj/objlist.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33

44
use std::ops::Range;
55

6-
use num_bigint::BigInt;
6+
use num_bigint::{BigInt, ToBigInt};
77
use num_traits::{One, Signed, ToPrimitive, Zero};
88

99
use crate::function::{OptionalArg, PyFuncArgs};
@@ -411,8 +411,20 @@ impl PyListRef {
411411
} else {
412412
// calculate the range for the reverse slice, first the bounds needs to be made
413413
// exclusive around stop, the lower number
414-
let start = start.as_ref().map(|x| x + 1);
415-
let stop = stop.as_ref().map(|x| x + 1);
414+
let start = start.as_ref().map(|x| {
415+
if *x == (-1).to_bigint().unwrap() {
416+
self.get_len() + BigInt::one() //.to_bigint().unwrap()
417+
} else {
418+
x + 1
419+
}
420+
});
421+
let stop = stop.as_ref().map(|x| {
422+
if *x == (-1).to_bigint().unwrap() {
423+
self.get_len().to_bigint().unwrap()
424+
} else {
425+
x + 1
426+
}
427+
});
416428
let range = self.get_slice_range(&stop, &start);
417429
if range.start < range.end {
418430
match (-step).to_i32() {

0 commit comments

Comments
 (0)