PyAPI: add utilities PyTuple_SET_ITEMS, Py_INCREF_RET
authorCampbell Barton <ideasman42@gmail.com>
Tue, 6 Jan 2015 05:42:22 +0000 (16:42 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 6 Jan 2015 08:09:11 +0000 (19:09 +1100)
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.

27 files changed:
source/blender/blenlib/BLI_utildefines.h
source/blender/freestyle/intern/python/BPy_ContextFunctions.cpp
source/blender/freestyle/intern/python/BPy_Convert.cpp
source/blender/freestyle/intern/python/BPy_Convert.h
source/blender/freestyle/intern/python/Interface0D/BPy_SVertex.cpp
source/blender/python/bmesh/bmesh_py_ops_call.c
source/blender/python/bmesh/bmesh_py_types_customdata.c
source/blender/python/bmesh/bmesh_py_types_meshdata.c
source/blender/python/bmesh/bmesh_py_utils.c
source/blender/python/generic/blf_py_api.c
source/blender/python/generic/idprop_py_api.c
source/blender/python/generic/py_capi_utils.c
source/blender/python/generic/py_capi_utils.h
source/blender/python/generic/python_utildefines.h [new file with mode: 0644]
source/blender/python/intern/bpy_app.c
source/blender/python/intern/bpy_app_handlers.c
source/blender/python/intern/bpy_app_translations.c
source/blender/python/intern/bpy_library.c
source/blender/python/intern/bpy_operator.c
source/blender/python/intern/bpy_rna.c
source/blender/python/mathutils/mathutils.c
source/blender/python/mathutils/mathutils_Color.c
source/blender/python/mathutils/mathutils_Euler.c
source/blender/python/mathutils/mathutils_Matrix.c
source/blender/python/mathutils/mathutils_Quaternion.c
source/blender/python/mathutils/mathutils_geometry.c
source/blender/python/mathutils/mathutils_kdtree.c

index 53896bb31b62a164c4f2b89c57a599f0aea65927..470219b2042cd2a6fea375c705f76618ca866e1a 100644 (file)
@@ -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))
index dd678ee6fbd6c86eb2a054cd8e69fa099709d37e..ad54a81f156f246cffbad27802462bf7c0807fa9 100644 (file)
@@ -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;
 }
 
index b0b43acb8daaebe23ab7114f53f65db117fd8773..4e1a0a119aa4a1a6761f5d06529a03093cee264b 100644 (file)
@@ -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;
 }
 
index e6e763e763eefb7feac01fab00bae3b0a6d2ed3d..35c1e58369cda8fd9143600e1d67b49f99aaee61 100644 (file)
@@ -92,6 +92,7 @@ extern "C" {
 ///////////////////////////////////////////////////////////////////////////////////////////
 
 #include "mathutils/mathutils.h"
+#include "generic/python_utildefines.h"
 
 //==============================
 // C++ => Python
index 6845bc3ed127a4454108406bc9fec21d499a3b01..1d51bf287af05f887470aab30c6f3f52a6a9defb 100644 (file)
@@ -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;
 }
 
index e8ef4c588950548d9e7771cc5626f1adcf657db2..84c1031a24a40716bf3124dde40c876e46e56114 100644 (file)
@@ -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
index 3c1502dc72b05db24a9a31b72f530033c308b3b5..bfcd91ac72deb8d7e5dd54a5f72778637e3caa19 100644 (file)
@@ -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_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index));
                PyList_SET_ITEM(ret, i++, item);
        }
 
@@ -559,7 +561,7 @@ static PyObject *bpy_bmlayercollection_get(BPy_BMLayerCollection *self, PyObject
                }
        }
 
-       return Py_INCREF(def), def;
+       return Py_INCREF_RET(def);
 }
 
 static struct PyMethodDef bpy_bmlayeritem_methods[] = {
index df0c39fd3b3d29420288ccae73d7cd28e80e8f11..94f38ffdec795bc3d8f7fab1954f1bcbf4ba605d 100644 (file)
@@ -45,6 +45,8 @@
 
 #include "bmesh_py_types_meshdata.h"
 
+#include "../generic/python_utildefines.h"
+
 
 /* Mesh BMTexPoly
  * ************** */
@@ -684,10 +686,9 @@ static PyObject *bpy_bmdeformvert_items(BPy_BMDeformVert *self)
        ret = PyList_New(self->data->totweight);
        for (i = 0; i < self->data->totweight; i++, dw++) {
                item = PyTuple_New(2);
-
-               PyTuple_SET_ITEM(item, 0, PyLong_FromLong(dw->def_nr));
-               PyTuple_SET_ITEM(item, 1, PyFloat_FromDouble(dw->weight));
-
+               PyTuple_SET_ITEMS(item,
+                       PyLong_FromLong(dw->def_nr),
+                       PyFloat_FromDouble(dw->weight));
                PyList_SET_ITEM(ret, i, item);
        }
 
@@ -721,7 +722,7 @@ static PyObject *bpy_bmdeformvert_get(BPy_BMDeformVert *self, PyObject *args)
                        return PyFloat_FromDouble(dw->weight);
                }
                else {
-                       return Py_INCREF(def), def;
+                       return Py_INCREF_RET(def);
                }
        }
 }
