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:
2021-08-05 16:44:03 +10:00
parent 2b51124d6a
commit d8582d966f
4 changed files with 10 additions and 0 deletions

View File

@@ -2825,9 +2825,11 @@ static PyObject *pyrna_prop_collection_subscript(BPy_PropertyRNA *self, PyObject
const 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);
}
}
@@ -2955,9 +2957,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);
}
}