[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [54984] branches/soc-2008-mxcurioni/source /blender/freestyle/intern/python/BPy_Convert.cpp: Fix for exceptions in converting Python float objects to C variables not properly handled .

Tamito Kajiyama rd6t-kjym at asahi-net.or.jp
Sun Mar 3 02:53:50 CET 2013


Revision: 54984
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=54984
Author:   kjym3
Date:     2013-03-03 01:53:49 +0000 (Sun, 03 Mar 2013)
Log Message:
-----------
Fix for exceptions in converting Python float objects to C variables not properly handled.
Based on review comment from Campbell.

Modified Paths:
--------------
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp	2013-03-03 01:24:09 UTC (rev 54983)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_Convert.cpp	2013-03-03 01:53:49 UTC (rev 54984)
@@ -608,62 +608,94 @@
 	return new Vec3r(r,g,b);
 }
 
+static int float_array_from_PyList(PyObject *obj, float *v, int n)
+{
+	for (int i = 0; i < n; i++) {
+		v[i] = PyFloat_AsDouble(PyList_GetItem(obj, i));
+		if (v[i] == -1.0f && PyErr_Occurred()) {
+			PyErr_SetString(PyExc_TypeError, "list elements must be a number");
+			return 0;
+		}
+	}
+	return 1;
+}
+
 Vec2f * Vec2f_ptr_from_PyList(PyObject* obj)
 {
+	float v[2];
+
 	if (!PyList_Check(obj) || PyList_Size(obj) != 2)
 		return NULL;
-	float x = PyFloat_AsDouble(PyList_GetItem(obj, 0));
-	float y = PyFloat_AsDouble(PyList_GetItem(obj, 1));
-	return new Vec2f(x,y);
+	if (!float_array_from_PyList(obj, v, 2))
+		return NULL;
+	return new Vec2f(v[0], v[1]);
 }
 
 Vec3f * Vec3f_ptr_from_PyList(PyObject* obj)
 {
+	float v[3];
+
 	if (!PyList_Check(obj) || PyList_Size(obj) != 3)
 		return NULL;
-	float x = PyFloat_AsDouble(PyList_GetItem(obj, 0));
-	float y = PyFloat_AsDouble(PyList_GetItem(obj, 1));
-	float z = PyFloat_AsDouble(PyList_GetItem(obj, 2));
-	return new Vec3f(x,y,z);
+	if (!float_array_from_PyList(obj, v, 3))
+		return NULL;
+	return new Vec3f(v[0], v[1], v[2]);
 }
 
 Vec3r * Vec3r_ptr_from_PyList(PyObject* obj)
 {
+	float v[3];
+
 	if (!PyList_Check(obj) || PyList_Size(obj) != 3)
 		return NULL;
-	float x = PyFloat_AsDouble(PyList_GetItem(obj, 0));
-	float y = PyFloat_AsDouble(PyList_GetItem(obj, 1));
-	float z = PyFloat_AsDouble(PyList_GetItem(obj, 2));
-	return new Vec3r(x,y,z);
+	if (!float_array_from_PyList(obj, v, 3))
+		return NULL;
+	return new Vec3r(v[0], v[1], v[2]);
 }
 
+static int float_array_from_PyTuple(PyObject *obj, float *v, int n)
+{
+	for (int i = 0; i < n; i++) {
+		v[i] = PyFloat_AsDouble(PyTuple_GetItem(obj, i));
+		if (v[i] == -1.0f && PyErr_Occurred()) {
+			PyErr_SetString(PyExc_TypeError, "tuple elements must be a number");
+			return 0;
+		}
+	}
+	return 1;
+}
+
 Vec2f * Vec2f_ptr_from_PyTuple(PyObject* obj)
 {
+	float v[2];
+
 	if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2)
 		return NULL;
-	float x = PyFloat_AsDouble(PyTuple_GetItem(obj, 0));
-	float y = PyFloat_AsDouble(PyTuple_GetItem(obj, 1));
-	return new Vec2f(x,y);
+	if (!float_array_from_PyTuple(obj, v, 2))
+		return NULL;
+	return new Vec2f(v[0], v[1]);
 }
 
 Vec3f * Vec3f_ptr_from_PyTuple(PyObject* obj)
 {
+	float v[3];
+
 	if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 3)
 		return NULL;
-	float x = PyFloat_AsDouble(PyTuple_GetItem(obj, 0));
-	float y = PyFloat_AsDouble(PyTuple_GetItem(obj, 1));
-	float z = PyFloat_AsDouble(PyTuple_GetItem(obj, 2));
-	return new Vec3f(x,y,z);
+	if (!float_array_from_PyTuple(obj, v, 3))
+		return NULL;
+	return new Vec3f(v[0], v[1], v[2]);
 }
 
 Vec3r * Vec3r_ptr_from_PyTuple(PyObject* obj)
 {
+	float v[3];
+
 	if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 3)
 		return NULL;
-	float x = PyFloat_AsDouble(PyTuple_GetItem(obj, 0));
-	float y = PyFloat_AsDouble(PyTuple_GetItem(obj, 1));
-	float z = PyFloat_AsDouble(PyTuple_GetItem(obj, 2));
-	return new Vec3r(x,y,z);
+	if (!float_array_from_PyTuple(obj, v, 3))
+		return NULL;
+	return new Vec3r(v[0], v[1], v[2]);
 }
 
 // helper for argument parsing
@@ -673,19 +705,15 @@
 	if (VectorObject_Check(obj) && ((VectorObject *)obj)->size == n) {
 		for (int i = 0; i < n; i++)
 			v[i] = ((VectorObject *)obj)->vec[i];
+		return 1;
 	}
 	else if (PyList_Check(obj) && PyList_Size(obj) == n) {
-		for (int i = 0; i < n; i++)
-			v[i] = PyFloat_AsDouble(PyList_GetItem(obj, i));
+		return float_array_from_PyList(obj, v, n);
 	}
 	else if (PyTuple_Check(obj) && PyTuple_Size(obj) == n) {
-		for (int i = 0; i < n; i++)
-			v[i] = PyFloat_AsDouble(PyTuple_GetItem(obj, i));
+		return float_array_from_PyList(obj, v, n);
 	}
-	else {
-		return 0;
-	}
-	return 1;
+	return 0;
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////




More information about the Bf-blender-cvs mailing list