index 7088036245ab14cdc7f8f6b2fb580e31d85360a6..0b667ab9029341769a75e5f3fbe4928aa2293ef0 100644 (file)
@@ -42,6 +42,8 @@
 #include "bmesh_py_types.h"
 #include "bmesh_py_utils.h" /* own include */
 
+#include "../generic/python_utildefines.h"
+
 
 PyDoc_STRVAR(bpy_bm_utils_vert_collapse_edge_doc,
 ".. method:: vert_collapse_edge(vert, edge)\n"
@@ -365,8 +367,9 @@ static PyObject *bpy_bm_utils_edge_split(PyObject *UNUSED(self), PyObject *args)
 
        if (v_new && e_new) {
                PyObject *ret = PyTuple_New(2);
-               PyTuple_SET_ITEM(ret, 0, BPy_BMEdge_CreatePyObject(bm, e_new));
-               PyTuple_SET_ITEM(ret, 1, BPy_BMVert_CreatePyObject(bm, v_new));
+               PyTuple_SET_ITEMS(ret,
+                       BPy_BMEdge_CreatePyObject(bm, e_new),
+                       BPy_BMVert_CreatePyObject(bm, v_new));
                return ret;
        }
        else {
@@ -524,8 +527,9 @@ static PyObject *bpy_bm_utils_face_split(PyObject *UNUSED(self), PyObject *args,
 
        if (f_new && l_new) {
                PyObject *ret = PyTuple_New(2);
-               PyTuple_SET_ITEM(ret, 0, BPy_BMFace_CreatePyObject(bm, f_new));
-               PyTuple_SET_ITEM(ret, 1, BPy_BMLoop_CreatePyObject(bm, l_new));
+               PyTuple_SET_ITEMS(ret,
+                       BPy_BMFace_CreatePyObject(bm, f_new),
+                       BPy_BMLoop_CreatePyObject(bm, l_new));
                return ret;
        }
        else {
index 655542e320aeb5a663cb5f4fdd0e893f96f68f9d..ed1ac7ceed9a7fa726d041379ea14f30de4fa055 100644 (file)
@@ -33,6 +33,9 @@
 
 #include "BLI_utildefines.h"
 
+#include "../generic/python_utildefines.h"
+
+
 PyDoc_STRVAR(py_blf_position_doc,
 ".. function:: position(fontid, x, y, z)\n"
 "\n"
@@ -183,8 +186,9 @@ static PyObject *py_blf_dimensions(PyObject *UNUSED(self), PyObject *args)
        BLF_width_and_height(fontid, text, INT_MAX, &r_width, &r_height);
 
        ret = PyTuple_New(2);
-       PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(r_width));
-       PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(r_height));
+       PyTuple_SET_ITEMS(ret,
+               PyFloat_FromDouble(r_width),
+               PyFloat_FromDouble(r_height));
        return ret;
 }
 
index be2782e90d6c9bec572bdfa552a19240b7c7c21a..1f35572a483fce284d3534a8db1cee7a27bd6ea4 100644 (file)
 
 #include "idprop_py_api.h"
 
-
 #include "BKE_idprop.h"
 
-
 #define USE_STRING_COERCE
 
 #ifdef USE_STRING_COERCE
 #include "py_capi_utils.h"
 #endif
 
+#include "../generic/python_utildefines.h"
+
+
 /*********************** ID Property Main Wrapper Stuff ***************/
 
 /* ----------------------------------------------------------------------------
@@ -746,10 +747,9 @@ static void BPy_IDGroup_CorrectListLen(IDProperty *prop, PyObject *seq, int len,
 
        printf("%s: ID Property Error found and corrected!\n", func);
 
-       /*fill rest of list with valid references to None*/
+       /* fill rest of list with valid references to None */
        for (j = len; j < prop->len; j++) {
-               Py_INCREF(Py_None);
-               PyList_SET_ITEM(seq, j, Py_None);
+               PyList_SET_ITEM(seq, j, Py_INCREF_RET(Py_None));
        }
 
        /*set correct group length*/
@@ -808,8 +808,9 @@ PyObject *BPy_Wrap_GetItems(ID *id, IDProperty *prop)
 
        for (i = 0, loop = prop->data.group.first; loop; loop = loop->next, i++) {
                PyObject *item = PyTuple_New(2);
-               PyTuple_SET_ITEM(item, 0, PyUnicode_FromString(loop->name));
-               PyTuple_SET_ITEM(item, 1, BPy_IDGroup_WrapData(id, loop, prop));
+               PyTuple_SET_ITEMS(item,
+                       PyUnicode_FromString(loop->name),
+                       BPy_IDGroup_WrapData(id, loop, prop));
                PyList_SET_ITEM(seq, i, item);
        }
 
@@ -1406,8 +1407,9 @@ static PyObject *BPy_Group_Iter_Next(BPy_IDGroup_Iter *self)
 
                if (self->mode == IDPROP_ITER_ITEMS) {
                        ret = PyTuple_New(2);
-                       PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(cur->name));
-                       PyTuple_SET_ITEM(ret, 1, BPy_IDGroup_WrapData(self->group->id, cur, self->group->prop));
+                       PyTuple_SET_ITEMS(ret,
+                               PyUnicode_FromString(cur->name),
+                               BPy_IDGroup_WrapData(self->group->id, cur, self->group->prop));
                        return ret;
                }
                else {
index 26deaf8d0338e684c28c09b43f054ceac163eec3..92ac82508e540d367ea05734aff3cabb88fff13d 100644 (file)
@@ -37,6 +37,8 @@
 
 #include "py_capi_utils.h"
 
+#include "../generic/python_utildefines.h"
+
 /* only for BLI_strncpy_wchar_from_utf8, should replace with py funcs but too late in release now */
 #include "BLI_string_utf8.h"
 
@@ -178,6 +180,17 @@ void PyC_Tuple_Fill(PyObject *tuple, PyObject *value)
        }
 }
 
