[Bf-blender-cvs] [42d65ef] master: Add bool parser for PyArg_ParseTuple

Campbell Barton noreply at git.blender.org
Mon Aug 3 12:05:57 CEST 2015


Commit: 42d65ef5cc9bd4b9c398957efc95828ec0f63e49
Author: Campbell Barton
Date:   Mon Aug 3 20:00:16 2015 +1000
Branches: master
https://developer.blender.org/rB42d65ef5cc9bd4b9c398957efc95828ec0f63e49

Add bool parser for PyArg_ParseTuple

Use for mathutils.bvhtree

===================================================================

M	source/blender/python/generic/py_capi_utils.c
M	source/blender/python/generic/py_capi_utils.h
M	source/blender/python/mathutils/mathutils_bvhtree.c

===================================================================

diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c
index d53e562..9e6ffe9 100644
--- a/source/blender/python/generic/py_capi_utils.c
+++ b/source/blender/python/generic/py_capi_utils.c
@@ -1024,3 +1024,21 @@ int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename
 
 	return error_ret;
 }
+
+/**
+ * Use with PyArg_ParseTuple's "O&" formatting.
+ */
+int PyC_ParseBool(PyObject *o, void *p)
+{
+	bool *bool_p = p;
+	long value;
+	if (((value = PyLong_AsLong(o)) == -1) || !ELEM(value, 0, 1)) {
+		PyErr_Format(PyExc_ValueError,
+		             "expected a bool or int (0/1), got %s",
+		             Py_TYPE(o)->tp_name);
+		return 0;
+	}
+
+	*bool_p = value ? true : false;
+	return 1;
+}
diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h
index 93a3cb5..0ebc06c 100644
--- a/source/blender/python/generic/py_capi_utils.h
+++ b/source/blender/python/generic/py_capi_utils.h
@@ -81,4 +81,6 @@ PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag);
 
 int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename);
 
+int PyC_ParseBool(PyObject *o, void *p);
+
 #endif  /* __PY_CAPI_UTILS_H__ */
diff --git a/source/blender/python/mathutils/mathutils_bvhtree.c b/source/blender/python/mathutils/mathutils_bvhtree.c
index b3b9532..e749644 100644
--- a/source/blender/python/mathutils/mathutils_bvhtree.c
+++ b/source/blender/python/mathutils/mathutils_bvhtree.c
@@ -563,7 +563,7 @@ static PyObject *C_BVHTree_FromPolygons(PyObject *UNUSED(cls), PyObject *args, P
 	unsigned int (*tris)[3] = NULL;
 	unsigned int coords_len, tris_len;
 	float epsilon = 0.0f;
-	int all_triangles = 0;
+	bool all_triangles = false;
 
 	/* when all_triangles is False */
 	int *orig_index = NULL;
@@ -574,8 +574,10 @@ static PyObject *C_BVHTree_FromPolygons(PyObject *UNUSED(cls), PyObject *args, P
 
 
 	if (!PyArg_ParseTupleAndKeywords(
-	        args, kwargs, (char *)"OO|$if:BVHTree.FromPolygons", (char **)keywords,
-	        &py_coords, &py_tris, &all_triangles, &epsilon))
+	        args, kwargs, (char *)"OO|$O&f:BVHTree.FromPolygons", (char **)keywords,
+	        &py_coords, &py_tris,
+	        PyC_ParseBool, &all_triangles,
+	        &epsilon))
 	{
 		return NULL;
 	}
@@ -996,9 +998,9 @@ static PyObject *C_BVHTree_FromObject(PyObject *UNUSED(cls), PyObject *args, PyO
 	Object *ob;
 	struct Scene *scene;
 	DerivedMesh *dm;
-	int use_deform = true;
-	int use_render = false;
-	int use_cage = false;
+	bool use_deform = true;
+	bool use_render = false;
+	bool use_cage = false;
 
 	const MLoopTri *lt;
 	const MLoop *mloop;
@@ -1009,8 +1011,12 @@ static PyObject *C_BVHTree_FromObject(PyObject *UNUSED(cls), PyObject *args, PyO
 	float epsilon = 0.0f;
 
 	if (!PyArg_ParseTupleAndKeywords(
-	        args, kwargs, (char *)"OO|$iiif:BVHTree.FromObject", (char **)keywords,
-	        &py_ob, &py_scene, &use_deform, &use_render, &use_cage, &epsilon) ||
+	        args, kwargs, (char *)"OO|$O&O&O&f:BVHTree.FromObject", (char **)keywords,
+	        &py_ob, &py_scene,
+	        PyC_ParseBool, &use_deform,
+	        PyC_ParseBool, &use_render,
+	        PyC_ParseBool, &use_cage,
+	        &epsilon) ||
 	    ((ob = PyC_RNA_AsPointer(py_ob, "Object")) == NULL) ||
 	    ((scene = PyC_RNA_AsPointer(py_scene, "Scene")) == NULL))
 	{




More information about the Bf-blender-cvs mailing list