From 928e2784c6596f64ca5201eed269959865d15970 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 27 Sep 2011 05:28:06 +0000 Subject: [PATCH] py api - use Py_ssize_t when dealing with python sequence sizes - dont call PySequence_Size(py_b) in a loop (its slow). - use faster sequence/float parsing in aud.Factory.filter --- intern/audaspace/Python/AUD_PyAPI.cpp | 22 +++++++++++++------- source/blender/python/generic/IDProp.c | 2 +- source/blender/python/generic/bgl.c | 4 ++-- source/blender/python/intern/bpy_rna.c | 4 ++-- source/blender/python/intern/bpy_rna_array.c | 12 +++++------ 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/intern/audaspace/Python/AUD_PyAPI.cpp b/intern/audaspace/Python/AUD_PyAPI.cpp index 928c67c5196..3ec088053ca 100644 --- a/intern/audaspace/Python/AUD_PyAPI.cpp +++ b/intern/audaspace/Python/AUD_PyAPI.cpp @@ -848,6 +848,8 @@ Factory_filter(Factory* self, PyObject* args) { PyObject* py_b; PyObject* py_a = NULL; + Py_ssize_t py_a_len; + Py_ssize_t py_b_len; if(!PyArg_ParseTuple(args, "O|O:filter", &py_b, &py_a)) return NULL; @@ -858,7 +860,10 @@ Factory_filter(Factory* self, PyObject* args) return NULL; } - if(!PySequence_Size(py_b) || (py_a != NULL && !PySequence_Size(py_a))) + py_a_len= py_a ? PySequence_Size(py_a) : 0; + py_b_len= PySequence_Size(py_b); + + if(!py_b_len || ((py_a != NULL) && !py_b_len)) { PyErr_SetString(PyExc_ValueError, "The sequence has to contain at least one value!"); return NULL; @@ -867,30 +872,31 @@ Factory_filter(Factory* self, PyObject* args) std::vector a, b; PyObject* py_value; float value; - int result; - for(int i = 0; i < PySequence_Size(py_b); i++) + for(Py_ssize_t i = 0; i < py_b_len; i++) { py_value = PySequence_GetItem(py_b, i); - result = PyArg_Parse(py_value, "f:filter", &value); + value= (float)PyFloat_AsDouble(py_value); Py_DECREF(py_value); - if(!result) + if (value==-1.0f && PyErr_Occurred()) { return NULL; + } b.push_back(value); } if(py_a) { - for(int i = 0; i < PySequence_Size(py_a); i++) + for(Py_ssize_t i = 0; i < py_a_len; i++) { py_value = PySequence_GetItem(py_a, i); - result = PyArg_Parse(py_value, "f:filter", &value); + value= (float)PyFloat_AsDouble(py_value); Py_DECREF(py_value); - if(!result) + if (value==-1.0f && PyErr_Occurred()) { return NULL; + } a.push_back(value); } diff --git a/source/blender/python/generic/IDProp.c b/source/blender/python/generic/IDProp.c index 2543d34f58c..e6883eb30af 100644 --- a/source/blender/python/generic/IDProp.c +++ b/source/blender/python/generic/IDProp.c @@ -269,7 +269,7 @@ static int idp_sequence_type(PyObject *seq) PyObject *item; int type= IDP_INT; - int i, len = PySequence_Size(seq); + Py_ssize_t i, len = PySequence_Size(seq); for (i=0; i < len; i++) { item = PySequence_GetItem(seq, i); if (PyFloat_Check(item)) { diff --git a/source/blender/python/generic/bgl.c b/source/blender/python/generic/bgl.c index 44d42a479ec..35c211d5424 100644 --- a/source/blender/python/generic/bgl.c +++ b/source/blender/python/generic/bgl.c @@ -286,8 +286,8 @@ static PyObject *Buffer_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject Buffer *buffer; int dimensions[MAX_DIMENSIONS]; - int i, type; - int ndimensions = 0; + int type; + Py_ssize_t i, ndimensions = 0; if(kwds && PyDict_Size(kwds)) { PyErr_SetString(PyExc_TypeError, diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 3175c0d088e..bcbd7670e2c 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -1718,7 +1718,7 @@ static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyOb } case PROP_COLLECTION: { - int seq_len, i; + Py_ssize_t seq_len, i; PyObject *item; PointerRNA itemptr; ListBase *lb; @@ -1736,7 +1736,7 @@ static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyOb } seq_len= PySequence_Size(value); - for(i=0; itp_name); @@ -147,8 +147,8 @@ static int count_items(PyObject *seq, int dim) int totitem= 0; if(dim > 1) { - const int seq_size= PySequence_Size(seq); - int i; + const Py_ssize_t seq_size= PySequence_Size(seq); + Py_ssize_t i; for (i= 0; i < seq_size; i++) { PyObject *item= PySequence_GetItem(seq, i); if(item) { @@ -281,9 +281,9 @@ static char *copy_value_single(PyObject *item, PointerRNA *ptr, PropertyRNA *pro static char *copy_values(PyObject *seq, PointerRNA *ptr, PropertyRNA *prop, int dim, char *data, unsigned int item_size, int *index, ItemConvertFunc convert_item, RNA_SetIndexFunc rna_set_index) { - unsigned int i; int totdim= RNA_property_array_dimension(ptr, prop, NULL); - const int seq_size= PySequence_Size(seq); + const Py_ssize_t seq_size= PySequence_Size(seq); + Py_ssize_t i; /* Regarding PySequence_GetItem() failing. *