+void PyC_List_Fill(PyObject *list, PyObject *value)
+{
+       unsigned int tot = PyList_GET_SIZE(list);
+       unsigned int i;
+
+       for (i = 0; i < tot; i++) {
+               PyList_SET_ITEM(list, i, value);
+               Py_INCREF(value);
+       }
+}
+
 /* for debugging */
 void PyC_ObSpit(const char *name, PyObject *var)
 {
@@ -664,8 +677,7 @@ void PyC_RunQuicky(const char *filepath, int n, ...)
                                PyErr_Print();
                                PyErr_Clear();
 
-                               PyList_SET_ITEM(values, i, Py_None); /* hold user */
-                               Py_INCREF(Py_None);
+                               PyList_SET_ITEM(values, i, Py_INCREF_RET(Py_None)); /* hold user */
 
                                sizes[i] = 0;
                        }
index 559a8e156782d18438b2dc12033796f88356fe3a..398d7da9179662ece1237e17d3a89360158e8e25 100644 (file)
@@ -42,6 +42,7 @@ int                           PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length,
 PyObject *      PyC_FromArray(const void *array, int length, const PyTypeObject *type,
                               const bool is_double, const char *error_prefix);
 void            PyC_Tuple_Fill(PyObject *tuple, PyObject *value);
+void            PyC_List_Fill(PyObject *list, PyObject *value);
 
 /* follow http://www.python.org/dev/peps/pep-0383/ */
 PyObject *      PyC_UnicodeFromByte(const char *str);
diff --git a/source/blender/python/generic/python_utildefines.h b/source/blender/python/generic/python_utildefines.h
new file mode 100644 (file)
index 0000000..555ad48
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/python/generic/python_utildefines.h
+ *  \ingroup pygen
+ *  \brief header-only utilities
+ *  \note light addition to Python.h, use py_capi_utils.h for larger features.
+ */
+
+#ifndef __PYTHON_UTILDEFINES_H__
+#define __PYTHON_UTILDEFINES_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define PyTuple_SET_ITEMS(op_arg, ...) \
+{ \
+       PyTupleObject *op = (PyTupleObject *)op_arg; \
+       PyObject **ob_items = op->ob_item; \
+       CHECK_TYPE_ANY(op_arg, PyObject *, PyTupleObject *); \
+       BLI_assert(_VA_NARGS_COUNT(__VA_ARGS__) == PyTuple_GET_SIZE(op)); \
+       ARRAY_SET_ITEMS(ob_items, __VA_ARGS__); \
+} (void)0
+
+/* wrap Py_INCREF & return the result,
+ * use sparingly to avoid comma operator or temp var assignment */
+BLI_INLINE PyObject *Py_INCREF_RET(PyObject *op) { Py_INCREF(op); return op; }
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  /* __PYTHON_UTILDEFINES_H__ */
+
index 25f855d06b2deee80923486cd013422ec4366a78..678fd62ae89e237782ff70a235e4394377810020 100644 (file)
@@ -50,6 +50,7 @@
 #include "BKE_global.h"
 
 #include "../generic/py_capi_utils.h"
