From 8f439bdc2de1f964c8037448796a3f03a9cce4fe Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 8 Nov 2022 12:03:38 +1100 Subject: Fix invalid function signatures for PySequenceMethods callbacks Function casts hid casting between potentially incompatible type signatures (using int instead of Py_ssize_t). As it happens this seems not to have caused any bugs on supported platforms so this change is mainly for correctness and to avoid problems in the future. --- source/blender/python/bmesh/bmesh_py_types.c | 2 +- .../python/bmesh/bmesh_py_types_customdata.c | 3 ++- .../blender/python/bmesh/bmesh_py_types_meshdata.c | 2 +- .../blender/python/bmesh/bmesh_py_types_select.c | 2 +- source/blender/python/generic/bgl.c | 28 +++++++++++----------- source/blender/python/generic/idprop_py_api.c | 6 ++--- source/blender/python/gpu/gpu_py_buffer.c | 12 +++++----- source/blender/python/intern/bpy_rna.c | 4 ++-- source/blender/python/mathutils/mathutils_Color.c | 6 ++--- source/blender/python/mathutils/mathutils_Euler.c | 6 ++--- source/blender/python/mathutils/mathutils_Matrix.c | 12 +++++----- .../python/mathutils/mathutils_Quaternion.c | 6 ++--- source/blender/python/mathutils/mathutils_Vector.c | 6 ++--- 13 files changed, 48 insertions(+), 47 deletions(-) (limited to 'source/blender/python') diff --git a/source/blender/python/bmesh/bmesh_py_types.c b/source/blender/python/bmesh/bmesh_py_types.c index 364adb5458b..5c9c83f2e6e 100644 --- a/source/blender/python/bmesh/bmesh_py_types.c +++ b/source/blender/python/bmesh/bmesh_py_types.c @@ -3118,7 +3118,7 @@ static Py_ssize_t bpy_bmelemseq_length(BPy_BMElemSeq *self) } } -static PyObject *bpy_bmelemseq_subscript_int(BPy_BMElemSeq *self, int keynum) +static PyObject *bpy_bmelemseq_subscript_int(BPy_BMElemSeq *self, Py_ssize_t keynum) { BPY_BM_CHECK_OBJ(self); diff --git a/source/blender/python/bmesh/bmesh_py_types_customdata.c b/source/blender/python/bmesh/bmesh_py_types_customdata.c index 58bfb922327..00b8f579021 100644 --- a/source/blender/python/bmesh/bmesh_py_types_customdata.c +++ b/source/blender/python/bmesh/bmesh_py_types_customdata.c @@ -740,7 +740,8 @@ static PyObject *bpy_bmlayercollection_subscript_str(BPy_BMLayerCollection *self return NULL; } -static PyObject *bpy_bmlayercollection_subscript_int(BPy_BMLayerCollection *self, int keynum) +static PyObject *bpy_bmlayercollection_subscript_int(BPy_BMLayerCollection *self, + Py_ssize_t keynum) { Py_ssize_t len; BPY_BM_CHECK_OBJ(self); diff --git a/source/blender/python/bmesh/bmesh_py_types_meshdata.c b/source/blender/python/bmesh/bmesh_py_types_meshdata.c index 9f200734786..fc2e70221a0 100644 --- a/source/blender/python/bmesh/bmesh_py_types_meshdata.c +++ b/source/blender/python/bmesh/bmesh_py_types_meshdata.c @@ -391,7 +391,7 @@ typedef struct BPy_BMDeformVert { /* Mapping Protocols * ================= */ -static int bpy_bmdeformvert_len(BPy_BMDeformVert *self) +static Py_ssize_t bpy_bmdeformvert_len(BPy_BMDeformVert *self) { return self->data->totweight; } diff --git a/source/blender/python/bmesh/bmesh_py_types_select.c b/source/blender/python/bmesh/bmesh_py_types_select.c index 93bce055b12..01d555c00ca 100644 --- a/source/blender/python/bmesh/bmesh_py_types_select.c +++ b/source/blender/python/bmesh/bmesh_py_types_select.c @@ -163,7 +163,7 @@ static Py_ssize_t bpy_bmeditselseq_length(BPy_BMEditSelSeq *self) return BLI_listbase_count(&self->bm->selected); } -static PyObject *bpy_bmeditselseq_subscript_int(BPy_BMEditSelSeq *self, int keynum) +static PyObject *bpy_bmeditselseq_subscript_int(BPy_BMEditSelSeq *self, Py_ssize_t keynum) { BMEditSelection *ese; diff --git a/source/blender/python/generic/bgl.c b/source/blender/python/generic/bgl.c index fd5f2e77672..07ee3ed8752 100644 --- a/source/blender/python/generic/bgl.c +++ b/source/blender/python/generic/bgl.c @@ -417,11 +417,11 @@ static PyObject *Method_ShaderSource(PyObject *self, PyObject *args); /* Buffer sequence methods */ -static int Buffer_len(Buffer *self); -static PyObject *Buffer_item(Buffer *self, int i); -static PyObject *Buffer_slice(Buffer *self, int begin, int end); -static int Buffer_ass_item(Buffer *self, int i, PyObject *v); -static int Buffer_ass_slice(Buffer *self, int begin, int end, PyObject *seq); +static Py_ssize_t Buffer_len(Buffer *self); +static PyObject *Buffer_item(Buffer *self, Py_ssize_t i); +static PyObject *Buffer_slice(Buffer *self, Py_ssize_t begin, Py_ssize_t end); +static int Buffer_ass_item(Buffer *self, Py_ssize_t i, PyObject *v); +static int Buffer_ass_slice(Buffer *self, Py_ssize_t begin, Py_ssize_t end, PyObject *seq); static PyObject *Buffer_subscript(Buffer *self, PyObject *item); static int Buffer_ass_subscript(Buffer *self, PyObject *item, PyObject *value); @@ -811,12 +811,12 @@ static PyObject *Buffer_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject /* Buffer sequence methods */ -static int Buffer_len(Buffer *self) +static Py_ssize_t Buffer_len(Buffer *self) { return self->dimensions[0]; } -static PyObject *Buffer_item(Buffer *self, int i) +static PyObject *Buffer_item(Buffer *self, Py_ssize_t i) { if (i >= self->dimensions[0] || i < 0) { PyErr_SetString(PyExc_IndexError, "array index out of range"); @@ -854,10 +854,9 @@ static PyObject *Buffer_item(Buffer *self, int i) return NULL; } -static PyObject *Buffer_slice(Buffer *self, int begin, int end) +static PyObject *Buffer_slice(Buffer *self, Py_ssize_t begin, Py_ssize_t end) { PyObject *list; - int count; if (begin < 0) { begin = 0; @@ -871,13 +870,13 @@ static PyObject *Buffer_slice(Buffer *self, int begin, int end) list = PyList_New(end - begin); - for (count = begin; count < end; count++) { + for (Py_ssize_t count = begin; count < end; count++) { PyList_SET_ITEM(list, count - begin, Buffer_item(self, count)); } return list; } -static int Buffer_ass_item(Buffer *self, int i, PyObject *v) +static int Buffer_ass_item(Buffer *self, Py_ssize_t i, PyObject *v) { if (i >= self->dimensions[0] || i < 0) { PyErr_SetString(PyExc_IndexError, "array assignment index out of range"); @@ -912,10 +911,11 @@ static int Buffer_ass_item(Buffer *self, int i, PyObject *v) } } -static int Buffer_ass_slice(Buffer *self, int begin, int end, PyObject *seq) +static int Buffer_ass_slice(Buffer *self, Py_ssize_t begin, Py_ssize_t end, PyObject *seq) { PyObject *item; - int count, err = 0; + int err = 0; + Py_ssize_t count; if (begin < 0) { begin = 0; @@ -935,7 +935,7 @@ static int Buffer_ass_slice(Buffer *self, int begin, int end, PyObject *seq) return -1; } - /* re-use count var */ + /* Re-use count variable. */ if ((count = PySequence_Size(seq)) != (end - begin)) { PyErr_Format(PyExc_TypeError, "buffer[:] = value, size mismatch in assignment. " diff --git a/source/blender/python/generic/idprop_py_api.c b/source/blender/python/generic/idprop_py_api.c index 3978f7f37cc..9ffea65bf55 100644 --- a/source/blender/python/generic/idprop_py_api.c +++ b/source/blender/python/generic/idprop_py_api.c @@ -1703,12 +1703,12 @@ static PyMethodDef BPy_IDArray_methods[] = { {NULL, NULL, 0, NULL}, }; -static int BPy_IDArray_Len(BPy_IDArray *self) +static Py_ssize_t BPy_IDArray_Len(BPy_IDArray *self) { return self->prop->len; } -static PyObject *BPy_IDArray_GetItem(BPy_IDArray *self, int index) +static PyObject *BPy_IDArray_GetItem(BPy_IDArray *self, Py_ssize_t index) { if (index < 0 || index >= self->prop->len) { PyErr_SetString(PyExc_IndexError, "index out of range!"); @@ -1730,7 +1730,7 @@ static PyObject *BPy_IDArray_GetItem(BPy_IDArray *self, int index) return NULL; } -static int BPy_IDArray_SetItem(BPy_IDArray *self, int index, PyObject *value) +static int BPy_IDArray_SetItem(BPy_IDArray *self, Py_ssize_t index, PyObject *value) { if (index < 0 || index >= self->prop->len) { PyErr_SetString(PyExc_RuntimeError, "index out of range!"); diff --git a/source/blender/python/gpu/gpu_py_buffer.c b/source/blender/python/gpu/gpu_py_buffer.c index 30a434f8667..cee8a1d349b 100644 --- a/source/blender/python/gpu/gpu_py_buffer.c +++ b/source/blender/python/gpu/gpu_py_buffer.c @@ -159,7 +159,7 @@ static BPyGPUBuffer *pygpu_buffer_make_from_data(PyObject *parent, return buffer; } -static PyObject *pygpu_buffer__sq_item(BPyGPUBuffer *self, int i) +static PyObject *pygpu_buffer__sq_item(BPyGPUBuffer *self, Py_ssize_t i) { if (i >= self->shape[0] || i < 0) { PyErr_SetString(PyExc_IndexError, "array index out of range"); @@ -200,10 +200,10 @@ static PyObject *pygpu_buffer__sq_item(BPyGPUBuffer *self, int i) static PyObject *pygpu_buffer_to_list(BPyGPUBuffer *self) { - int i, len = self->shape[0]; + const Py_ssize_t len = self->shape[0]; PyObject *list = PyList_New(len); - for (i = 0; i < len; i++) { + for (Py_ssize_t i = 0; i < len; i++) { PyList_SET_ITEM(list, i, pygpu_buffer__sq_item(self, i)); } @@ -313,7 +313,7 @@ static PyObject *pygpu_buffer__tp_repr(BPyGPUBuffer *self) return repr; } -static int pygpu_buffer__sq_ass_item(BPyGPUBuffer *self, int i, PyObject *v); +static int pygpu_buffer__sq_ass_item(BPyGPUBuffer *self, Py_ssize_t i, PyObject *v); static int pygpu_buffer_ass_slice(BPyGPUBuffer *self, Py_ssize_t begin, @@ -430,7 +430,7 @@ static int pygpu_buffer__tp_is_gc(BPyGPUBuffer *self) /* BPyGPUBuffer sequence methods */ -static int pygpu_buffer__sq_length(BPyGPUBuffer *self) +static Py_ssize_t pygpu_buffer__sq_length(BPyGPUBuffer *self) { return self->shape[0]; } @@ -458,7 +458,7 @@ static PyObject *pygpu_buffer_slice(BPyGPUBuffer *self, Py_ssize_t begin, Py_ssi return list; } -static int pygpu_buffer__sq_ass_item(BPyGPUBuffer *self, int i, PyObject *v) +static int pygpu_buffer__sq_ass_item(BPyGPUBuffer *self, Py_ssize_t i, PyObject *v) { if (i >= self->shape[0] || i < 0) { PyErr_SetString(PyExc_IndexError, "array assignment index out of range"); diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 02f7e16e805..e6525db62a4 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -2328,7 +2328,7 @@ static int pyrna_prop_collection_ass_subscript_int(BPy_PropertyRNA *self, return 0; } -static PyObject *pyrna_prop_array_subscript_int(BPy_PropertyArrayRNA *self, int keynum) +static PyObject *pyrna_prop_array_subscript_int(BPy_PropertyArrayRNA *self, Py_ssize_t keynum) { int len; @@ -2883,7 +2883,7 @@ static PyObject *pyrna_prop_array_subscript(BPy_PropertyArrayRNA *self, PyObject if (key_slice->start == Py_None && key_slice->stop == Py_None) { /* NOTE: no significant advantage with optimizing [:] slice as with collections, * but include here for consistency with collection slice func */ - const Py_ssize_t len = (Py_ssize_t)pyrna_prop_array_length(self); + const Py_ssize_t len = pyrna_prop_array_length(self); return pyrna_prop_array_subscript_slice(self, &self->ptr, self->prop, 0, len, len); } diff --git a/source/blender/python/mathutils/mathutils_Color.c b/source/blender/python/mathutils/mathutils_Color.c index 5218ea68f7b..8bce6af4910 100644 --- a/source/blender/python/mathutils/mathutils_Color.c +++ b/source/blender/python/mathutils/mathutils_Color.c @@ -351,13 +351,13 @@ static Py_hash_t Color_hash(ColorObject *self) * \{ */ /** Sequence length: `len(object)`. */ -static int Color_len(ColorObject *UNUSED(self)) +static Py_ssize_t Color_len(ColorObject *UNUSED(self)) { return COLOR_SIZE; } /** Sequence accessor (get): `x = object[i]`. */ -static PyObject *Color_item(ColorObject *self, int i) +static PyObject *Color_item(ColorObject *self, Py_ssize_t i) { if (i < 0) { i = COLOR_SIZE - i; @@ -378,7 +378,7 @@ static PyObject *Color_item(ColorObject *self, int i) } /** Sequence accessor (set): `object[i] = x`. */ -static int Color_ass_item(ColorObject *self, int i, PyObject *value) +static int Color_ass_item(ColorObject *self, Py_ssize_t i, PyObject *value) { float f; diff --git a/source/blender/python/mathutils/mathutils_Euler.c b/source/blender/python/mathutils/mathutils_Euler.c index ddc0f115742..fd4467c9799 100644 --- a/source/blender/python/mathutils/mathutils_Euler.c +++ b/source/blender/python/mathutils/mathutils_Euler.c @@ -434,13 +434,13 @@ static Py_hash_t Euler_hash(EulerObject *self) * \{ */ /** Sequence length: `len(object)`. */ -static int Euler_len(EulerObject *UNUSED(self)) +static Py_ssize_t Euler_len(EulerObject *UNUSED(self)) { return EULER_SIZE; } /** Sequence accessor (get): `x = object[i]`. */ -static PyObject *Euler_item(EulerObject *self, int i) +static PyObject *Euler_item(EulerObject *self, Py_ssize_t i) { if (i < 0) { i = EULER_SIZE - i; @@ -461,7 +461,7 @@ static PyObject *Euler_item(EulerObject *self, int i) } /** Sequence accessor (set): `object[i] = x`. */ -static int Euler_ass_item(EulerObject *self, int i, PyObject *value) +static int Euler_ass_item(EulerObject *self, Py_ssize_t i, PyObject *value) { float f; diff --git a/source/blender/python/mathutils/mathutils_Matrix.c b/source/blender/python/mathutils/mathutils_Matrix.c index a44f42bc337..682f40c23f1 100644 --- a/source/blender/python/mathutils/mathutils_Matrix.c +++ b/source/blender/python/mathutils/mathutils_Matrix.c @@ -2379,7 +2379,7 @@ static Py_hash_t Matrix_hash(MatrixObject *self) * \{ */ /** Sequence length: `len(object)`. */ -static int Matrix_len(MatrixObject *self) +static Py_ssize_t Matrix_len(MatrixObject *self) { return self->row_num; } @@ -2388,7 +2388,7 @@ static int Matrix_len(MatrixObject *self) * Sequence accessor (get): `x = object[i]`. * \note the wrapped vector gives direct access to the matrix data. */ -static PyObject *Matrix_item_row(MatrixObject *self, int row) +static PyObject *Matrix_item_row(MatrixObject *self, Py_ssize_t row) { if (BaseMath_ReadCallback_ForWrite(self) == -1) { return NULL; @@ -2407,7 +2407,7 @@ static PyObject *Matrix_item_row(MatrixObject *self, int row) * Sequence accessor (get): `x = object.col[i]`. * \note the wrapped vector gives direct access to the matrix data. */ -static PyObject *Matrix_item_col(MatrixObject *self, int col) +static PyObject *Matrix_item_col(MatrixObject *self, Py_ssize_t col) { if (BaseMath_ReadCallback_ForWrite(self) == -1) { return NULL; @@ -3625,15 +3625,15 @@ static int MatrixAccess_len(MatrixAccessObject *self) return (self->type == MAT_ACCESS_ROW) ? self->matrix_user->row_num : self->matrix_user->col_num; } -static PyObject *MatrixAccess_slice(MatrixAccessObject *self, int begin, int end) +static PyObject *MatrixAccess_slice(MatrixAccessObject *self, Py_ssize_t begin, Py_ssize_t end) { PyObject *tuple; - int count; + Py_ssize_t count; /* row/col access */ MatrixObject *matrix_user = self->matrix_user; int matrix_access_len; - PyObject *(*Matrix_item_new)(MatrixObject *, int); + PyObject *(*Matrix_item_new)(MatrixObject *, Py_ssize_t); if (self->type == MAT_ACCESS_ROW) { matrix_access_len = matrix_user->row_num; diff --git a/source/blender/python/mathutils/mathutils_Quaternion.c b/source/blender/python/mathutils/mathutils_Quaternion.c index 976c1da1916..55269ae20c1 100644 --- a/source/blender/python/mathutils/mathutils_Quaternion.c +++ b/source/blender/python/mathutils/mathutils_Quaternion.c @@ -881,13 +881,13 @@ static Py_hash_t Quaternion_hash(QuaternionObject *self) * \{ */ /** Sequence length: `len(object)`. */ -static int Quaternion_len(QuaternionObject *UNUSED(self)) +static Py_ssize_t Quaternion_len(QuaternionObject *UNUSED(self)) { return QUAT_SIZE; } /** Sequence accessor (get): `x = object[i]`. */ -static PyObject *Quaternion_item(QuaternionObject *self, int i) +static PyObject *Quaternion_item(QuaternionObject *self, Py_ssize_t i) { if (i < 0) { i = QUAT_SIZE - i; @@ -908,7 +908,7 @@ static PyObject *Quaternion_item(QuaternionObject *self, int i) } /** Sequence accessor (set): `object[i] = x`. */ -static int Quaternion_ass_item(QuaternionObject *self, int i, PyObject *ob) +static int Quaternion_ass_item(QuaternionObject *self, Py_ssize_t i, PyObject *ob) { float f; diff --git a/source/blender/python/mathutils/mathutils_Vector.c b/source/blender/python/mathutils/mathutils_Vector.c index 290be771c90..a31297571a5 100644 --- a/source/blender/python/mathutils/mathutils_Vector.c +++ b/source/blender/python/mathutils/mathutils_Vector.c @@ -1667,7 +1667,7 @@ static Py_hash_t Vector_hash(VectorObject *self) * \{ */ /** Sequence length: `len(object)`. */ -static int Vector_len(VectorObject *self) +static Py_ssize_t Vector_len(VectorObject *self) { return self->vec_num; } @@ -1699,7 +1699,7 @@ static PyObject *vector_item_internal(VectorObject *self, int i, const bool is_a } /** Sequence accessor (get): `x = object[i]`. */ -static PyObject *Vector_item(VectorObject *self, int i) +static PyObject *Vector_item(VectorObject *self, Py_ssize_t i) { return vector_item_internal(self, i, false); } @@ -1747,7 +1747,7 @@ static int vector_ass_item_internal(VectorObject *self, int i, PyObject *value, } /** Sequence accessor (set): `object[i] = x`. */ -static int Vector_ass_item(VectorObject *self, int i, PyObject *value) +static int Vector_ass_item(VectorObject *self, Py_ssize_t i, PyObject *value) { return vector_ass_item_internal(self, i, value, false); } -- cgit v1.2.3