Fix slicing with negative indices

Negative indices that remained negative after adding the sequence length
caused incorrect slicing.

With the default scene for example:

   bpy.context.scene.objects[-4:2]

Gave a different result to:

   tuple(bpy.context.scene.objects)[-4:2]

Clamp indices above zero so loops that step forward works as intended.
This commit is contained in:
Campbell Barton 2021-08-05 16:44:03 +10:00 committed by Jeroen Bakker
parent 6d1895c681
commit 120292a190
Notes: blender-bot 2023-02-14 06:27:47 +01:00
Referenced by issue #77348, Blender LTS: Maintenance Task 2.83
4 changed files with 10 additions and 0 deletions

View File

@ -3290,9 +3290,11 @@ static PyObject *bpy_bmelemseq_subscript(BPy_BMElemSeq *self, PyObject *key)
Py_ssize_t len = bpy_bmelemseq_length(self);
if (start < 0) {
start += len;
CLAMP_MIN(start, 0);
}
if (stop < 0) {
stop += len;
CLAMP_MIN(stop, 0);
}
}

View File

@ -786,9 +786,11 @@ static PyObject *bpy_bmlayercollection_subscript(BPy_BMLayerCollection *self, Py
Py_ssize_t len = bpy_bmlayercollection_length(self);
if (start < 0) {
start += len;
CLAMP_MIN(start, 0);
}
if (stop < 0) {
stop += len;
CLAMP_MIN(stop, 0);
}
}

View File

@ -284,9 +284,11 @@ static PyObject *bpy_bmeditselseq_subscript(BPy_BMEditSelSeq *self, PyObject *ke
Py_ssize_t len = bpy_bmeditselseq_length(self);
if (start < 0) {
start += len;
CLAMP_MIN(start, 0);
}
if (stop < 0) {
stop += len;
CLAMP_MIN(stop, 0);
}
}

View File

@ -2745,9 +2745,11 @@ static PyObject *pyrna_prop_collection_subscript(BPy_PropertyRNA *self, PyObject
Py_ssize_t len = (Py_ssize_t)RNA_property_collection_length(&self->ptr, self->prop);
if (start < 0) {
start += len;
CLAMP_MIN(start, 0);
}
if (stop < 0) {
stop += len;
CLAMP_MIN(stop, 0);
}
}
@ -2880,9 +2882,11 @@ static int pyrna_prop_collection_ass_subscript(BPy_PropertyRNA *self,
Py_ssize_t len = (Py_ssize_t)RNA_property_collection_length(&self->ptr, self->prop);
if (start < 0) {
start += len;
CLAMP_MIN(start, 0);
}
if (stop < 0) {
stop += len;
CLAMP_MIN(stop, 0);
}
}