+#include "../generic/python_utildefines.h"
 
 #ifdef BUILD_DATE
 extern char build_date[];
@@ -264,8 +265,7 @@ static PyObject *bpy_app_driver_dict_get(PyObject *UNUSED(self), void *UNUSED(cl
                }
        }
 
-       Py_INCREF(bpy_pydriver_Dict);
-       return bpy_pydriver_Dict;
+       return Py_INCREF_RET(bpy_pydriver_Dict);
 }
 
 static PyObject *bpy_app_autoexec_fail_message_get(PyObject *UNUSED(self), void *UNUSED(closure))
index cf20f6fa742a8498efb9556ee272a2f0c23d67f4..6a8b0b065c2fce0b763ecd17c0f840bbb26af980 100644 (file)
@@ -37,6 +37,8 @@
 #include "bpy_rna.h"
 #include "bpy_app_handlers.h"
 
+#include "../generic/python_utildefines.h"
+
 #include "BPY_extern.h"
 
 void bpy_app_generic_callback(struct Main *main, struct ID *id, void *arg);
@@ -306,8 +308,7 @@ void bpy_app_generic_callback(struct Main *UNUSED(main), struct ID *id, void *ar
                        PyTuple_SET_ITEM(args, 0, pyrna_struct_CreatePyObject(&id_ptr));
                }
                else {
-                       PyTuple_SET_ITEM(args, 0, Py_None);
-                       Py_INCREF(Py_None);
+                       PyTuple_SET_ITEM(args, 0, Py_INCREF_RET(Py_None));
                }
 
                /* Iterate the list and run the callbacks
index 2135abdd5893f84414fd46163dad5df6db857d45..1a56c0f7c5993264cfaae21459e5bb009e009712 100644 (file)
@@ -45,6 +45,7 @@
 
 #include "RNA_types.h"
 
+#include "../generic/python_utildefines.h"
 
 typedef struct
 {
@@ -406,7 +407,7 @@ static PyObject *app_translations_contexts_make(void)
        }
 
 #define SetObjString(item) PyStructSequence_SET_ITEM(translations_contexts, pos++, PyUnicode_FromString((item)))
-#define SetObjNone() Py_INCREF(Py_None); PyStructSequence_SET_ITEM(translations_contexts, pos++, Py_None)
+#define SetObjNone() PyStructSequence_SET_ITEM(translations_contexts, pos++, Py_INCREF_RET(Py_None))
 
        for (ctxt = _contexts; ctxt->c_id; ctxt++) {
                if (ctxt->value) {
@@ -516,9 +517,7 @@ static PyObject *_py_pgettext(PyObject *args, PyObject *kw, const char *(*_pgett
                return NULL;
        }
 
-       Py_INCREF(msgid);
-
-       return msgid;
+       return Py_INCREF_RET(msgid);
 #endif
 }
 
index 3d7d08024c7708f4b956c38e244b4d84861fdfd5..3b95b99a0a1fd2530d6f50ee6014f1fff6ad4af2 100644 (file)
@@ -53,6 +53,8 @@
 #include "bpy_util.h"
 #include "bpy_library.h"
 
+#include "../generic/python_utildefines.h"
+
 /* nifty feature. swap out strings for RNA data */
 #define USE_RNA_DATABLOCKS
 
@@ -274,10 +276,9 @@ static PyObject *bpy_lib_enter(BPy_Library *self, PyObject *UNUSED(args))
 
        /* return pair */
        ret = PyTuple_New(2);
-
-       PyTuple_SET_ITEM(ret, 0, (PyObject *)self_from);
-
-       PyTuple_SET_ITEM(ret, 1, (PyObject *)self);
+       PyTuple_SET_ITEMS(ret,
+               (PyObject *)self_from,
+               (PyObject *)self);
        Py_INCREF(self);
 
        BKE_reports_clear(&reports);
