[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24591] trunk/blender/source/blender/ python/generic: update idproperty python api for python 3.1

Campbell Barton ideasman42 at gmail.com
Mon Nov 16 19:56:58 CET 2009


Revision: 24591
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24591
Author:   campbellbarton
Date:     2009-11-16 19:56:58 +0100 (Mon, 16 Nov 2009)

Log Message:
-----------
update idproperty python api for python 3.1
- removed 'group.has_key("key")', use... '"key" in group' instead
- removed verbose float and int conversions that are not needed
- fixed memory leak

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

Modified: trunk/blender/source/blender/python/generic/IDProp.c
===================================================================
--- trunk/blender/source/blender/python/generic/IDProp.c	2009-11-16 18:53:11 UTC (rev 24590)
+++ trunk/blender/source/blender/python/generic/IDProp.c	2009-11-16 18:56:58 UTC (rev 24591)
@@ -17,20 +17,18 @@
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  *
- * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
- * All rights reserved.
  *
- * Contributor(s): Joseph Eagar
+ * Contributor(s): Joseph Eagar, Campbell Barton
  *
  * ***** END GPL LICENSE BLOCK *****
  */
- 
+
 #include "DNA_ID.h"
 
 #include "BKE_idprop.h"
 
 #include "IDProp.h"
-#include "gen_utils.h"
+// #include "gen_utils.h"
 
 #include "MEM_guardedalloc.h"
 
@@ -46,7 +44,7 @@
 
 PyObject *IDGroup_repr( BPy_IDProperty *self )
 {
-	return PyString_FromString( "(ID Property)" );
+	return PyUnicode_FromFormat( "<bpy ID property from \"%s\">", self->id->name);
 }
 
 extern PyTypeObject IDGroup_Type;
