[Bf-blender-cvs] [9fd569a] master: PyAPI: add utilities PyTuple_SET_ITEMS, Py_INCREF_RET

Campbell Barton noreply at git.blender.org
Tue Jan 6 09:13:44 CET 2015


Commit: 9fd569a654ded46901c7f20c5fe080972cbb10d2
Author: Campbell Barton
Date:   Tue Jan 6 16:42:22 2015 +1100
Branches: master
https://developer.blender.org/rB9fd569a654ded46901c7f20c5fe080972cbb10d2

PyAPI: add utilities PyTuple_SET_ITEMS, Py_INCREF_RET

Setting all values of a tuple is such a common operation that it deserves its own macro.
Also added Py_INCREF_RET to avoid confusing use of comma operator.

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

M	source/blender/blenlib/BLI_utildefines.h
M	source/blender/freestyle/intern/python/BPy_ContextFunctions.cpp
M	source/blender/freestyle/intern/python/BPy_Convert.cpp
M	source/blender/freestyle/intern/python/BPy_Convert.h
M	source/blender/freestyle/intern/python/Interface0D/BPy_SVertex.cpp
M	source/blender/python/bmesh/bmesh_py_ops_call.c
M	source/blender/python/bmesh/bmesh_py_types_customdata.c
M	source/blender/python/bmesh/bmesh_py_types_meshdata.c
M	source/blender/python/bmesh/bmesh_py_utils.c
M	source/blender/python/generic/blf_py_api.c
M	source/blender/python/generic/idprop_py_api.c
M	source/blender/python/generic/py_capi_utils.c
M	source/blender/python/generic/py_capi_utils.h
A	source/blender/python/generic/python_utildefines.h
M	source/blender/python/intern/bpy_app.c
M	source/blender/python/intern/bpy_app_handlers.c
M	source/blender/python/intern/bpy_app_translations.c
M	source/blender/python/intern/bpy_library.c
M	source/blender/python/intern/bpy_operator.c
M	source/blender/python/intern/bpy_rna.c
M	source/blender/python/mathutils/mathutils.c
M	source/blender/python/mathutils/mathutils_Color.c
M	source/blender/python/mathutils/mathutils_Euler.c
M	source/blender/python/mathutils/mathutils_Matrix.c
M	source/blender/python/mathutils/mathutils_Quaternion.c
M	source/blender/python/mathutils/mathutils_geometry.c
M	source/blender/python/mathutils/mathutils_kdtree.c

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

diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h
index 53896bb..470219b 100644
--- a/source/blender/blenlib/BLI_utildefines.h
+++ b/source/blender/blenlib/BLI_utildefines.h
@@ -55,7 +55,8 @@ extern "C" {
 	_49_, _50_, _51_, _52_, _53_, _54_, _55_, _56_, _57_, _58_, _59_, _60_, _61_, _62_, _63_, _64_, \
 	count, ...) count
 #define _VA_NARGS_EXPAND(args) _VA_NARGS_RETURN_COUNT args
-#define _VA_NARGS_COUNT_MAX64(...) _VA_NARGS_EXPAND((__VA_ARGS__, \
+/* 64 args max */
+#define _VA_NARGS_COUNT(...) _VA_NARGS_EXPAND((__VA_ARGS__, \
 	64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, \
 	48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, \
 	32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, \