@@ -362,8 +363,7 @@ static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args))
                                                                /* just warn for now */
                                                                /* err = -1; */
 #ifdef USE_RNA_DATABLOCKS
-                                                               item = Py_None;
-                                                               Py_INCREF(item);
+                                                               item = Py_INCREF_RET(Py_None);
 #endif
                                                        }
 
@@ -375,8 +375,7 @@ static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args))
                                                        PyErr_Clear();
 
 #ifdef USE_RNA_DATABLOCKS
-                                                       item = Py_None;
-                                                       Py_INCREF(item);
+                                                       item = Py_INCREF_RET(Py_None);
 #endif
                                                }
 
index 86282f251c3f2ef597db5f3b7fa856e3c96aee2d..aad47d14b7cfc456dc7c20364840ffda872d917e 100644 (file)
@@ -44,6 +44,7 @@
 #include "bpy_rna.h" /* for setting arg props only - pyrna_py_to_prop() */
 #include "bpy_util.h"
 #include "../generic/bpy_internal_import.h"
+#include "../generic/python_utildefines.h"
 
 #include "RNA_access.h"
 #include "RNA_enum_types.h"
@@ -127,9 +128,8 @@ static PyObject *pyop_poll(PyObject *UNUSED(self), PyObject *args)
        /* restore with original context dict, probably NULL but need this for nested operator calls */
        Py_XDECREF(context_dict);
        CTX_py_dict_set(C, (void *)context_dict_back);
-       
-       Py_INCREF(ret);
-       return ret;
+
+       return Py_INCREF_RET(ret);
 }
 
 static PyObject *pyop_call(PyObject *UNUSED(self), PyObject *args)
index be316a8b7028c148c4a8ad95fae8fc2f05221c4c..74006c48dee086de5842bb171c3a9c19c206022b 100644 (file)
@@ -76,6 +76,7 @@
 
 #include "../generic/idprop_py_api.h" /* for IDprop lookups */
 #include "../generic/py_capi_utils.h"
+#include "../generic/python_utildefines.h"
 
 #define USE_PEDANTIC_WRITE
 #define USE_MATHUTILS
@@ -805,7 +806,7 @@ static PyObject *pyrna_struct_richcmp(PyObject *a, PyObject *b, int op)
                        return NULL;
        }
 
-       return Py_INCREF(res), res;
+       return Py_INCREF_RET(res);
 }
 
 static PyObject *pyrna_prop_richcmp(PyObject *a, PyObject *b, int op)
@@ -835,7 +836,7 @@ static PyObject *pyrna_prop_richcmp(PyObject *a, PyObject *b, int op)
                        return NULL;
        }
 
-       return Py_INCREF(res), res;
+       return Py_INCREF_RET(res);
 }
 
 /*----------------------repr--------------------------------------------*/
@@ -4223,7 +4224,7 @@ static PyObject *pyrna_struct_get(BPy_StructRNA *self, PyObject *args)
                }
        }
 
-       return Py_INCREF(def), def;
+       return Py_INCREF_RET(def);
 }
 
 PyDoc_STRVAR(pyrna_struct_as_pointer_doc,
@@ -4285,7 +4286,7 @@ static PyObject *pyrna_prop_collection_get(BPy_PropertyRNA *self, PyObject *args
                             Py_TYPE(key_ob)->tp_name);
        }
 