@@ -55,9 +53,9 @@
 {
 	switch ( prop->type ) {
 		case IDP_STRING:
-			return PyString_FromString( prop->data.pointer );
+			return PyUnicode_FromString( prop->data.pointer );
 		case IDP_INT:
-			return PyInt_FromLong( (long)prop->data.val );
+			return PyLong_FromLong( (long)prop->data.val );
 		case IDP_FLOAT:
 			return PyFloat_FromDouble( (double)(*(float*)(&prop->data.val)) );
 		case IDP_DOUBLE:
@@ -66,10 +64,11 @@
 			/*blegh*/
 			{
 				BPy_IDProperty *group = PyObject_New(BPy_IDProperty, &IDGroup_Type);
-				if (!group)
-					return EXPP_ReturnPyObjError( PyExc_RuntimeError,
-					   "PyObject_New() failed" );
-			
+				if (!group) {
+					PyErr_SetString( PyExc_RuntimeError, "PyObject_New() failed" );
+					return NULL;
+				}
+
 				group->id = id;
 				group->prop = prop;
 				return (PyObject*) group;
@@ -77,10 +76,10 @@
 		case IDP_ARRAY:
 			{
 				BPy_IDProperty *array = PyObject_New(BPy_IDProperty, &IDArray_Type);
-				if (!array)
-					return EXPP_ReturnPyObjError( PyExc_RuntimeError,
-					   "PyObject_New() failed" );
-					   
+				if (!array) {
+					PyErr_SetString( PyExc_RuntimeError, "PyObject_New() failed" );
+					return NULL;
+				}
 				array->id = id;
 				array->prop = prop;
 				return (PyObject*) array;
@@ -95,10 +94,12 @@
 		case IDP_STRING:
 		{
 			char *st;
-			if (!PyString_Check(value))
-				return EXPP_ReturnIntError(PyExc_TypeError, "expected a string!");
+			if (!PyUnicode_Check(value)) {
+				PyErr_SetString(PyExc_TypeError, "expected a string!");
+				return -1;
+			}
 
-			st = PyString_AsString(value);
+			st = _PyUnicode_AsString(value);
 			IDP_ResizeArray(prop, strlen(st)+1);
 			strcpy(prop->data.pointer, st);
 			return 0;
@@ -106,72 +107,70 @@
 
 		case IDP_INT:
 		{
-			int ivalue;
-			if (!PyNumber_Check(value))
-				return EXPP_ReturnIntError(PyExc_TypeError, "expected an int!");
-			value = PyNumber_Int(value);
-			if (!value)
-				return EXPP_ReturnIntError(PyExc_TypeError, "expected an int!");
-			ivalue = (int) PyInt_AsLong(value);
+			int ivalue= PyLong_AsSsize_t(value);
+			if (ivalue==-1 && PyErr_Occurred()) {
+				PyErr_SetString(PyExc_TypeError, "expected an int type");
+				return -1;
+			}
 			prop->data.val = ivalue;
-			Py_XDECREF(value);
 			break;
 		}
 		case IDP_FLOAT:
 		{
-			float fvalue;
-			if (!PyNumber_Check(value))
-				return EXPP_ReturnIntError(PyExc_TypeError, "expected a float!");
-			value = PyNumber_Float(value);
-			if (!value)
-				return EXPP_ReturnIntError(PyExc_TypeError, "expected a float!");
-			fvalue = (float) PyFloat_AsDouble(value);
+			float fvalue= (float)PyFloat_AsDouble(value);
+			if (fvalue==-1 && PyErr_Occurred()) {
+				PyErr_SetString(PyExc_TypeError, "expected a float");
+				return -1;
+			}
 			*(float*)&self->prop->data.val = fvalue;
-			Py_XDECREF(value);
 			break;
 		}
 		case IDP_DOUBLE:
 		{
-			double dvalue;
-			if (!PyNumber_Check(value))
-				return EXPP_ReturnIntError(PyExc_TypeError, "expected a float!");
-			value = PyNumber_Float(value);
-			if (!value)
-				return EXPP_ReturnIntError(PyExc_TypeError, "expected a float!");
-			dvalue = (float) PyFloat_AsDouble(value);
+			double dvalue= PyFloat_AsDouble(value);
+			if (dvalue==-1 && PyErr_Occurred()) {
+				PyErr_SetString(PyExc_TypeError, "expected a float");
+				return -1;
+			}
 			*(double*)&self->prop->data.val = dvalue;
-			Py_XDECREF(value);
 			break;
 		}
 		default:
-			return EXPP_ReturnIntError(PyExc_AttributeError, "attempt to set read-only attribute!");
+			PyErr_SetString(PyExc_AttributeError, "attempt to set read-only attribute!");
+			return -1;
 	}
 	return 0;
 }
 
 PyObject *BPy_IDGroup_GetName(BPy_IDProperty *self, void *bleh)
 {
-	return PyString_FromString(self->prop->name);
+	return PyUnicode_FromString(self->prop->name);
 }
 
 static int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value, void *bleh)
 {
 	char *st;
-	if (!PyString_Check(value))
-		return EXPP_ReturnIntError(PyExc_TypeError, "expected a string!");
+	if (!PyUnicode_Check(value)) {
+		PyErr_SetString(PyExc_TypeError, "expected a string!");
+		return -1;
+	}
 
-	st = PyString_AsString(value);
-	if (strlen(st) >= MAX_IDPROP_NAME)
-		return EXPP_ReturnIntError(PyExc_TypeError, "string length cannot exceed 31 characters!");
+	st = _PyUnicode_AsString(value);
+	if (strlen(st) >= MAX_IDPROP_NAME) {
+		PyErr_SetString(PyExc_TypeError, "string length cannot exceed 31 characters!");
+		return -1;
+	}
 
 	strcpy(self->prop->name, st);
 	return 0;
 }
 
+#if 0
 static PyObject *BPy_IDGroup_GetType(BPy_IDProperty *self)
 {
-	return PyInt_FromLong((long)self->prop->type);
+	return PyLong_FromSsize_t(self->prop->type);
 }
+#endif
 
 static PyGetSetDef BPy_IDGroup_getseters[] = {
 	{"name",
@@ -180,56 +179,67 @@
 	 NULL},
 	 {NULL, NULL, NULL, NULL, NULL}
 };
-	 
-static int BPy_IDGroup_Map_Len(BPy_IDProperty *self)
+
+static Py_ssize_t BPy_IDGroup_Map_Len(BPy_IDProperty *self)
 {
-	if (self->prop->type != IDP_GROUP)
-		return EXPP_ReturnIntError( PyExc_TypeError,
-			"len() of unsized object");
-			
+	if (self->prop->type != IDP_GROUP) {
+		PyErr_SetString( PyExc_TypeError, "len() of unsized object");
+		return -1;
+	}
+
 	return self->prop->len;
 }
 
 static PyObject *BPy_IDGroup_Map_GetItem(BPy_IDProperty *self, PyObject *item)
 {
-	IDProperty *loop;
-	char *st;
-	
-	if (self->prop->type  != IDP_GROUP)
-		return EXPP_ReturnPyObjError( PyExc_TypeError,
-			"unsubscriptable object");
-			
-	if (!PyString_Check(item)) 
-		return EXPP_ReturnPyObjError( PyExc_TypeError,
-			"only strings are allowed as keys of ID properties");
-	
-	st = PyString_AsString(item);
-	for (loop=self->prop->data.group.first; loop; loop=loop->next) {
-		if (BSTR_EQ(loop->name, st)) return BPy_IDGroup_WrapData(self->id, loop);
+	IDProperty *idprop;
+	char *name;
+
+	if (self->prop->type  != IDP_GROUP) {
+		PyErr_SetString( PyExc_TypeError, "unsubscriptable object");
+		return NULL;
 	}
-	return EXPP_ReturnPyObjError( PyExc_KeyError,
-		"key not in subgroup dict");
+
+	name= _PyUnicode_AsString(item);
+
+	if (name == NULL) {
+		PyErr_SetString( PyExc_TypeError, "only strings are allowed as keys of ID properties");
+		return NULL;
+	}
+
+	idprop= IDP_GetPropertyFromGroup(self->prop, name);
+
+	if(idprop==NULL) {
+		PyErr_SetString( PyExc_KeyError, "key not in subgroup dict");
+		return NULL;
+	}
+
+	return BPy_IDGroup_WrapData(self->id, idprop);
+
 }
 
 /*returns NULL on success, error string on failure*/
-static char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObject *ob)
+char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObject *ob)
 {
 	IDProperty *prop = NULL;
 	IDPropertyTemplate val = {0};
-	
+
+	if(strlen(name) >= sizeof(group->name))
+		return "the length of IDProperty names is limited to 31 characters";
+
 	if (PyFloat_Check(ob)) {
 		val.d = PyFloat_AsDouble(ob);
 		prop = IDP_New(IDP_DOUBLE, val, name);
-	} else if (PyInt_Check(ob)) {
-		val.i = (int) PyInt_AsLong(ob);
+	} else if (PyLong_Check(ob)) {
+		val.i = (int) PyLong_AsSsize_t(ob);
 		prop = IDP_New(IDP_INT, val, name);
-	} else if (PyString_Check(ob)) {
-		val.str = PyString_AsString(ob);
+	} else if (PyUnicode_Check(ob)) {
+		val.str = _PyUnicode_AsString(ob);
 		prop = IDP_New(IDP_STRING, val, name);
 	} else if (PySequence_Check(ob)) {
 		PyObject *item;
 		int i;
-		
+
 		/*validate sequence and derive type.
 		we assume IDP_INT unless we hit a float
 		number; then we assume it's */
@@ -238,21 +248,21 @@
 		for (i=0; i<val.array.len; i++) {
 			item = PySequence_GetItem(ob, i);
 			if (PyFloat_Check(item)) val.array.type = IDP_DOUBLE;
-			else if (!PyInt_Check(item)) return "only floats and ints are allowed in ID property arrays";
+			else if (!PyLong_Check(item)) {
+				Py_XDECREF(item);
+				return "only floats and ints are allowed in ID property arrays";
+			}
 			Py_XDECREF(item);
 		}
-		
+
 		prop = IDP_New(IDP_ARRAY, val, name);
 		for (i=0; i<val.array.len; i++) {
 			item = PySequence_GetItem(ob, i);
 			if (val.array.type == IDP_INT) {
-				item = PyNumber_Int(item);
-				((int*)prop->data.pointer)[i] = (int)PyInt_AsLong(item);
+				((int*)prop->data.pointer)[i] = (int)PyLong_AsSsize_t(item);
 			} else {
-				item = PyNumber_Float(item);
 				((double*)prop->data.pointer)[i] = (float)PyFloat_AsDouble(item);
 			}
-			Py_XDECREF(item);
 		}
 	} else if (PyMapping_Check(ob)) {
 		PyObject *keys, *vals, *key, *pval;
@@ -260,7 +270,7 @@
 		/*yay! we get into recursive stuff now!*/
 		keys = PyMapping_Keys(ob);
 		vals = PyMapping_Values(ob);
-		
+
 		/*we allocate the group first; if we hit any invalid data,
 		  we can delete it easily enough.*/
 		prop = IDP_New(IDP_GROUP, val, name);
@@ -268,7 +278,7 @@
 		for (i=0; i<len; i++) {
 			key = PySequence_GetItem(keys, i);
 			pval = PySequence_GetItem(vals, i);
-			if (!PyString_Check(key)) {
+			if (!PyUnicode_Check(key)) {
 				IDP_FreeProperty(prop);
 				MEM_freeN(prop);
 				Py_XDECREF(keys);
@@ -277,7 +287,7 @@
 				Py_XDECREF(pval);
 				return "invalid element in subgroup dict template!";
 			}
-			if (BPy_IDProperty_Map_ValidateAndCreate(PyString_AsString(key), prop, pval)) {
+			if (BPy_IDProperty_Map_ValidateAndCreate(_PyUnicode_AsString(key), prop, pval)) {
 				IDP_FreeProperty(prop);
 				MEM_freeN(prop);
 				Py_XDECREF(keys);
@@ -292,7 +302,7 @@
 		Py_XDECREF(keys);
 		Py_XDECREF(vals);
 	} else return "invalid property value";
-	
+
 	IDP_ReplaceInGroup(group, prop);

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list