[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [60809] trunk/blender/source/blender/ python/generic/idprop_py_api.c: simplify & improve error handling for id-property python-api.

Campbell Barton ideasman42 at gmail.com
Thu Oct 17 04:57:59 CEST 2013


Revision: 60809
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=60809
Author:   campbellbarton
Date:     2013-10-17 02:57:59 +0000 (Thu, 17 Oct 2013)
Log Message:
-----------
simplify & improve error handling for id-property python-api.

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	2013-10-17 02:36:33 UTC (rev 60808)
+++ trunk/blender/source/blender/python/generic/idprop_py_api.c	2013-10-17 02:57:59 UTC (rev 60809)
@@ -25,15 +25,16 @@
  *  \ingroup pygen
  */
 
-
 #include <Python.h>
 
-#include "idprop_py_api.h"
 #include "MEM_guardedalloc.h"
 
 #include "BLI_string.h"
 #include "BLI_utildefines.h"
 
+#include "idprop_py_api.h"
+
+
 #include "BKE_idprop.h"
 
 
@@ -329,22 +330,30 @@
 	return type;
 }
 
-/* note: group can be a pointer array or a group.
- * assume we already checked key is a string. */
-const char *BPy_IDProperty_Map_ValidateAndCreate(PyObject *name_obj, IDProperty *group, PyObject *ob)
+/**
+ * \note group can be a pointer array or a group.
+ * assume we already checked key is a string.
+ *
+ * \return success.
+ */
+bool BPy_IDProperty_Map_ValidateAndCreate(PyObject *name_obj, IDProperty *group, PyObject *ob)
 {
 	IDProperty *prop = NULL;
 	IDPropertyTemplate val = {0};
 
-	const char *name = "";
+	const char *name;
 
 	if (name_obj) {
 		Py_ssize_t name_size;
 		name = _PyUnicode_AsStringAndSize(name_obj, &name_size);
 		if (name_size > MAX_IDPROP_NAME) {
-			return "the length of IDProperty names is limited to 63 characters";
+			PyErr_SetString(PyExc_KeyError, "the length of IDProperty names is limited to 63 characters");
+			return false;
 		}
 	}
+	else {
+		name = "";
+	}
 
 	if (PyFloat_Check(ob)) {
 		val.d = PyFloat_AsDouble(ob);
@@ -353,7 +362,7 @@
 	else if (PyLong_Check(ob)) {
 		val.i = _PyLong_AsInt(ob);
 		if (val.i == -1 && PyErr_Occurred()) {
-			return "error converting to an int";
+			return false;
 		}
 		prop = IDP_New(IDP_INT, &val, name);
 	}
@@ -384,14 +393,13 @@
 		int i;
 
 		if (ob_seq_fast == NULL) {
-			PyErr_Print();
-			PyErr_Clear();
-			return "error converting the sequence";
+			return false;
 		}
 
 		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";
+			PyErr_SetString(PyExc_TypeError, "only floats, ints and dicts are allowed in ID property arrays");
+			return false;
 		}
 
 		/* validate sequence and derive type.
@@ -418,19 +426,21 @@
 			case IDP_IDPARRAY:
 				prop = IDP_NewIDPArray(name);
 				for (i = 0; i < val.array.len; i++) {
-					const char *error;
+					bool ok;
 					item = PySequence_Fast_GET_ITEM(ob_seq_fast, i);
-					error = BPy_IDProperty_Map_ValidateAndCreate(NULL, prop, item);
+					ok = BPy_IDProperty_Map_ValidateAndCreate(NULL, prop, item);
 
-					if (error) {
+					if (ok == false) {
 						Py_DECREF(ob_seq_fast);
-						return error;
+						return ok;
 					}
 				}
 				break;
 			default:
+				/* should never happen */
 				Py_DECREF(ob_seq_fast);
-				return "internal error with idp array.type";
+				PyErr_SetString(PyExc_RuntimeError, "internal error with idp array.type");
+				return false;
 		}
 
 		Py_DECREF(ob_seq_fast);
@@ -456,16 +466,20 @@
 				Py_XDECREF(vals);
 				Py_XDECREF(key);
 				Py_XDECREF(pval);
-				return "invalid element in subgroup dict template!";
+				PyErr_Format(PyExc_TypeError,
+				             "invalid id-property key type expected a string, not %.200s",
+				             Py_TYPE(key)->tp_name);
+				return false;
 			}
-			if (BPy_IDProperty_Map_ValidateAndCreate(key, prop, pval)) {
+			if (BPy_IDProperty_Map_ValidateAndCreate(key, prop, pval) == false) {
 				IDP_FreeProperty(prop);
 				MEM_freeN(prop);
 				Py_XDECREF(keys);
 				Py_XDECREF(vals);
 				Py_XDECREF(key);
 				Py_XDECREF(pval);
-				return "invalid element in subgroup dict template!";
+				/* error is already set */
+				return false;
 			}
 			Py_XDECREF(key);
 			Py_XDECREF(pval);
@@ -474,7 +488,10 @@
 		Py_XDECREF(vals);
 	}
 	else {
-		return "invalid property value";
+		PyErr_Format(PyExc_TypeError,
+		             "invalid id-property type %.200s not supported",
+		             Py_TYPE(ob)->tp_name);
+		return false;
 	}
 
 	if (group->type == IDP_IDPARRAY) {
@@ -486,7 +503,7 @@
 		IDP_ReplaceInGroup(group, prop);
 	}
 
-	return NULL;
+	return true;
 }
 
 int BPy_Wrap_SetMapItem(IDProperty *prop, PyObject *key, PyObject *val)
@@ -508,16 +525,15 @@
 		}
 	}
 	else {
-		const char *err;
+		bool ok;
 
 		if (!PyUnicode_Check(key)) {
 			PyErr_SetString(PyExc_TypeError, "only strings are allowed as subgroup keys");
 			return -1;
 		}
 
-		err = BPy_IDProperty_Map_ValidateAndCreate(key, prop, val);
-		if (err) {
-			PyErr_SetString(PyExc_KeyError, err);
+		ok = BPy_IDProperty_Map_ValidateAndCreate(key, prop, val);
+		if (ok == false) {
 			return -1;
 		}
 




More information about the Bf-blender-cvs mailing list