-       return Py_INCREF(def), def;
+       return Py_INCREF_RET(def);
 }
 
 PyDoc_STRVAR(pyrna_prop_collection_find_doc,
@@ -4798,8 +4799,7 @@ static PyObject *pyrna_prop_new(PyTypeObject *type, PyObject *args, PyObject *UN
                return NULL;
 
        if (type == Py_TYPE(base)) {
-               Py_INCREF(base);
-               return (PyObject *)base;
+               return Py_INCREF_RET((PyObject *)base);
        }
        else if (PyType_IsSubtype(type, &pyrna_prop_Type)) {
                BPy_PropertyRNA *ret = (BPy_PropertyRNA *) type->tp_alloc(type, 0);
@@ -6283,7 +6283,7 @@ static PyObject *pyrna_srna_Subtype(StructRNA *srna)
 
                        /* arg[1] (bases=...) */
                        PyTuple_SET_ITEM(args, 1, item = PyTuple_New(1));
-                       PyTuple_SET_ITEM(item, 0, py_base); Py_INCREF(py_base);
+                       PyTuple_SET_ITEM(item, 0, Py_INCREF_RET(py_base));
 
 
                        /* arg[2] (dict=...) */
index 0a5501051245ccc0b9c27b5789fdbc5a4138ab9d..6fe8dc3866b9fb61b75b8e784aadcf187b95d4fb 100644 (file)
@@ -31,6 +31,8 @@
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
 
+#include "../generic/python_utildefines.h"
+
 #ifndef MATH_STANDALONE
 #  include "BLI_dynstr.h"
 #endif
@@ -444,8 +446,7 @@ char BaseMathObject_owner_doc[] = "The item this is wrapping or None  (read-only
 PyObject *BaseMathObject_owner_get(BaseMathObject *self, void *UNUSED(closure))
 {
        PyObject *ret = self->cb_user ? self->cb_user : Py_None;
-       Py_INCREF(ret);
-       return ret;
+       return Py_INCREF_RET(ret);
 }
 
 char BaseMathObject_is_wrapped_doc[] = "True when this object wraps external data (read-only).\n\n:type: boolean";
index 5bc6f6177dec24c024df2e33770a2ec75c8f1829..8426038f2d41929ce762e145afeeeed694089b92 100644 (file)
@@ -32,6 +32,8 @@
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
 
+#include "../generic/python_utildefines.h"
+
 #ifndef MATH_STANDALONE
 #  include "BLI_dynstr.h"
 #endif
@@ -187,7 +189,7 @@ static PyObject *Color_richcmpr(PyObject *a, PyObject *b, int op)
                        return NULL;
        }
 
-       return Py_INCREF(res), res;
+       return Py_INCREF_RET(res);
 }
 
 /* ---------------------SEQUENCE PROTOCOLS------------------------ */
@@ -753,9 +755,10 @@ static PyObject *Color_hsv_get(ColorObject *self, void *UNUSED(closure))
        rgb_to_hsv(self->col[0], self->col[1], self->col[2], &(hsv[0]), &(hsv[1]), &(hsv[2]));
 
        ret = PyTuple_New(3);
-       PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(hsv[0]));
-       PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(hsv[1]));
-       PyTuple_SET_ITEM(ret, 2, PyFloat_FromDouble(hsv[2]));
+       PyTuple_SET_ITEMS(ret,
+               PyFloat_FromDouble(hsv[0]),
+               PyFloat_FromDouble(hsv[1]),
+               PyFloat_FromDouble(hsv[2]));
        return ret;
 }
 
index 1c45d5e88ac7464181c292a93a0d117f0bec8bce..9c0ced39403927ca68d13e68b3137d3e1e4dc00a 100644 (file)
@@ -31,6 +31,7 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "../generic/python_utildefines.h"
 
 #ifndef MATH_STANDALONE
 #  include "BLI_dynstr.h"
@@ -382,7 +383,7 @@ static PyObject *Euler_richcmpr(PyObject *a, PyObject *b, int op)
                        return NULL;
        }
 
-       return Py_INCREF(res), res;
+       return Py_INCREF_RET(res);
 }
 
 /* ---------------------SEQUENCE PROTOCOLS------------------------ */
index 5be4fdedff1f12d920073b8bea688334c1542740..4706c09176ecbbb4971db781194d745d195b2732 100644 (file)
@@ -32,6 +32,8 @@
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
 
+#include "../generic/python_utildefines.h"
+
 #ifndef MATH_STANDALONE
 #  include "BLI_string.h"
 #  include "BLI_dynstr.h"
@@ -1653,10 +1655,10 @@ static PyObject *Matrix_decompose(MatrixObject *self)
        mat3_to_quat(quat, rot);
 
        ret = PyTuple_New(3);
-       PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(loc, 3, NULL));
-       PyTuple_SET_ITEM(ret, 1, Quaternion_CreatePyObject(quat, NULL));
-       PyTuple_SET_ITEM(ret, 2, Vector_CreatePyObject(size, 3, NULL));
-
+       PyTuple_SET_ITEMS(ret,
+               Vector_CreatePyObject(loc, 3, NULL),
+               Quaternion_CreatePyObject(quat, NULL),
+               Vector_CreatePyObject(size, 3, NULL));
        return ret;
 }
 
@@ -2032,7 +2034,7 @@ static PyObject *Matrix_richcmpr(PyObject *a, PyObject *b, int op)
                        return NULL;
        }
 
