[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [51281] trunk/blender/source/blender/ python/generic/idprop_py_api.c: optimization for PyObject -> ID-property sequence conversion, use PySequence_Fast.

Campbell Barton ideasman42 at gmail.com
Fri Oct 12 05:59:51 CEST 2012


Revision: 51281
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=51281
Author:   campbellbarton
Date:     2012-10-12 03:59:50 +0000 (Fri, 12 Oct 2012)
Log Message:
-----------
optimization for PyObject -> ID-property sequence conversion, use PySequence_Fast.

Modified Paths:
--------------
    trunk/blender/source/blender/python/generic/idprop_py_api.c

Modified: trunk/blender/source/blender/python/generic/idprop_py_api.c
===================================================================
--- trunk/blender/source/blender/python/generic/idprop_py_api.c	2012-10-12 03:24:47 UTC (rev 51280)
+++ trunk/blender/source/blender/python/generic/idprop_py_api.c	2012-10-12 03:59:50 UTC (rev 51281)
@@ -300,40 +300,34 @@
 }
 
 /* returns NULL on success, error string on failure */
-static int idp_sequence_type(PyObject *seq)
+static int idp_sequence_type(PyObject *seq_fast)
 {
 	PyObject *item;
 	int type = IDP_INT;
 
-	Py_ssize_t i, len = PySequence_Size(seq);
+	Py_ssize_t i, len = PySequence_Fast_GET_SIZE(seq_fast);
 	for (i = 0; i < len; i++) {
-		item = PySequence_GetItem(seq, i);
+		item = PySequence_Fast_GET_ITEM(seq_fast, i);
 		if (PyFloat_Check(item)) {
 			if (type == IDP_IDPARRAY) { /* mixed dict/int */
-				Py_DECREF(item);
 				return -1;
 			}
 			type = IDP_DOUBLE;
 		}
 		else if (PyLong_Check(item)) {
 			if (type == IDP_IDPARRAY) { /* mixed dict/int */
-				Py_DECREF(item);
 				return -1;
 			}
 		}
 		else if (PyMapping_Check(item)) {
 			if (i != 0 && (type != IDP_IDPARRAY)) { /* mixed dict/int */
-				Py_DECREF(item);
 				return -1;
 			}
 			type = IDP_IDPARRAY;
 		}
 		else {
-			Py_XDECREF(item);
 			return -1;
 		}
-
-		Py_DECREF(item);
 	}
 
 	return type;
@@ -386,50 +380,61 @@
 		//prop->subtype = IDP_STRING_SUB_BYTE;
 	}
 	else if (PySequence_Check(ob)) {
+		PyObject *ob_seq_fast = PySequence_Fast(ob, "py -> idprop");
 		PyObject *item;
 		int i;
 
-		if ((val.array.type = idp_sequence_type(ob)) == -1)
+		if (ob_seq_fast == NULL) {
+			PyErr_Print();
+			PyErr_Clear();
+			return "error converting the sequence";
+		}
+
+		if ((val.array.type = idp_sequence_type(ob_seq_fast)) == -1) {
+			Py_DECREF(ob_seq_fast);
 			return "only floats, ints and dicts are allowed in ID property arrays";
+		}
 
 		/* validate sequence and derive type.
 		 * we assume IDP_INT unless we hit a float
 		 * number; then we assume it's */
 
-		val.array.len = PySequence_Size(ob);
+		val.array.len = PySequence_Fast_GET_SIZE(ob_seq_fast);
 
 		switch (val.array.type) {
 			case IDP_DOUBLE:
 				prop = IDP_New(IDP_ARRAY, &val, name);
 				for (i = 0; i < val.array.len; i++) {
-					item = PySequence_GetItem(ob, i);
+					item = PySequence_Fast_GET_ITEM(ob_seq_fast, i);
 					((double *)IDP_Array(prop))[i] = (float)PyFloat_AsDouble(item);
-					Py_DECREF(item);
 				}
 				break;
 			case IDP_INT:
 				prop = IDP_New(IDP_ARRAY, &val, name);
 				for (i = 0; i < val.array.len; i++) {
-					item = PySequence_GetItem(ob, i);
+					item = PySequence_Fast_GET_ITEM(ob_seq_fast, i);
 					((int *)IDP_Array(prop))[i] = (int)PyLong_AsSsize_t(item);
-					Py_DECREF(item);
 				}
 				break;
 			case IDP_IDPARRAY:
 				prop = IDP_NewIDPArray(name);
 				for (i = 0; i < val.array.len; i++) {
 					const char *error;
-					item = PySequence_GetItem(ob, i);
+					item = PySequence_Fast_GET_ITEM(ob_seq_fast, i);
 					error = BPy_IDProperty_Map_ValidateAndCreate(NULL, prop, item);
-					Py_DECREF(item);
 
-					if (error)
+					if (error) {
+						Py_DECREF(ob_seq_fast);
 						return error;
+					}
 				}
 				break;
 			default:
+				Py_DECREF(ob_seq_fast);
 				return "internal error with idp array.type";
 		}
+
+		Py_DECREF(ob_seq_fast);
 	}
 	else if (PyMapping_Check(ob)) {
 		PyObject *keys, *vals, *key, *pval;




More information about the Bf-blender-cvs mailing list