[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [21456] branches/blender2.5/blender/source /blender/python/intern/bpy_rna.c: Dictionary style get() to property rna.

Campbell Barton ideasman42 at gmail.com
Thu Jul 9 10:06:28 CEST 2009


Revision: 21456
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=21456
Author:   campbellbarton
Date:     2009-07-09 10:06:26 +0200 (Thu, 09 Jul 2009)

Log Message:
-----------
Dictionary style get() to property rna.
eg..
mesh = bpy.data.meshes.get("SomeMesh", fallback)

Set length limits for python string formatting.

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c	2009-07-09 07:35:35 UTC (rev 21455)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c	2009-07-09 08:06:26 UTC (rev 21456)
@@ -172,12 +172,12 @@
 	/* print name if available */
 	name= RNA_struct_name_get_alloc(&self->ptr, NULL, 0);
 	if(name) {
-		pyob= PyUnicode_FromFormat( "[BPy_StructRNA \"%s\" -> \"%s\"]", RNA_struct_identifier(self->ptr.type), name);
+		pyob= PyUnicode_FromFormat( "[BPy_StructRNA \"%.200s\" -> \"%.200s\"]", RNA_struct_identifier(self->ptr.type), name);
 		MEM_freeN(name);
 		return pyob;
 	}
 
-	return PyUnicode_FromFormat( "[BPy_StructRNA \"%s\"]", RNA_struct_identifier(self->ptr.type));
+	return PyUnicode_FromFormat( "[BPy_StructRNA \"%.200s\"]", RNA_struct_identifier(self->ptr.type));
 }
 
 static PyObject *pyrna_prop_repr( BPy_PropertyRNA * self )