@@ -65,7 +66,7 @@ extern "C" {
 #define _VA_NARGS_OVERLOAD_MACRO(name,  count) _VA_NARGS_OVERLOAD_MACRO1(name, count)
 /* --- expose for re-use --- */
 #define VA_NARGS_CALL_OVERLOAD(name, ...) \
-	_VA_NARGS_GLUE(_VA_NARGS_OVERLOAD_MACRO(name, _VA_NARGS_COUNT_MAX64(__VA_ARGS__)), (__VA_ARGS__))
+	_VA_NARGS_GLUE(_VA_NARGS_OVERLOAD_MACRO(name, _VA_NARGS_COUNT(__VA_ARGS__)), (__VA_ARGS__))
 
 /* useful for finding bad use of min/max */
 #if 0
@@ -442,6 +443,44 @@ extern "C" {
 #  define ARRAY_SIZE(arr)  (sizeof(arr) / sizeof(*(arr)))
 #endif
 
+/* ELEM#(v, ...): is the first arg equal any others? */
+/* internal helpers*/
+#define _VA_ARRAY_SET_ITEMS2(v, a) \
+       ((v)[0] = (a))
+#define _VA_ARRAY_SET_ITEMS3(v, a, b) \
+       _VA_ARRAY_SET_ITEMS2(v, a); ((v)[1] = (b))
+#define _VA_ARRAY_SET_ITEMS4(v, a, b, c) \
+       _VA_ARRAY_SET_ITEMS3(v, a, b); ((v)[2] = (c))
+#define _VA_ARRAY_SET_ITEMS5(v, a, b, c, d) \
+       _VA_ARRAY_SET_ITEMS4(v, a, b, c); ((v)[3] = (d))
+#define _VA_ARRAY_SET_ITEMS6(v, a, b, c, d, e) \
+       _VA_ARRAY_SET_ITEMS5(v, a, b, c, d); ((v)[4] = (e))
+#define _VA_ARRAY_SET_ITEMS7(v, a, b, c, d, e, f) \
+       _VA_ARRAY_SET_ITEMS6(v, a, b, c, d, e); ((v)[5] = (f))
+#define _VA_ARRAY_SET_ITEMS8(v, a, b, c, d, e, f, g) \
+       _VA_ARRAY_SET_ITEMS7(v, a, b, c, d, e, f); ((v)[6] = (g))
+#define _VA_ARRAY_SET_ITEMS9(v, a, b, c, d, e, f, g, h) \
+       _VA_ARRAY_SET_ITEMS8(v, a, b, c, d, e, f, g); ((v)[7] = (h))
+#define _VA_ARRAY_SET_ITEMS10(v, a, b, c, d, e, f, g, h, i) \
+       _VA_ARRAY_SET_ITEMS9(v, a, b, c, d, e, f, g, h); ((v)[8] = (i))
+#define _VA_ARRAY_SET_ITEMS11(v, a, b, c, d, e, f, g, h, i, j) \
+       _VA_ARRAY_SET_ITEMS10(v, a, b, c, d, e, f, g, h, i); ((v)[9] = (j))
+#define _VA_ARRAY_SET_ITEMS12(v, a, b, c, d, e, f, g, h, i, j, k) \
+       _VA_ARRAY_SET_ITEMS11(v, a, b, c, d, e, f, g, h, i, j); ((v)[10] = (k))
+#define _VA_ARRAY_SET_ITEMS13(v, a, b, c, d, e, f, g, h, i, j, k, l) \
+       _VA_ARRAY_SET_ITEMS12(v, a, b, c, d, e, f, g, h, i, j, k); ((v)[11] = (l))
+#define _VA_ARRAY_SET_ITEMS14(v, a, b, c, d, e, f, g, h, i, j, k, l, m) \
+       _VA_ARRAY_SET_ITEMS13(v, a, b, c, d, e, f, g, h, i, j, k, l); ((v)[12] = (m))
+#define _VA_ARRAY_SET_ITEMS15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
+       _VA_ARRAY_SET_ITEMS14(v, a, b, c, d, e, f, g, h, i, j, k, l, m); ((v)[13] = (n))
+#define _VA_ARRAY_SET_ITEMS16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
+       _VA_ARRAY_SET_ITEMS15(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n); ((v)[14] = (o))
+#define _VA_ARRAY_SET_ITEMS17(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
+       _VA_ARRAY_SET_ITEMS16(v, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o); ((v)[15] = (p))
+
+/* reusable ELEM macro */
+#define ARRAY_SET_ITEMS(...) { VA_NARGS_CALL_OVERLOAD(_VA_ARRAY_SET_ITEMS, __VA_ARGS__); } (void)0
+
 /* Like offsetof(typeof(), member), for non-gcc compilers */
 #define OFFSETOF_STRUCT(_struct, _member) \
 	((((char *)&((_struct)->_member)) - ((char *)(_struct))) + sizeof((_struct)->_member))
diff --git a/source/blender/freestyle/intern/python/BPy_ContextFunctions.cpp b/source/blender/freestyle/intern/python/BPy_ContextFunctions.cpp
index dd678ee..ad54a81 100644
--- a/source/blender/freestyle/intern/python/BPy_ContextFunctions.cpp
+++ b/source/blender/freestyle/intern/python/BPy_ContextFunctions.cpp
@@ -92,10 +92,11 @@ ContextFunctions_get_border(PyObject *self)
 {
 	BBox<Vec2i> border(ContextFunctions::GetBorderCF());
 	PyObject *v = PyTuple_New(4);
-	PyTuple_SET_ITEM(v, 0, PyLong_FromLong(border.getMin().x()));
-	PyTuple_SET_ITEM(v, 1, PyLong_FromLong(border.getMin().y()));
-	PyTuple_SET_ITEM(v, 2, PyLong_FromLong(border.getMax().x()));
-	PyTuple_SET_ITEM(v, 3, PyLong_FromLong(border.getMax().y()));
+	PyTuple_SET_ITEMS(v,
+	        PyLong_FromLong(border.getMin().x()),
+	        PyLong_FromLong(border.getMin().y()),
+	        PyLong_FromLong(border.getMax().x()),
+	        PyLong_FromLong(border.getMax().y()));
 	return v;
 }
 
diff --git a/source/blender/freestyle/intern/python/BPy_Convert.cpp b/source/blender/freestyle/intern/python/BPy_Convert.cpp
index b0b43ac..4e1a0a1 100644
--- a/source/blender/freestyle/intern/python/BPy_Convert.cpp
+++ b/source/blender/freestyle/intern/python/BPy_Convert.cpp
@@ -401,8 +401,9 @@ PyObject *BPy_CurvePoint_from_CurvePoint(CurvePoint& cp)
 PyObject *BPy_directedViewEdge_from_directedViewEdge(ViewVertex::directedViewEdge& dve)
 {
 	PyObject *py_dve = PyTuple_New(2);
-	PyTuple_SET_ITEM(py_dve, 0, BPy_ViewEdge_from_ViewEdge(*(dve.first)));
-	PyTuple_SET_ITEM(py_dve, 1, PyBool_from_bool(dve.second));
+	PyTuple_SET_ITEMS(py_dve,
+	        BPy_ViewEdge_from_ViewEdge(*(dve.first)),
+	        PyBool_from_bool(dve.second));
 	return py_dve;
 }
 
diff --git a/source/blender/freestyle/intern/python/BPy_Convert.h b/source/blender/freestyle/intern/python/BPy_Convert.h
index e6e763e..35c1e58 100644
--- a/source/blender/freestyle/intern/python/BPy_Convert.h
+++ b/source/blender/freestyle/intern/python/BPy_Convert.h
@@ -92,6 +92,7 @@ extern "C" {
 ///////////////////////////////////////////////////////////////////////////////////////////
 
 #include "mathutils/mathutils.h"
+#include "generic/python_utildefines.h"
 
 //==============================
 // C++ => Python
diff --git a/source/blender/freestyle/intern/python/Interface0D/BPy_SVertex.cpp b/source/blender/freestyle/intern/python/Interface0D/BPy_SVertex.cpp
index 6845bc3..1d51bf2 100644
--- a/source/blender/freestyle/intern/python/Interface0D/BPy_SVertex.cpp
+++ b/source/blender/freestyle/intern/python/Interface0D/BPy_SVertex.cpp
@@ -400,13 +400,14 @@ static PyObject *SVertex_curvatures_get(BPy_SVertex *self, void *UNUSED(closure)
 	Vec3r e2(info->e2.x(), info->e2.y(), info->e2.z());
 	Vec3r er(info->er.x(), info->er.y(), info->er.z());
 	PyObject *retval = PyTuple_New(7);
-	PyTuple_SET_ITEM(retval, 0, PyFloat_FromDouble(info->K1));
-	PyTuple_SET_ITEM(retval, 2, Vector_from_Vec3r(e1));
-	PyTuple_SET_ITEM(retval, 1, PyFloat_FromDouble(info->K2));
-	PyTuple_SET_ITEM(retval, 3, Vector_from_Vec3r(e2));
-	PyTuple_SET_ITEM(retval, 4, PyFloat_FromDouble(info->Kr));
-	PyTuple_SET_ITEM(retval, 5, Vector_from_Vec3r(er));
-	PyTuple_SET_ITEM(retval, 6, PyFloat_FromDouble(info->dKr));
+	PyTuple_SET_ITEMS(retval,
+	        PyFloat_FromDouble(info->K1),
+	        PyFloat_FromDouble(info->K2),
+	        Vector_from_Vec3r(e1),
+	        Vector_from_Vec3r(e2),
+	        PyFloat_FromDouble(info->Kr),
+	        Vector_from_Vec3r(er),
+	        PyFloat_FromDouble(info->dKr));
 	return retval;
 }
 
diff --git a/source/blender/python/bmesh/bmesh_py_ops_call.c b/source/blender/python/bmesh/bmesh_py_ops_call.c
index e8ef4c5..84c1031 100644
--- a/source/blender/python/bmesh/bmesh_py_ops_call.c
+++ b/source/blender/python/bmesh/bmesh_py_ops_call.c
@@ -43,6 +43,8 @@
 
 #include "bmesh_py_types.h"
 
+#include "../generic/python_utildefines.h"
+
 static int bpy_bm_op_as_py_error(BMesh *bm)
 {
 	if (BMO_error_occurred(bm)) {
@@ -547,13 +549,13 @@ static PyObject *bpy_slot_to_py(BMesh *bm, BMOpSlot *slot)
 			break;
 		case BMO_OP_SLOT_PTR:
 			BLI_assert(0);  /* currently we don't have any pointer return values in use */
-			item = (Py_INCREF(Py_None), Py_None);
+			item = Py_INCREF_RET(Py_None);
 			break;
 		case BMO_OP_SLOT_ELEMENT_BUF:
 		{
 			if (slot->slot_subtype.elem & BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE) {
 				BMHeader *ele = BMO_slot_buffer_get_single(slot);
-				item = ele ? BPy_BMElem_CreatePyObject(bm, ele) : (Py_INCREF(Py_None), Py_None);
+				item = ele ? BPy_BMElem_CreatePyObject(bm, ele) : Py_INCREF_RET(Py_None);
 			}
 			else {
 				const int size = slot->len;
@@ -664,7 +666,7 @@ static PyObject *bpy_slot_to_py(BMesh *bm, BMOpSlot *slot)
 				}
 				case BMO_OP_SLOT_SUBTYPE_MAP_INTERNAL:
 					/* can't convert from these */
-					item = (Py_INCREF(Py_None), Py_None);
+					item = Py_INCREF_RET(Py_None);
 					break;
 			}
 			break;
@@ -743,7 +745,7 @@ PyObject *BPy_BMO_call(BPy_BMeshOpFunc *self, PyObject *args, PyObject *kw)
 		ret = NULL;  /* exception raised above */
 	}
 	else if (bmop.slots_out[0].slot_name == NULL) {
-		ret = (Py_INCREF(Py_None), Py_None);
+		ret = Py_INCREF_RET(Py_None);
 	}
 	else {
 		/* build return value */
@@ -759,7 +761,7 @@ PyObject *BPy_BMO_call(BPy_BMeshOpFunc *self, PyObject *args, PyObject *kw)
 			/* this function doesn't throw exceptions */
 			item = bpy_slot_to_py(bm, slot);
 			if (item == NULL) {
-				item = (Py_INCREF(Py_None), Py_None);
+				item = Py_INCREF_RET(Py_None);
 			}
 
 #if 1
diff --git a/source/blender/python/bmesh/bmesh_py_types_customdata.c b/source/blender/python/bmesh/bmesh_py_types_customdata.c
index 3c1502d..bfcd91a 100644
--- a/source/blender/python/bmesh/bmesh_py_types_customdata.c
+++ b/source/blender/python/bmesh/bmesh_py_types_customdata.c
@@ -42,6 +42,7 @@
 #include "bmesh_py_types_meshdata.h"
 
 #include "../mathutils/mathutils.h"
+#include "../generic/python_utildefines.h"
 
 #include "BKE_customdata.h"
 
@@ -483,8 +484,9 @@ static PyObject *bpy_bmlayercollection_items(BPy_BMLayerCollection *self)
 
 	for (i = 0; tot-- > 0; index++) {
 		item = PyTuple_New(2);
-		PyTuple_SET_ITEM(item, 0, PyUnicode_FromString(data->layers[index].name));
-		PyTuple_SET_ITEM(item, 1, BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index));
+		PyTuple_SET_ITEMS(item,
+		        PyUnicode_FromString(data->layers[index].name),
+		        BPy_B

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list