From 03659c51ca70dbf89d97cc9c68725a687d3841d1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 May 2017 10:33:23 +1000 Subject: [PATCH] py/objrange: Fix slicing of range when step of slice is negative. --- py/objrange.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/py/objrange.c b/py/objrange.c index c78c53f11c..8c4e14f49c 100644 --- a/py/objrange.c +++ b/py/objrange.c @@ -154,6 +154,10 @@ STATIC mp_obj_t range_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { o->start = self->start + slice.start * self->step; o->stop = self->start + slice.stop * self->step; o->step = slice.step * self->step; + if (slice.step < 0) { + // Negative slice steps have inclusive stop, so adjust for exclusive + o->stop -= self->step; + } return MP_OBJ_FROM_PTR(o); } #endif