@@ -192,13 +192,13 @@
 		name= RNA_struct_name_get_alloc(&ptr, NULL, 0);
 
 		if(name) {
-			pyob= PyUnicode_FromFormat( "[BPy_PropertyRNA \"%s\" -> \"%s\" -> \"%s\" ]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop), name);
+			pyob= PyUnicode_FromFormat( "[BPy_PropertyRNA \"%.200s\" -> \"%.200s\" -> \"%.200s\" ]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop), name);
 			MEM_freeN(name);
 			return pyob;
 		}
 	}
 
-	return PyUnicode_FromFormat( "[BPy_PropertyRNA \"%s\" -> \"%s\"]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
+	return PyUnicode_FromFormat( "[BPy_PropertyRNA \"%.200s\" -> \"%.200s\"]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
 }
 
 static long pyrna_struct_hash( BPy_StructRNA * self )
@@ -375,7 +375,7 @@
 		if (strcmp(arg_name, "rna_type")==0) continue;
 
 		if (kw==NULL) {
-			PyErr_Format( PyExc_AttributeError, "%s: no keywords, expected \"%s\"", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
+			PyErr_Format( PyExc_AttributeError, "%.200s: no keywords, expected \"%.200s\"", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
 			error_val= -1;
 			break;
 		}
@@ -383,7 +383,7 @@
 		item= PyDict_GetItemString(kw, arg_name);
 
 		if (item == NULL) {
-			PyErr_Format( PyExc_AttributeError, "%s: keyword \"%s\" missing", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
+			PyErr_Format( PyExc_AttributeError, "%.200s: keyword \"%.200s\" missing", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
 			error_val = -1; /* pyrna_py_to_prop sets the error */
 			break;
 		}
@@ -407,7 +407,7 @@
 			arg_name= NULL;
 		}
 
-		PyErr_Format( PyExc_AttributeError, "%s: keyword \"%s\" unrecognized", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
+		PyErr_Format( PyExc_AttributeError, "%.200s: keyword \"%.200s\" unrecognized", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
 		error_val = -1;
 	}
 
@@ -459,7 +459,7 @@
 			py_len= (int)PySequence_Length(value);
 		}
 		else {
-			PyErr_Format(PyExc_TypeError, "RNA array assignment expected a sequence instead of %s instance.", Py_TYPE(value)->tp_name);
+			PyErr_Format(PyExc_TypeError, "RNA array assignment expected a sequence instead of %.200s instance.", Py_TYPE(value)->tp_name);
 			return -1;
 		}
 		/* done getting the length */
@@ -620,7 +620,7 @@
 			
 			if (param==NULL) {
 				char *enum_str= pyrna_enum_as_string(ptr, prop);
-				PyErr_Format(PyExc_TypeError, "expected a string enum type in (%s)", enum_str);
+				PyErr_Format(PyExc_TypeError, "expected a string enum type in (%.200s)", enum_str);
 				MEM_freeN(enum_str);
 				return -1;
 			} else {
@@ -630,7 +630,7 @@
 					else		RNA_property_enum_set(ptr, prop, val);
 				} else {
 					char *enum_str= pyrna_enum_as_string(ptr, prop);
-					PyErr_Format(PyExc_AttributeError, "enum \"%s\" not found in (%s)", param, enum_str);
+					PyErr_Format(PyExc_AttributeError, "enum \"%.200s\" not found in (%.200s)", param, enum_str);
 					MEM_freeN(enum_str);
 					return -1;
 				}
@@ -645,7 +645,7 @@
 			if(!BPy_StructRNA_Check(value) && value != Py_None) {
 				PointerRNA tmp;
 				RNA_pointer_create(NULL, ptype, NULL, &tmp);
-				PyErr_Format(PyExc_TypeError, "expected a %s type", RNA_struct_identifier(tmp.type));
+				PyErr_Format(PyExc_TypeError, "expected a %.200s type", RNA_struct_identifier(tmp.type));
 				return -1;
 			} else {
 				BPy_StructRNA *param= (BPy_StructRNA*)value;
@@ -682,7 +682,7 @@
 					else {
 						PointerRNA tmp;
 						RNA_pointer_create(NULL, ptype, NULL, &tmp);
-						PyErr_Format(PyExc_TypeError, "expected a %s type", RNA_struct_identifier(tmp.type));
+						PyErr_Format(PyExc_TypeError, "expected a %.200s type", RNA_struct_identifier(tmp.type));
 						return -1;
 					}
 				}
@@ -690,7 +690,7 @@
 				if(raise_error) {
 					PointerRNA tmp;
 					RNA_pointer_create(NULL, ptype, NULL, &tmp);
-					PyErr_Format(PyExc_TypeError, "expected a %s type", RNA_struct_identifier(tmp.type));
+					PyErr_Format(PyExc_TypeError, "expected a %.200s type", RNA_struct_identifier(tmp.type));
 					return -1;
 				}
 			}
@@ -852,7 +852,7 @@
 	if(RNA_property_collection_lookup_int(&self->ptr, self->prop, keynum, &newptr))
 		return pyrna_struct_CreatePyObject(&newptr);
 
-	PyErr_SetString(PyExc_IndexError, "out of range");
+	PyErr_Format(PyExc_IndexError, "index %d out of range", keynum);
 	return NULL;
 }
 static PyObject *prop_subscript_array_int(BPy_PropertyRNA * self, int keynum)
@@ -864,7 +864,7 @@
 	if(keynum >= 0 && keynum < len)
 		return pyrna_prop_to_py_index(&self->ptr, self->prop, keynum);
 
-	PyErr_SetString(PyExc_IndexError, "out of range");
+	PyErr_Format(PyExc_IndexError, "index %d out of range", keynum);
 	return NULL;
 }
 
@@ -874,7 +874,7 @@
 	if(RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
 		return pyrna_struct_CreatePyObject(&newptr);
 
-	PyErr_SetString(PyExc_KeyError, "key not found");
+	PyErr_Format(PyExc_KeyError, "key \"%.200s\" not found", keyname);
 	return NULL;
 }
 /* static PyObject *prop_subscript_array_str(BPy_PropertyRNA * self, char *keyname) */
@@ -952,7 +952,7 @@
 	}
 #endif
 	else {
-		PyErr_SetString(PyExc_AttributeError, "invalid key, key must be a string or an int");
+		PyErr_Format(PyExc_TypeError, "invalid rna key, key must be a string or an int instead of %.200s instance.", Py_TYPE(key)->tp_name);
 		return NULL;
 	}
 }
