diff options
author | Campbell Barton <ideasman42@gmail.com> | 2021-08-05 09:44:03 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2021-08-05 09:44:03 +0300 |
commit | d8582d966fe4ae46c82fcbb69d3d0ca62238ea93 (patch) | |
tree | 509b2691a506ff69786bf0ef453a306862f2934d /source/blender | |
parent | 2b51124d6a59bedf00397d4ef2fd4442c4fddd22 (diff) |
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.
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/python/bmesh/bmesh_py_types.c | 2 | ||||
-rw-r--r-- | source/blender/python/bmesh/bmesh_py_types_customdata.c | 2 | ||||
-rw-r--r-- | source/blender/python/bmesh/bmesh_py_types_select.c | 2 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 4 |
4 files changed, 10 insertions, 0 deletions
diff --git a/source/blender/python/bmesh/bmesh_py_types.c b/source/blender/python/bmesh/bmesh_py_types.c index 612446e6d19..e5e601e0eb6 100644 --- a/source/blender/python/bmesh/bmesh_py_types.c +++ b/source/blender/python/bmesh/bmesh_py_types.c @@ -3241,9 +3241,11 @@ static PyObject *bpy_bmelemseq_subscript(BPy_BMElemSeq *self, PyObject *key) const 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); } } diff --git a/source/blender/python/bmesh/bmesh_py_types_customdata.c b/source/blender/python/bmesh/bmesh_py_types_customdata.c index ff06cf43026..0aa92158524 100644 --- a/source/blender/python/bmesh/bmesh_py_types_customdata.c +++ b/source/blender/python/bmesh/bmesh_py_types_customdata.c @@ -843,9 +843,11 @@ static PyObject *bpy_bmlayercollection_subscript(BPy_BMLayerCollection *self, Py const 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); } } diff --git a/source/blender/python/bmesh/bmesh_py_types_select.c b/source/blender/python/bmesh/bmesh_py_types_select.c index 99f17bcec8f..b89822a080c 100644 --- a/source/blender/python/bmesh/bmesh_py_types_select.c +++ b/source/blender/python/bmesh/bmesh_py_types_select.c @@ -273,9 +273,11 @@ static PyObject *bpy_bmeditselseq_subscript(BPy_BMEditSelSeq *self, PyObject *ke const 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); } } diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index dff96f74d62..ec2e70ad262 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -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); } } |