[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [26943] trunk/blender/source/blender: bugfix [#21173] Autocompleate raises an error

Campbell Barton ideasman42 at gmail.com
Tue Feb 16 00:43:51 CET 2010


Revision: 26943
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=26943
Author:   campbellbarton
Date:     2010-02-16 00:43:51 +0100 (Tue, 16 Feb 2010)

Log Message:
-----------
bugfix [#21173] Autocompleate raises an error

split PropertyRNA off into 3 types, base type, collection and array, since array and collections needed internal checks inside almost every function its better to have the, as subclassed to the property type. This makes introspection more useful.

Also made printing of structs and properties prettier giveing type and length.

Modified Paths:
--------------
    trunk/blender/source/blender/makesrna/RNA_enum_types.h
    trunk/blender/source/blender/makesrna/intern/rna_rna.c
    trunk/blender/source/blender/python/intern/bpy_rna.c
    trunk/blender/source/blender/python/intern/bpy_rna.h

Modified: trunk/blender/source/blender/makesrna/RNA_enum_types.h
===================================================================
--- trunk/blender/source/blender/makesrna/RNA_enum_types.h	2010-02-15 22:50:53 UTC (rev 26942)
+++ trunk/blender/source/blender/makesrna/RNA_enum_types.h	2010-02-15 23:43:51 UTC (rev 26943)
@@ -79,6 +79,7 @@
 
 extern EnumPropertyItem wm_report_items[];
 
+extern EnumPropertyItem property_type_items[];
 extern EnumPropertyItem property_unit_items[];
 
 struct bContext;

Modified: trunk/blender/source/blender/makesrna/intern/rna_rna.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_rna.c	2010-02-15 22:50:53 UTC (rev 26942)
+++ trunk/blender/source/blender/makesrna/intern/rna_rna.c	2010-02-15 23:43:51 UTC (rev 26943)
@@ -32,6 +32,16 @@
 
 #include "rna_internal.h"
 
+EnumPropertyItem property_type_items[] = {
+	{PROP_BOOLEAN, "BOOLEAN", 0, "Boolean", ""},
+	{PROP_INT, "INT", 0, "Integer", ""},
+	{PROP_FLOAT, "FLOAT", 0, "Float", ""},
+	{PROP_STRING, "STRING", 0, "String", ""},
+	{PROP_ENUM, "ENUM", 0, "Enumeration", ""},
+	{PROP_POINTER, "POINTER", 0, "Pointer", ""},
+	{PROP_COLLECTION, "COLLECTION", 0, "Collection", ""},
+	{0, NULL, 0, NULL, NULL}};
+
 EnumPropertyItem property_unit_items[] = {
 	{PROP_UNIT_NONE, "NONE", 0, "None", ""},
 	{PROP_UNIT_LENGTH, "LENGTH", 0, "Length", ""},
@@ -907,15 +917,6 @@
 {
 	StructRNA *srna;
 	PropertyRNA *prop;
-	static EnumPropertyItem type_items[] = {
-		{PROP_BOOLEAN, "BOOLEAN", 0, "Boolean", ""},
-		{PROP_INT, "INT", 0, "Integer", ""},
-		{PROP_FLOAT, "FLOAT", 0, "Float", ""},
-		{PROP_STRING, "STRING", 0, "String", ""},
-		{PROP_ENUM, "ENUM", 0, "Enumeration", ""},
-		{PROP_POINTER, "POINTER", 0, "Pointer", ""},
-		{PROP_COLLECTION, "COLLECTION", 0, "Collection", ""},
-		{0, NULL, 0, NULL, NULL}};
 	static EnumPropertyItem subtype_items[] = {
 		{PROP_NONE, "NONE", 0, "None", ""},
 		{PROP_FILEPATH, "FILE_PATH", 0, "File Path", ""},
@@ -961,7 +962,7 @@
 
 	prop= RNA_def_property(srna, "type", PROP_ENUM, PROP_NONE);
 	RNA_def_property_clear_flag(prop, PROP_EDITABLE);
-	RNA_def_property_enum_items(prop, type_items);
+	RNA_def_property_enum_items(prop, property_type_items);
 	RNA_def_property_enum_funcs(prop, "rna_Property_type_get", NULL, NULL);
 	RNA_def_property_ui_text(prop, "Type", "Data type of the property");
 

Modified: trunk/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_rna.c	2010-02-15 22:50:53 UTC (rev 26942)
+++ trunk/blender/source/blender/python/intern/bpy_rna.c	2010-02-15 23:43:51 UTC (rev 26943)
@@ -34,6 +34,7 @@
 
 #include "RNA_access.h"
 #include "RNA_define.h" /* for defining our own rna */
+#include "RNA_enum_types.h"
 
 #include "MEM_guardedalloc.h"
 #include "BKE_utildefines.h"
@@ -55,8 +56,11 @@
 #include "../generic/Mathutils.h" /* so we can have mathutils callbacks */
 #include "../generic/IDProp.h" /* for IDprop lookups */
 
+#include <string.h>
 
-static PyObject *prop_subscript_array_slice(BPy_PropertyRNA *self, PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length);
+static PyObject *pyrna_prop_array_subscript_slice(BPy_PropertyRNA *self, PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length);
+static Py_ssize_t pyrna_prop_array_length(BPy_PropertyRNA *self);
+static Py_ssize_t pyrna_prop_collection_length( BPy_PropertyRNA *self );
 
 /* bpyrna vector/euler/quat callbacks */
 static int mathutils_rna_array_cb_index= -1; /* index for our callbacks */
@@ -241,7 +245,7 @@
 		if(is_thick) {
 			/* this is an array we cant reference (since its not thin wrappable)
 			 * and cannot be coerced into a mathutils type, so return as a list */
-			ret = prop_subscript_array_slice(NULL, ptr, prop, 0, len, len);
+			ret = pyrna_prop_array_subscript_slice(NULL, ptr, prop, 0, len, len);
 		} else {
 			ret = pyrna_prop_CreatePyObject(ptr, prop); /* owned by the Mathutils PyObject */
 		}
@@ -333,12 +337,12 @@
 	/* print name if available */
 	name= RNA_struct_name_get_alloc(&self->ptr, NULL, FALSE);
 	if(name) {
-		pyob= PyUnicode_FromFormat( "[BPy_StructRNA \"%.200s\" -> \"%.200s\"]", RNA_struct_identifier(self->ptr.type), name);
+		pyob= PyUnicode_FromFormat( "<bpy_struct, %.200s(\"%.200s\")>", RNA_struct_identifier(self->ptr.type), name);
 		MEM_freeN(name);
 		return pyob;
 	}
 
-	return PyUnicode_FromFormat( "[BPy_StructRNA \"%.200s\"]", RNA_struct_identifier(self->ptr.type));
+	return PyUnicode_FromFormat( "<bpy_struct, %.200s>", RNA_struct_identifier(self->ptr.type));
 }
 
 static PyObject *pyrna_prop_repr( BPy_PropertyRNA *self )
@@ -346,20 +350,44 @@
 	PyObject *pyob;
 	PointerRNA ptr;
 	char *name;
+	const char *type_id= NULL;
+	char type_fmt[64]= "";
+	int type= RNA_property_type(self->prop);
 
+	if(RNA_enum_id_from_value(property_type_items, type, &type_id)==0) {
+		PyErr_SetString(PyExc_SystemError, "could not use property type, internal error"); /* should never happen */
+		return NULL;
+	}
+	else {
+		/* this should never fail */
+		int len = -1;
+		char *c= type_fmt;
+
+		while ( (*c++= tolower(*type_id++)) ) {} ;
+
+		if(type==PROP_COLLECTION) {
+			len= pyrna_prop_collection_length(self);
+		} else if (RNA_property_array_check(&self->ptr, self->prop)) {
+			len= pyrna_prop_array_length(self);
+		}
+
+		if(len != -1)
+			sprintf(--c, "[%d]", len);
+	}
+
 	/* if a pointer, try to print name of pointer target too */
 	if(RNA_property_type(self->prop) == PROP_POINTER) {
 		ptr= RNA_property_pointer_get(&self->ptr, self->prop);
 		name= RNA_struct_name_get_alloc(&ptr, NULL, FALSE);
 
 		if(name) {
-			pyob= PyUnicode_FromFormat( "[BPy_PropertyRNA \"%.200s\" -> \"%.200s\" -> \"%.200s\" ]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop), name);
+			pyob= PyUnicode_FromFormat( "<bpy_%.200s, %.200s.%.200s(\"%.200s\")>", type_fmt, RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop), name);
 			MEM_freeN(name);
 			return pyob;
 		}
 	}
 
-	return PyUnicode_FromFormat( "[BPy_PropertyRNA \"%.200s\" -> \"%.200s\"]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
+	return PyUnicode_FromFormat( "<bpy_%.200s, %.200s.%.200s>", type_fmt, RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
 }
 
 static long pyrna_struct_hash( BPy_StructRNA *self )
@@ -1022,7 +1050,7 @@
 }
 
 //---------------sequence-------------------------------------------
-static int pyrna_prop_array_length(BPy_PropertyRNA *self)
+static Py_ssize_t pyrna_prop_array_length(BPy_PropertyRNA *self)
 {
 	if (RNA_property_array_dimension(&self->ptr, self->prop, NULL) > 1)
 		return RNA_property_multi_array_length(&self->ptr, self->prop, self->arraydim);
@@ -1030,24 +1058,13 @@
 		return RNA_property_array_length(&self->ptr, self->prop);
 }
 
-static Py_ssize_t pyrna_prop_len( BPy_PropertyRNA *self )
+static Py_ssize_t pyrna_prop_collection_length( BPy_PropertyRNA *self )
 {
-	Py_ssize_t len;
-	
-	if (RNA_property_type(self->prop) == PROP_COLLECTION) {
-		len = RNA_property_collection_length(&self->ptr, self->prop);
-	} else if (RNA_property_array_check(&self->ptr, self->prop)) {
-		len = pyrna_prop_array_length(self);
-	} else {
-		PyErr_SetString(PyExc_AttributeError, "len() only available for collection and array RNA types");
-		len = -1; /* error value */
-	}
-	
-	return len;
+	return RNA_property_collection_length(&self->ptr, self->prop);
 }
 
 /* internal use only */
-static PyObject *prop_subscript_collection_int(BPy_PropertyRNA *self, int keynum)
+static PyObject *pyrna_prop_collection_subscript_int(BPy_PropertyRNA *self, Py_ssize_t keynum)
 {
 	PointerRNA newptr;
 
@@ -1060,7 +1077,7 @@
 	return NULL;
 }
 
-static PyObject *prop_subscript_array_int(BPy_PropertyRNA *self, int keynum)
+static PyObject *pyrna_prop_array_subscript_int(BPy_PropertyRNA *self, int keynum)
 {
 	int len= pyrna_prop_array_length(self);
 
@@ -1073,7 +1090,7 @@
 	return NULL;
 }
 
-static PyObject *prop_subscript_collection_str(BPy_PropertyRNA *self, char *keyname)
+static PyObject *pyrna_prop_collection_subscript_str(BPy_PropertyRNA *self, char *keyname)
 {
 	PointerRNA newptr;
 	if(RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
@@ -1082,9 +1099,9 @@
 	PyErr_Format(PyExc_KeyError, "key \"%.200s\" not found", keyname);
 	return NULL;
 }
-/* static PyObject *prop_subscript_array_str(BPy_PropertyRNA *self, char *keyname) */
+/* static PyObject *pyrna_prop_array_subscript_str(BPy_PropertyRNA *self, char *keyname) */
 
-static PyObject *prop_subscript_collection_slice(PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length)
+static PyObject *pyrna_prop_collection_subscript_slice(PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length)
 {
 	PointerRNA newptr;
 	PyObject *list = PyList_New(stop - start);
@@ -1111,7 +1128,7 @@
  * note: could also use pyrna_prop_to_py_index(self, count) in a loop but its a lot slower
  * since at the moment it reads (and even allocates) the entire array for each index.
  */
-static PyObject *prop_subscript_array_slice(BPy_PropertyRNA *self, PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length)
+static PyObject *pyrna_prop_array_subscript_slice(BPy_PropertyRNA *self, PointerRNA *ptr, PropertyRNA *prop, int start, int stop, int length)
 {
 	int count, totdim;
 
@@ -1183,17 +1200,17 @@
 	return list;
 }
 
-static PyObject *prop_subscript_collection(BPy_PropertyRNA *self, PyObject *key)
+static PyObject *pyrna_prop_collection_subscript(BPy_PropertyRNA *self, PyObject *key)
 {
 	if (PyUnicode_Check(key)) {
-		return prop_subscript_collection_str(self, _PyUnicode_AsString(key));
+		return pyrna_prop_collection_subscript_str(self, _PyUnicode_AsString(key));
 	}
 	else if (PyIndex_Check(key)) {
 		Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
 		if (i == -1 && PyErr_Occurred())
 			return NULL;
 
-		return prop_subscript_collection_int(self, i);
+		return pyrna_prop_collection_subscript_int(self, i);
 	}
 	else if (PySlice_Check(key)) {
 		int len= RNA_property_collection_length(&self->ptr, self->prop);
@@ -1206,7 +1223,7 @@
 			return PyList_New(0);
 		}
 		else if (step == 1) {
-			return prop_subscript_collection_slice(&self->ptr, self->prop, start, stop, len);

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list