-       return Py_INCREF(res), res;
+       return Py_INCREF_RET(res);
 }
 
 /*---------------------SEQUENCE PROTOCOLS------------------------
index 7e15a3fc6048c56710e87c197fd5734ff26e7d50..d422634a4966b6263938b8aae2b51f763babf1ec 100644 (file)
@@ -32,6 +32,8 @@
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
 
+#include "../generic/python_utildefines.h"
+
 #ifndef MATH_STANDALONE
 #  include "BLI_dynstr.h"
 #endif
@@ -169,8 +171,9 @@ static PyObject *Quaternion_to_axis_angle(QuaternionObject *self)
        quat__axis_angle_sanitize(axis, &angle);
 
        ret = PyTuple_New(2);
-       PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(axis, 3, NULL));
-       PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(angle));
+       PyTuple_SET_ITEMS(ret,
+               Vector_CreatePyObject(axis, 3, NULL),
+               PyFloat_FromDouble(angle));
        return ret;
 }
 
@@ -544,7 +547,7 @@ static PyObject *Quaternion_richcmpr(PyObject *a, PyObject *b, int op)
                        return NULL;
        }
 
-       return Py_INCREF(res), res;
+       return Py_INCREF_RET(res);
 }
 
 /* ---------------------SEQUENCE PROTOCOLS------------------------ */
index c5368381e2f6eea6c1394f0f31d9f5251537facd..261234e6aae207a0bc575a2792bfe6b26851da96 100644 (file)
@@ -42,6 +42,7 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "../generic/python_utildefines.h"
 
 /*-------------------------DOC STRINGS ---------------------------*/
 PyDoc_STRVAR(M_Geometry_doc,
@@ -207,8 +208,9 @@ static PyObject *M_Geometry_intersect_line_line(PyObject *UNUSED(self), PyObject
        }
        else {
                tuple = PyTuple_New(2);
-               PyTuple_SET_ITEM(tuple, 0, Vector_CreatePyObject(i1, len, NULL));
-               PyTuple_SET_ITEM(tuple, 1, Vector_CreatePyObject(i2, len, NULL));
+               PyTuple_SET_ITEMS(tuple,
+                       Vector_CreatePyObject(i1, len, NULL),
+                       Vector_CreatePyObject(i2, len, NULL));
                return tuple;
        }
 }
@@ -267,8 +269,9 @@ static PyObject *M_Geometry_intersect_sphere_sphere_2d(PyObject *UNUSED(self), P
            (dist < FLT_EPSILON))
        {
                /* out of range */
-               PyTuple_SET_ITEM(ret, 0,  Py_None); Py_INCREF(Py_None);
-               PyTuple_SET_ITEM(ret, 1,  Py_None); Py_INCREF(Py_None);
+               PyTuple_SET_ITEMS(ret,
+                       Py_INCREF_RET(Py_None),
+                       Py_INCREF_RET(Py_None));
        }
        else {
                const float dist_delta = ((rad_a * rad_a) - (rad_b * rad_b) + (dist * dist)) / (2.0f * dist);
@@ -285,8 +288,9 @@ static PyObject *M_Geometry_intersect_sphere_sphere_2d(PyObject *UNUSED(self), P
                i2[0] = i_cent[0] - h * v_ab[1] / dist;
                i2[1] = i_cent[1] + h * v_ab[0] / dist;
 
-               PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(i1, 2, NULL));
-               PyTuple_SET_ITEM(ret, 1, Vector_CreatePyObject(i2, 2, NULL));
+               PyTuple_SET_ITEMS(ret,
+                       Vector_CreatePyObject(i1, 2, NULL),
+                       Vector_CreatePyObject(i2, 2, NULL));
        }
 
        return ret;
@@ -555,16 +559,14 @@ static PyObject *M_Geometry_intersect_plane_plane(PyObject *UNUSED(self), PyObje
                ret_no = Vector_CreatePyObject(isect_no, 3, NULL);
        }
        else {
-               ret_co = Py_None;
-               ret_no = Py_None;
-
-               Py_INCREF(ret_co);
-               Py_INCREF(ret_no);
+               ret_co = Py_INCREF_RET(Py_None);
+               ret_no = Py_INCREF_RET(Py_None);
        }
 
        ret = PyTuple_New(2);
-       PyTuple_SET_ITEM(ret, 0, ret_co);
-       PyTuple_SET_ITEM(ret, 1, ret_no);
+       PyTuple_SET_ITEMS(ret,
+               ret_co,
+               ret_no);
        return ret;
 }
 
@@ -631,11 +633,9 @@ static PyObject *M_Geometry_intersect_line_sphere(PyObject *UNUSED(self), PyObje
                                break;
                }
 