@@ -1045,20 +1045,20 @@
 	/* char *keyname = NULL; */ /* not supported yet */
 	
 	if (!RNA_property_editable(&self->ptr, self->prop)) {
-		PyErr_Format( PyExc_AttributeError, "PropertyRNA - attribute \"%s\" from \"%s\" is read-only", RNA_property_identifier(self->prop), RNA_struct_identifier(self->ptr.type) );
+		PyErr_Format( PyExc_AttributeError, "PropertyRNA - attribute \"%.200s\" from \"%.200s\" is read-only", RNA_property_identifier(self->prop), RNA_struct_identifier(self->ptr.type) );
 		return -1;
 	}
 	
 	/* maybe one day we can support this... */
 	if (RNA_property_type(self->prop) == PROP_COLLECTION) {
-		PyErr_Format( PyExc_AttributeError, "PropertyRNA - attribute \"%s\" from \"%s\" is a collection, assignment not supported", RNA_property_identifier(self->prop), RNA_struct_identifier(self->ptr.type) );
+		PyErr_Format( PyExc_AttributeError, "PropertyRNA - attribute \"%.200s\" from \"%.200s\" is a collection, assignment not supported", RNA_property_identifier(self->prop), RNA_struct_identifier(self->ptr.type) );
 		return -1;
 	}
 
 	if (PyIndex_Check(key)) {
 		Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
 		if (i == -1 && PyErr_Occurred())
-			return NULL;
+			return -1;
 
 		return prop_subscript_ass_array_int(self, i, value);
 	}
@@ -1288,7 +1288,7 @@
 		BLI_freelistN(&newlb);
 	}
 	else {
-		PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%s\" not found", name);
+		PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%.200s\" not found", name);
 		ret = NULL;
 	}
 	
@@ -1306,13 +1306,13 @@
 			return 0;
 		}
 		else {
-			PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%s\" not found", name);
+			PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%.200s\" not found", name);
 			return -1;
 		}
 	}		
 	
 	if (!RNA_property_editable(&self->ptr, prop)) {
-		PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%s\" from \"%s\" is read-only", RNA_property_identifier(prop), RNA_struct_identifier(self->ptr.type) );
+		PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%.200s\" from \"%.200s\" is read-only", RNA_property_identifier(prop), RNA_struct_identifier(self->ptr.type) );
 		return -1;
 	}
 		
@@ -1415,6 +1415,24 @@
 	return ret;
 }
 
+static PyObject *pyrna_prop_get(BPy_PropertyRNA *self, PyObject *args)
+{
+	PointerRNA newptr;
+	
+	char *key;
+	PyObject* def = Py_None;
+
+	if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
+		return NULL;
+	
+	if(RNA_property_collection_lookup_string(&self->ptr, self->prop, key, &newptr))
+		return pyrna_struct_CreatePyObject(&newptr);
+	
+	Py_INCREF(def);
+	return def;
+}
+
+
 #if (PY_VERSION_HEX >= 0x03000000) /* foreach needs py3 */
 static void foreach_attr_type(	BPy_PropertyRNA *self, char *attr,
 									/* values to assign */
@@ -1698,6 +1716,8 @@
 	{"keys", (PyCFunction)pyrna_prop_keys, METH_NOARGS, NULL},
 	{"items", (PyCFunction)pyrna_prop_items, METH_NOARGS,NULL},
 	{"values", (PyCFunction)pyrna_prop_values, METH_NOARGS, NULL},
+	
+	{"get", (PyCFunction)pyrna_prop_get, METH_VARARGS, NULL},
 
 #if (PY_VERSION_HEX >= 0x03000000)
 	/* array accessor function */
@@ -1913,7 +1933,7 @@
 				tid= RNA_struct_identifier(self_ptr->type);
 				fid= RNA_function_identifier(self_func);
 
-				PyErr_Format(PyExc_AttributeError, "%s.%s(): required parameter \"%s\" not specified", tid, fid, pid);
+				PyErr_Format(PyExc_AttributeError, "%.200s.%.200s(): required parameter \"%.200s\" not specified", tid, fid, pid);
 				err= -1;
 				break;
 			}
@@ -2325,12 +2345,12 @@
 	if (RNA_property_collection_lookup_string(&self->ptr, self->prop, _PyUnicode_AsString(pyname), &newptr)) {
 		ret= pyrna_struct_Subtype(&newptr);
 		if (ret==NULL) {
-			PyErr_Format(PyExc_SystemError, "bpy.types.%s subtype could not be generated, this is a bug!", _PyUnicode_AsString(pyname));
+			PyErr_Format(PyExc_SystemError, "bpy.types.%.200s subtype could not be generated, this is a bug!", _PyUnicode_AsString(pyname));
 		}
 		return ret;
 	}

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list