Skip to content

Commit 03659c5

Browse files
committed
py/objrange: Fix slicing of range when step of slice is negative.
1 parent e1b0f2a commit 03659c5

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

py/objrange.c

+4
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ STATIC mp_obj_t range_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
154154
o->start = self->start + slice.start * self->step;
155155
o->stop = self->start + slice.stop * self->step;
156156
o->step = slice.step * self->step;
157+
if (slice.step < 0) {
158+
// Negative slice steps have inclusive stop, so adjust for exclusive
159+
o->stop -= self->step;
160+
}
157161
return MP_OBJ_FROM_PTR(o);
158162
}
159163
#endif

0 commit comments

Comments
 (0)