-               if (use_a) { PyTuple_SET_ITEM(ret, 0,  Vector_CreatePyObject(isect_a, 3, NULL)); }
-               else       { PyTuple_SET_ITEM(ret, 0,  Py_None); Py_INCREF(Py_None); }
-
-               if (use_b) { PyTuple_SET_ITEM(ret, 1,  Vector_CreatePyObject(isect_b, 3, NULL)); }
-               else       { PyTuple_SET_ITEM(ret, 1,  Py_None); Py_INCREF(Py_None); }
+               PyTuple_SET_ITEMS(ret,
+                       use_a ? Vector_CreatePyObject(isect_a, 3, NULL) : Py_INCREF_RET(Py_None),
+                       use_b ? Vector_CreatePyObject(isect_b, 3, NULL) : Py_INCREF_RET(Py_None));
 
                return ret;
        }
@@ -705,11 +705,9 @@ static PyObject *M_Geometry_intersect_line_sphere_2d(PyObject *UNUSED(self), PyO
                                break;
                }
 
-               if (use_a) { PyTuple_SET_ITEM(ret, 0,  Vector_CreatePyObject(isect_a, 2, NULL)); }
-               else       { PyTuple_SET_ITEM(ret, 0,  Py_None); Py_INCREF(Py_None); }
-
-               if (use_b) { PyTuple_SET_ITEM(ret, 1,  Vector_CreatePyObject(isect_b, 2, NULL)); }
-               else       { PyTuple_SET_ITEM(ret, 1,  Py_None); Py_INCREF(Py_None); }
+               PyTuple_SET_ITEMS(ret,
+                       use_a ? Vector_CreatePyObject(isect_a, 2, NULL) : Py_INCREF_RET(Py_None),
+                       use_b ? Vector_CreatePyObject(isect_b, 2, NULL) : Py_INCREF_RET(Py_None));
 
                return ret;
        }
@@ -756,8 +754,9 @@ static PyObject *M_Geometry_intersect_point_line(PyObject *UNUSED(self), PyObjec
        lambda = closest_to_line_v3(pt_out, pt, line_a, line_b);
        
        ret = PyTuple_New(2);
-       PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(pt_out, size, NULL));
-       PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(lambda));
+       PyTuple_SET_ITEMS(ret,
+               Vector_CreatePyObject(pt_out, size, NULL),
+               PyFloat_FromDouble(lambda));
        return ret;
 }
 
@@ -1090,8 +1089,9 @@ static PyObject *M_Geometry_points_in_planes(PyObject *UNUSED(self), PyObject *a
 
                {
                        PyObject *ret = PyTuple_New(2);
-                       PyTuple_SET_ITEM(ret, 0, py_verts);
-                       PyTuple_SET_ITEM(ret, 1, py_plane_index);
+                       PyTuple_SET_ITEMS(ret,
+                               py_verts,
+                               py_plane_index);
                        return ret;
                }
        }
@@ -1397,8 +1397,9 @@ static PyObject *M_Geometry_box_pack_2d(PyObject *UNUSED(self), PyObject *boxlis
        }
 
        ret = PyTuple_New(2);
-       PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(tot_width));
-       PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(tot_height));
+       PyTuple_SET_ITEMS(ret,
+               PyFloat_FromDouble(tot_width),
+               PyFloat_FromDouble(tot_height));
        return ret;
 }
 
index 677fa9830f110e171dc9b48be3e554f5830fb2fe..199c2e02da4820712d220f267fcd449c45b4e7a9 100644 (file)
@@ -35,6 +35,7 @@
 #include "BLI_kdtree.h"
 
 #include "../generic/py_capi_utils.h"
+#include "../generic/python_utildefines.h"
 
 #include "mathutils.h"
 #include "mathutils_kdtree.h"  /* own include */
@@ -58,9 +59,10 @@ static void kdtree_nearest_to_py_tuple(const KDTreeNearest *nearest, PyObject *p
        BLI_assert(nearest->index >= 0);
        BLI_assert(PyTuple_GET_SIZE(py_retval) == 3);
 
-       PyTuple_SET_ITEM(py_retval, 0, Vector_CreatePyObject((float *)nearest->co, 3, NULL));
-       PyTuple_SET_ITEM(py_retval, 1, PyLong_FromLong(nearest->index));
-       PyTuple_SET_ITEM(py_retval, 2, PyFloat_FromDouble(nearest->dist));
+       PyTuple_SET_ITEMS(py_retval,
+               Vector_CreatePyObject((float *)nearest->co, 3, NULL),
+               PyLong_FromLong(nearest->index),
+               PyFloat_FromDouble(nearest->dist));
 }
 
 static PyObject *kdtree_nearest_to_py(const KDTreeNearest *nearest)