py api
authorCampbell Barton <ideasman42@gmail.com>
Tue, 27 Sep 2011 05:28:06 +0000 (05:28 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 27 Sep 2011 05:28:06 +0000 (05:28 +0000)
- 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
source/blender/python/generic/IDProp.c
source/blender/python/generic/bgl.c
source/blender/python/intern/bpy_rna.c
source/blender/python/intern/bpy_rna_array.c

index 928c67c..3ec0880 100644 (file)
@@ -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<float> 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);
                }
index 2543d34..e6883eb 100644 (file)
@@ -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)) {
index 44d42a4..35c211d 100644 (file)
@@ -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,
index 3175c0d..bcbd767 100644 (file)
@@ -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; i<seq_len; i++) {
+                       for(i=0; i < seq_len; i++) {
                                item= PySequence_GetItem(value, i);
 
                                if(item==NULL) {
index e50ce23..cab5772 100644 (file)
@@ -61,12 +61,12 @@ typedef void (*RNA_SetIndexFunc)(PointerRNA *, PropertyRNA *, int index, void *)
 static int validate_array_type(PyObject *seq, int dim, int totdim, int dimsize[],
                                                           ItemTypeCheckFunc check_item_type, const char *item_type_str, const char *error_prefix)
 {
-       int i;
+       Py_ssize_t i;
 
        /* not the last dimension */
        if (dim + 1 < totdim) {
                /* check that a sequence contains dimsize[dim] items */
-               const int seq_size= PySequence_Size(seq);
+               const Py_ssize_t seq_size= PySequence_Size(seq);
                if(seq_size == -1) {
                        PyErr_Format(PyExc_ValueError, "%s sequence expected at dimension %d, not '%s'",
                                     error_prefix, (int)dim + 1, Py_TYPE(seq)->tp_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.
         *