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.
This commit is contained in:
Campbell Barton 2015-01-06 16:42:22 +11:00
parent ee58d44945
commit 9fd569a654
27 changed files with 253 additions and 118 deletions

@ -55,7 +55,8 @@ extern "C" {
_49_, _50_, _51_, _52_, _53_, _54_, _55_, _56_, _57_, _58_, _59_, _60_, _61_, _62_, _63_, _64_, \ _49_, _50_, _51_, _52_, _53_, _54_, _55_, _56_, _57_, _58_, _59_, _60_, _61_, _62_, _63_, _64_, \
count, ...) count count, ...) count
#define _VA_NARGS_EXPAND(args) _VA_NARGS_RETURN_COUNT args #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, \ 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, \ 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, \ 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) #define _VA_NARGS_OVERLOAD_MACRO(name, count) _VA_NARGS_OVERLOAD_MACRO1(name, count)
/* --- expose for re-use --- */ /* --- expose for re-use --- */
#define VA_NARGS_CALL_OVERLOAD(name, ...) \ #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 */ /* useful for finding bad use of min/max */
#if 0 #if 0
@ -442,6 +443,44 @@ extern "C" {
# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr))) # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr)))
#endif #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 */ /* Like offsetof(typeof(), member), for non-gcc compilers */
#define OFFSETOF_STRUCT(_struct, _member) \ #define OFFSETOF_STRUCT(_struct, _member) \
((((char *)&((_struct)->_member)) - ((char *)(_struct))) + sizeof((_struct)->_member)) ((((char *)&((_struct)->_member)) - ((char *)(_struct))) + sizeof((_struct)->_member))

@ -92,10 +92,11 @@ ContextFunctions_get_border(PyObject *self)
{ {
BBox<Vec2i> border(ContextFunctions::GetBorderCF()); BBox<Vec2i> border(ContextFunctions::GetBorderCF());
PyObject *v = PyTuple_New(4); PyObject *v = PyTuple_New(4);
PyTuple_SET_ITEM(v, 0, PyLong_FromLong(border.getMin().x())); PyTuple_SET_ITEMS(v,
PyTuple_SET_ITEM(v, 1, PyLong_FromLong(border.getMin().y())); PyLong_FromLong(border.getMin().x()),
PyTuple_SET_ITEM(v, 2, PyLong_FromLong(border.getMax().x())); PyLong_FromLong(border.getMin().y()),
PyTuple_SET_ITEM(v, 3, PyLong_FromLong(border.getMax().y())); PyLong_FromLong(border.getMax().x()),
PyLong_FromLong(border.getMax().y()));
return v; return v;
} }

@ -401,8 +401,9 @@ PyObject *BPy_CurvePoint_from_CurvePoint(CurvePoint& cp)
PyObject *BPy_directedViewEdge_from_directedViewEdge(ViewVertex::directedViewEdge& dve) PyObject *BPy_directedViewEdge_from_directedViewEdge(ViewVertex::directedViewEdge& dve)
{ {
PyObject *py_dve = PyTuple_New(2); PyObject *py_dve = PyTuple_New(2);
PyTuple_SET_ITEM(py_dve, 0, BPy_ViewEdge_from_ViewEdge(*(dve.first))); PyTuple_SET_ITEMS(py_dve,
PyTuple_SET_ITEM(py_dve, 1, PyBool_from_bool(dve.second)); BPy_ViewEdge_from_ViewEdge(*(dve.first)),
PyBool_from_bool(dve.second));
return py_dve; return py_dve;
} }

@ -92,6 +92,7 @@ extern "C" {
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
#include "mathutils/mathutils.h" #include "mathutils/mathutils.h"
#include "generic/python_utildefines.h"
//============================== //==============================
// C++ => Python // C++ => Python

@ -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 e2(info->e2.x(), info->e2.y(), info->e2.z());
Vec3r er(info->er.x(), info->er.y(), info->er.z()); Vec3r er(info->er.x(), info->er.y(), info->er.z());
PyObject *retval = PyTuple_New(7); PyObject *retval = PyTuple_New(7);
PyTuple_SET_ITEM(retval, 0, PyFloat_FromDouble(info->K1)); PyTuple_SET_ITEMS(retval,
PyTuple_SET_ITEM(retval, 2, Vector_from_Vec3r(e1)); PyFloat_FromDouble(info->K1),
PyTuple_SET_ITEM(retval, 1, PyFloat_FromDouble(info->K2)); PyFloat_FromDouble(info->K2),
PyTuple_SET_ITEM(retval, 3, Vector_from_Vec3r(e2)); Vector_from_Vec3r(e1),
PyTuple_SET_ITEM(retval, 4, PyFloat_FromDouble(info->Kr)); Vector_from_Vec3r(e2),
PyTuple_SET_ITEM(retval, 5, Vector_from_Vec3r(er)); PyFloat_FromDouble(info->Kr),
PyTuple_SET_ITEM(retval, 6, PyFloat_FromDouble(info->dKr)); Vector_from_Vec3r(er),
PyFloat_FromDouble(info->dKr));
return retval; return retval;
} }

@ -43,6 +43,8 @@
#include "bmesh_py_types.h" #include "bmesh_py_types.h"
#include "../generic/python_utildefines.h"
static int bpy_bm_op_as_py_error(BMesh *bm) static int bpy_bm_op_as_py_error(BMesh *bm)
{ {
if (BMO_error_occurred(bm)) { if (BMO_error_occurred(bm)) {
@ -547,13 +549,13 @@ static PyObject *bpy_slot_to_py(BMesh *bm, BMOpSlot *slot)
break; break;
case BMO_OP_SLOT_PTR: case BMO_OP_SLOT_PTR:
BLI_assert(0); /* currently we don't have any pointer return values in use */ 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; break;
case BMO_OP_SLOT_ELEMENT_BUF: case BMO_OP_SLOT_ELEMENT_BUF:
{ {
if (slot->slot_subtype.elem & BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE) { if (slot->slot_subtype.elem & BMO_OP_SLOT_SUBTYPE_ELEM_IS_SINGLE) {
BMHeader *ele = BMO_slot_buffer_get_single(slot); 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 { else {
const int size = slot->len; 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: case BMO_OP_SLOT_SUBTYPE_MAP_INTERNAL:
/* can't convert from these */ /* can't convert from these */
item = (Py_INCREF(Py_None), Py_None); item = Py_INCREF_RET(Py_None);
break; break;
} }
break; break;
@ -743,7 +745,7 @@ PyObject *BPy_BMO_call(BPy_BMeshOpFunc *self, PyObject *args, PyObject *kw)
ret = NULL; /* exception raised above */ ret = NULL; /* exception raised above */
} }
else if (bmop.slots_out[0].slot_name == NULL) { else if (bmop.slots_out[0].slot_name == NULL) {
ret = (Py_INCREF(Py_None), Py_None); ret = Py_INCREF_RET(Py_None);
} }
else { else {
/* build return value */ /* build return value */
@ -759,7 +761,7 @@ PyObject *BPy_BMO_call(BPy_BMeshOpFunc *self, PyObject *args, PyObject *kw)
/* this function doesn't throw exceptions */ /* this function doesn't throw exceptions */
item = bpy_slot_to_py(bm, slot); item = bpy_slot_to_py(bm, slot);
if (item == NULL) { if (item == NULL) {
item = (Py_INCREF(Py_None), Py_None); item = Py_INCREF_RET(Py_None);
} }
#if 1 #if 1

@ -42,6 +42,7 @@
#include "bmesh_py_types_meshdata.h" #include "bmesh_py_types_meshdata.h"
#include "../mathutils/mathutils.h" #include "../mathutils/mathutils.h"
#include "../generic/python_utildefines.h"
#include "BKE_customdata.h" #include "BKE_customdata.h"
@ -483,8 +484,9 @@ static PyObject *bpy_bmlayercollection_items(BPy_BMLayerCollection *self)
for (i = 0; tot-- > 0; index++) { for (i = 0; tot-- > 0; index++) {
item = PyTuple_New(2); item = PyTuple_New(2);
PyTuple_SET_ITEM(item, 0, PyUnicode_FromString(data->layers[index].name)); PyTuple_SET_ITEMS(item,
PyTuple_SET_ITEM(item, 1, BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index)); PyUnicode_FromString(data->layers[index].name),
BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index));
PyList_SET_ITEM(ret, i++, item); 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[] = { static struct PyMethodDef bpy_bmlayeritem_methods[] = {

@ -45,6 +45,8 @@
#include "bmesh_py_types_meshdata.h" #include "bmesh_py_types_meshdata.h"
#include "../generic/python_utildefines.h"
/* Mesh BMTexPoly /* Mesh BMTexPoly
* ************** */ * ************** */
@ -684,10 +686,9 @@ static PyObject *bpy_bmdeformvert_items(BPy_BMDeformVert *self)
ret = PyList_New(self->data->totweight); ret = PyList_New(self->data->totweight);
for (i = 0; i < self->data->totweight; i++, dw++) { for (i = 0; i < self->data->totweight; i++, dw++) {
item = PyTuple_New(2); item = PyTuple_New(2);
PyTuple_SET_ITEMS(item,
PyTuple_SET_ITEM(item, 0, PyLong_FromLong(dw->def_nr)); PyLong_FromLong(dw->def_nr),
PyTuple_SET_ITEM(item, 1, PyFloat_FromDouble(dw->weight)); PyFloat_FromDouble(dw->weight));
PyList_SET_ITEM(ret, i, item); 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); return PyFloat_FromDouble(dw->weight);
} }
else { else {
return Py_INCREF(def), def; return Py_INCREF_RET(def);
} }
} }
} }

@ -42,6 +42,8 @@
#include "bmesh_py_types.h" #include "bmesh_py_types.h"
#include "bmesh_py_utils.h" /* own include */ #include "bmesh_py_utils.h" /* own include */
#include "../generic/python_utildefines.h"
PyDoc_STRVAR(bpy_bm_utils_vert_collapse_edge_doc, PyDoc_STRVAR(bpy_bm_utils_vert_collapse_edge_doc,
".. method:: vert_collapse_edge(vert, edge)\n" ".. 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) { if (v_new && e_new) {
PyObject *ret = PyTuple_New(2); PyObject *ret = PyTuple_New(2);
PyTuple_SET_ITEM(ret, 0, BPy_BMEdge_CreatePyObject(bm, e_new)); PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 1, BPy_BMVert_CreatePyObject(bm, v_new)); BPy_BMEdge_CreatePyObject(bm, e_new),
BPy_BMVert_CreatePyObject(bm, v_new));
return ret; return ret;
} }
else { else {
@ -524,8 +527,9 @@ static PyObject *bpy_bm_utils_face_split(PyObject *UNUSED(self), PyObject *args,
if (f_new && l_new) { if (f_new && l_new) {
PyObject *ret = PyTuple_New(2); PyObject *ret = PyTuple_New(2);
PyTuple_SET_ITEM(ret, 0, BPy_BMFace_CreatePyObject(bm, f_new)); PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 1, BPy_BMLoop_CreatePyObject(bm, l_new)); BPy_BMFace_CreatePyObject(bm, f_new),
BPy_BMLoop_CreatePyObject(bm, l_new));
return ret; return ret;
} }
else { else {

@ -33,6 +33,9 @@
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
#include "../generic/python_utildefines.h"
PyDoc_STRVAR(py_blf_position_doc, PyDoc_STRVAR(py_blf_position_doc,
".. function:: position(fontid, x, y, z)\n" ".. function:: position(fontid, x, y, z)\n"
"\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); BLF_width_and_height(fontid, text, INT_MAX, &r_width, &r_height);
ret = PyTuple_New(2); ret = PyTuple_New(2);
PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(r_width)); PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(r_height)); PyFloat_FromDouble(r_width),
PyFloat_FromDouble(r_height));
return ret; return ret;
} }

@ -33,16 +33,17 @@
#include "idprop_py_api.h" #include "idprop_py_api.h"
#include "BKE_idprop.h" #include "BKE_idprop.h"
#define USE_STRING_COERCE #define USE_STRING_COERCE
#ifdef USE_STRING_COERCE #ifdef USE_STRING_COERCE
#include "py_capi_utils.h" #include "py_capi_utils.h"
#endif #endif
#include "../generic/python_utildefines.h"
/*********************** ID Property Main Wrapper Stuff ***************/ /*********************** 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); 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++) { for (j = len; j < prop->len; j++) {
Py_INCREF(Py_None); PyList_SET_ITEM(seq, j, Py_INCREF_RET(Py_None));
PyList_SET_ITEM(seq, j, Py_None);
} }
/*set correct group length*/ /*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++) { for (i = 0, loop = prop->data.group.first; loop; loop = loop->next, i++) {
PyObject *item = PyTuple_New(2); PyObject *item = PyTuple_New(2);
PyTuple_SET_ITEM(item, 0, PyUnicode_FromString(loop->name)); PyTuple_SET_ITEMS(item,
PyTuple_SET_ITEM(item, 1, BPy_IDGroup_WrapData(id, loop, prop)); PyUnicode_FromString(loop->name),
BPy_IDGroup_WrapData(id, loop, prop));
PyList_SET_ITEM(seq, i, item); 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) { if (self->mode == IDPROP_ITER_ITEMS) {
ret = PyTuple_New(2); ret = PyTuple_New(2);
PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(cur->name)); PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 1, BPy_IDGroup_WrapData(self->group->id, cur, self->group->prop)); PyUnicode_FromString(cur->name),
BPy_IDGroup_WrapData(self->group->id, cur, self->group->prop));
return ret; return ret;
} }
else { else {

@ -37,6 +37,8 @@
#include "py_capi_utils.h" #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 */ /* only for BLI_strncpy_wchar_from_utf8, should replace with py funcs but too late in release now */
#include "BLI_string_utf8.h" #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 */ /* for debugging */
void PyC_ObSpit(const char *name, PyObject *var) void PyC_ObSpit(const char *name, PyObject *var)
{ {
@ -664,8 +677,7 @@ void PyC_RunQuicky(const char *filepath, int n, ...)
PyErr_Print(); PyErr_Print();
PyErr_Clear(); PyErr_Clear();
PyList_SET_ITEM(values, i, Py_None); /* hold user */ PyList_SET_ITEM(values, i, Py_INCREF_RET(Py_None)); /* hold user */
Py_INCREF(Py_None);
sizes[i] = 0; sizes[i] = 0;
} }

@ -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, PyObject * PyC_FromArray(const void *array, int length, const PyTypeObject *type,
const bool is_double, const char *error_prefix); const bool is_double, const char *error_prefix);
void PyC_Tuple_Fill(PyObject *tuple, PyObject *value); 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/ */ /* follow http://www.python.org/dev/peps/pep-0383/ */
PyObject * PyC_UnicodeFromByte(const char *str); PyObject * PyC_UnicodeFromByte(const char *str);

@ -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__ */

@ -50,6 +50,7 @@
#include "BKE_global.h" #include "BKE_global.h"
#include "../generic/py_capi_utils.h" #include "../generic/py_capi_utils.h"
#include "../generic/python_utildefines.h"
#ifdef BUILD_DATE #ifdef BUILD_DATE
extern char 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 Py_INCREF_RET(bpy_pydriver_Dict);
return bpy_pydriver_Dict;
} }
static PyObject *bpy_app_autoexec_fail_message_get(PyObject *UNUSED(self), void *UNUSED(closure)) static PyObject *bpy_app_autoexec_fail_message_get(PyObject *UNUSED(self), void *UNUSED(closure))

@ -37,6 +37,8 @@
#include "bpy_rna.h" #include "bpy_rna.h"
#include "bpy_app_handlers.h" #include "bpy_app_handlers.h"
#include "../generic/python_utildefines.h"
#include "BPY_extern.h" #include "BPY_extern.h"
void bpy_app_generic_callback(struct Main *main, struct ID *id, void *arg); 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)); PyTuple_SET_ITEM(args, 0, pyrna_struct_CreatePyObject(&id_ptr));
} }
else { else {
PyTuple_SET_ITEM(args, 0, Py_None); PyTuple_SET_ITEM(args, 0, Py_INCREF_RET(Py_None));
Py_INCREF(Py_None);
} }
/* Iterate the list and run the callbacks /* Iterate the list and run the callbacks

@ -45,6 +45,7 @@
#include "RNA_types.h" #include "RNA_types.h"
#include "../generic/python_utildefines.h"
typedef struct 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 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++) { for (ctxt = _contexts; ctxt->c_id; ctxt++) {
if (ctxt->value) { if (ctxt->value) {
@ -516,9 +517,7 @@ static PyObject *_py_pgettext(PyObject *args, PyObject *kw, const char *(*_pgett
return NULL; return NULL;
} }
Py_INCREF(msgid); return Py_INCREF_RET(msgid);
return msgid;
#endif #endif
} }

@ -53,6 +53,8 @@
#include "bpy_util.h" #include "bpy_util.h"
#include "bpy_library.h" #include "bpy_library.h"
#include "../generic/python_utildefines.h"
/* nifty feature. swap out strings for RNA data */ /* nifty feature. swap out strings for RNA data */
#define USE_RNA_DATABLOCKS #define USE_RNA_DATABLOCKS
@ -274,10 +276,9 @@ static PyObject *bpy_lib_enter(BPy_Library *self, PyObject *UNUSED(args))
/* return pair */ /* return pair */
ret = PyTuple_New(2); ret = PyTuple_New(2);
PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 0, (PyObject *)self_from); (PyObject *)self_from,
(PyObject *)self);
PyTuple_SET_ITEM(ret, 1, (PyObject *)self);
Py_INCREF(self); Py_INCREF(self);
BKE_reports_clear(&reports); BKE_reports_clear(&reports);
@ -362,8 +363,7 @@ static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args))
/* just warn for now */ /* just warn for now */
/* err = -1; */ /* err = -1; */
#ifdef USE_RNA_DATABLOCKS #ifdef USE_RNA_DATABLOCKS
item = Py_None; item = Py_INCREF_RET(Py_None);
Py_INCREF(item);
#endif #endif
} }
@ -375,8 +375,7 @@ static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args))
PyErr_Clear(); PyErr_Clear();
#ifdef USE_RNA_DATABLOCKS #ifdef USE_RNA_DATABLOCKS
item = Py_None; item = Py_INCREF_RET(Py_None);
Py_INCREF(item);
#endif #endif
} }

@ -44,6 +44,7 @@
#include "bpy_rna.h" /* for setting arg props only - pyrna_py_to_prop() */ #include "bpy_rna.h" /* for setting arg props only - pyrna_py_to_prop() */
#include "bpy_util.h" #include "bpy_util.h"
#include "../generic/bpy_internal_import.h" #include "../generic/bpy_internal_import.h"
#include "../generic/python_utildefines.h"
#include "RNA_access.h" #include "RNA_access.h"
#include "RNA_enum_types.h" #include "RNA_enum_types.h"
@ -128,8 +129,7 @@ static PyObject *pyop_poll(PyObject *UNUSED(self), PyObject *args)
Py_XDECREF(context_dict); Py_XDECREF(context_dict);
CTX_py_dict_set(C, (void *)context_dict_back); CTX_py_dict_set(C, (void *)context_dict_back);
Py_INCREF(ret); return Py_INCREF_RET(ret);
return ret;
} }
static PyObject *pyop_call(PyObject *UNUSED(self), PyObject *args) static PyObject *pyop_call(PyObject *UNUSED(self), PyObject *args)

@ -76,6 +76,7 @@
#include "../generic/idprop_py_api.h" /* for IDprop lookups */ #include "../generic/idprop_py_api.h" /* for IDprop lookups */
#include "../generic/py_capi_utils.h" #include "../generic/py_capi_utils.h"
#include "../generic/python_utildefines.h"
#define USE_PEDANTIC_WRITE #define USE_PEDANTIC_WRITE
#define USE_MATHUTILS #define USE_MATHUTILS
@ -805,7 +806,7 @@ static PyObject *pyrna_struct_richcmp(PyObject *a, PyObject *b, int op)
return NULL; return NULL;
} }
return Py_INCREF(res), res; return Py_INCREF_RET(res);
} }
static PyObject *pyrna_prop_richcmp(PyObject *a, PyObject *b, int op) 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 NULL;
} }
return Py_INCREF(res), res; return Py_INCREF_RET(res);
} }
/*----------------------repr--------------------------------------------*/ /*----------------------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, 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); Py_TYPE(key_ob)->tp_name);
} }
return Py_INCREF(def), def; return Py_INCREF_RET(def);
} }
PyDoc_STRVAR(pyrna_prop_collection_find_doc, PyDoc_STRVAR(pyrna_prop_collection_find_doc,
@ -4798,8 +4799,7 @@ static PyObject *pyrna_prop_new(PyTypeObject *type, PyObject *args, PyObject *UN
return NULL; return NULL;
if (type == Py_TYPE(base)) { if (type == Py_TYPE(base)) {
Py_INCREF(base); return Py_INCREF_RET((PyObject *)base);
return (PyObject *)base;
} }
else if (PyType_IsSubtype(type, &pyrna_prop_Type)) { else if (PyType_IsSubtype(type, &pyrna_prop_Type)) {
BPy_PropertyRNA *ret = (BPy_PropertyRNA *) type->tp_alloc(type, 0); BPy_PropertyRNA *ret = (BPy_PropertyRNA *) type->tp_alloc(type, 0);
@ -6283,7 +6283,7 @@ static PyObject *pyrna_srna_Subtype(StructRNA *srna)
/* arg[1] (bases=...) */ /* arg[1] (bases=...) */
PyTuple_SET_ITEM(args, 1, item = PyTuple_New(1)); 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=...) */ /* arg[2] (dict=...) */

@ -31,6 +31,8 @@
#include "BLI_math.h" #include "BLI_math.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
#include "../generic/python_utildefines.h"
#ifndef MATH_STANDALONE #ifndef MATH_STANDALONE
# include "BLI_dynstr.h" # include "BLI_dynstr.h"
#endif #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 *BaseMathObject_owner_get(BaseMathObject *self, void *UNUSED(closure))
{ {
PyObject *ret = self->cb_user ? self->cb_user : Py_None; PyObject *ret = self->cb_user ? self->cb_user : Py_None;
Py_INCREF(ret); return Py_INCREF_RET(ret);
return ret;
} }
char BaseMathObject_is_wrapped_doc[] = "True when this object wraps external data (read-only).\n\n:type: boolean"; char BaseMathObject_is_wrapped_doc[] = "True when this object wraps external data (read-only).\n\n:type: boolean";

@ -32,6 +32,8 @@
#include "BLI_math.h" #include "BLI_math.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
#include "../generic/python_utildefines.h"
#ifndef MATH_STANDALONE #ifndef MATH_STANDALONE
# include "BLI_dynstr.h" # include "BLI_dynstr.h"
#endif #endif
@ -187,7 +189,7 @@ static PyObject *Color_richcmpr(PyObject *a, PyObject *b, int op)
return NULL; return NULL;
} }
return Py_INCREF(res), res; return Py_INCREF_RET(res);
} }
/* ---------------------SEQUENCE PROTOCOLS------------------------ */ /* ---------------------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])); rgb_to_hsv(self->col[0], self->col[1], self->col[2], &(hsv[0]), &(hsv[1]), &(hsv[2]));
ret = PyTuple_New(3); ret = PyTuple_New(3);
PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(hsv[0])); PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(hsv[1])); PyFloat_FromDouble(hsv[0]),
PyTuple_SET_ITEM(ret, 2, PyFloat_FromDouble(hsv[2])); PyFloat_FromDouble(hsv[1]),
PyFloat_FromDouble(hsv[2]));
return ret; return ret;
} }

@ -31,6 +31,7 @@
#include "BLI_math.h" #include "BLI_math.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
#include "../generic/python_utildefines.h"
#ifndef MATH_STANDALONE #ifndef MATH_STANDALONE
# include "BLI_dynstr.h" # include "BLI_dynstr.h"
@ -382,7 +383,7 @@ static PyObject *Euler_richcmpr(PyObject *a, PyObject *b, int op)
return NULL; return NULL;
} }
return Py_INCREF(res), res; return Py_INCREF_RET(res);
} }
/* ---------------------SEQUENCE PROTOCOLS------------------------ */ /* ---------------------SEQUENCE PROTOCOLS------------------------ */

@ -32,6 +32,8 @@
#include "BLI_math.h" #include "BLI_math.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
#include "../generic/python_utildefines.h"
#ifndef MATH_STANDALONE #ifndef MATH_STANDALONE
# include "BLI_string.h" # include "BLI_string.h"
# include "BLI_dynstr.h" # include "BLI_dynstr.h"
@ -1653,10 +1655,10 @@ static PyObject *Matrix_decompose(MatrixObject *self)
mat3_to_quat(quat, rot); mat3_to_quat(quat, rot);
ret = PyTuple_New(3); ret = PyTuple_New(3);
PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(loc, 3, NULL)); PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 1, Quaternion_CreatePyObject(quat, NULL)); Vector_CreatePyObject(loc, 3, NULL),
PyTuple_SET_ITEM(ret, 2, Vector_CreatePyObject(size, 3, NULL)); Quaternion_CreatePyObject(quat, NULL),
Vector_CreatePyObject(size, 3, NULL));
return ret; return ret;
} }
@ -2032,7 +2034,7 @@ static PyObject *Matrix_richcmpr(PyObject *a, PyObject *b, int op)
return NULL; return NULL;
} }
return Py_INCREF(res), res; return Py_INCREF_RET(res);
} }
/*---------------------SEQUENCE PROTOCOLS------------------------ /*---------------------SEQUENCE PROTOCOLS------------------------

@ -32,6 +32,8 @@
#include "BLI_math.h" #include "BLI_math.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
#include "../generic/python_utildefines.h"
#ifndef MATH_STANDALONE #ifndef MATH_STANDALONE
# include "BLI_dynstr.h" # include "BLI_dynstr.h"
#endif #endif
@ -169,8 +171,9 @@ static PyObject *Quaternion_to_axis_angle(QuaternionObject *self)
quat__axis_angle_sanitize(axis, &angle); quat__axis_angle_sanitize(axis, &angle);
ret = PyTuple_New(2); ret = PyTuple_New(2);
PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(axis, 3, NULL)); PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(angle)); Vector_CreatePyObject(axis, 3, NULL),
PyFloat_FromDouble(angle));
return ret; return ret;
} }
@ -544,7 +547,7 @@ static PyObject *Quaternion_richcmpr(PyObject *a, PyObject *b, int op)
return NULL; return NULL;
} }
return Py_INCREF(res), res; return Py_INCREF_RET(res);
} }
/* ---------------------SEQUENCE PROTOCOLS------------------------ */ /* ---------------------SEQUENCE PROTOCOLS------------------------ */

@ -42,6 +42,7 @@
#include "BLI_math.h" #include "BLI_math.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
#include "../generic/python_utildefines.h"
/*-------------------------DOC STRINGS ---------------------------*/ /*-------------------------DOC STRINGS ---------------------------*/
PyDoc_STRVAR(M_Geometry_doc, PyDoc_STRVAR(M_Geometry_doc,
@ -207,8 +208,9 @@ static PyObject *M_Geometry_intersect_line_line(PyObject *UNUSED(self), PyObject
} }
else { else {
tuple = PyTuple_New(2); tuple = PyTuple_New(2);
PyTuple_SET_ITEM(tuple, 0, Vector_CreatePyObject(i1, len, NULL)); PyTuple_SET_ITEMS(tuple,
PyTuple_SET_ITEM(tuple, 1, Vector_CreatePyObject(i2, len, NULL)); Vector_CreatePyObject(i1, len, NULL),
Vector_CreatePyObject(i2, len, NULL));
return tuple; return tuple;
} }
} }
@ -267,8 +269,9 @@ static PyObject *M_Geometry_intersect_sphere_sphere_2d(PyObject *UNUSED(self), P
(dist < FLT_EPSILON)) (dist < FLT_EPSILON))
{ {
/* out of range */ /* out of range */
PyTuple_SET_ITEM(ret, 0, Py_None); Py_INCREF(Py_None); PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 1, Py_None); Py_INCREF(Py_None); Py_INCREF_RET(Py_None),
Py_INCREF_RET(Py_None));
} }
else { else {
const float dist_delta = ((rad_a * rad_a) - (rad_b * rad_b) + (dist * dist)) / (2.0f * dist); 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[0] = i_cent[0] - h * v_ab[1] / dist;
i2[1] = i_cent[1] + h * v_ab[0] / dist; i2[1] = i_cent[1] + h * v_ab[0] / dist;
PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(i1, 2, NULL)); PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 1, Vector_CreatePyObject(i2, 2, NULL)); Vector_CreatePyObject(i1, 2, NULL),
Vector_CreatePyObject(i2, 2, NULL));
} }
return ret; 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); ret_no = Vector_CreatePyObject(isect_no, 3, NULL);
} }
else { else {
ret_co = Py_None; ret_co = Py_INCREF_RET(Py_None);
ret_no = Py_None; ret_no = Py_INCREF_RET(Py_None);
Py_INCREF(ret_co);
Py_INCREF(ret_no);
} }
ret = PyTuple_New(2); ret = PyTuple_New(2);
PyTuple_SET_ITEM(ret, 0, ret_co); PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 1, ret_no); ret_co,
ret_no);
return ret; return ret;
} }
@ -631,11 +633,9 @@ static PyObject *M_Geometry_intersect_line_sphere(PyObject *UNUSED(self), PyObje
break; break;
} }
if (use_a) { PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(isect_a, 3, NULL)); } PyTuple_SET_ITEMS(ret,
else { PyTuple_SET_ITEM(ret, 0, Py_None); Py_INCREF(Py_None); } 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));
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); }
return ret; return ret;
} }
@ -705,11 +705,9 @@ static PyObject *M_Geometry_intersect_line_sphere_2d(PyObject *UNUSED(self), PyO
break; break;
} }
if (use_a) { PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(isect_a, 2, NULL)); } PyTuple_SET_ITEMS(ret,
else { PyTuple_SET_ITEM(ret, 0, Py_None); Py_INCREF(Py_None); } 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));
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); }
return ret; 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); lambda = closest_to_line_v3(pt_out, pt, line_a, line_b);
ret = PyTuple_New(2); ret = PyTuple_New(2);
PyTuple_SET_ITEM(ret, 0, Vector_CreatePyObject(pt_out, size, NULL)); PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(lambda)); Vector_CreatePyObject(pt_out, size, NULL),
PyFloat_FromDouble(lambda));
return ret; return ret;
} }
@ -1090,8 +1089,9 @@ static PyObject *M_Geometry_points_in_planes(PyObject *UNUSED(self), PyObject *a
{ {
PyObject *ret = PyTuple_New(2); PyObject *ret = PyTuple_New(2);
PyTuple_SET_ITEM(ret, 0, py_verts); PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 1, py_plane_index); py_verts,
py_plane_index);
return ret; return ret;
} }
} }
@ -1397,8 +1397,9 @@ static PyObject *M_Geometry_box_pack_2d(PyObject *UNUSED(self), PyObject *boxlis
} }
ret = PyTuple_New(2); ret = PyTuple_New(2);
PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(tot_width)); PyTuple_SET_ITEMS(ret,
PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(tot_height)); PyFloat_FromDouble(tot_width),
PyFloat_FromDouble(tot_height));
return ret; return ret;
} }

@ -35,6 +35,7 @@
#include "BLI_kdtree.h" #include "BLI_kdtree.h"
#include "../generic/py_capi_utils.h" #include "../generic/py_capi_utils.h"
#include "../generic/python_utildefines.h"
#include "mathutils.h" #include "mathutils.h"
#include "mathutils_kdtree.h" /* own include */ #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(nearest->index >= 0);
BLI_assert(PyTuple_GET_SIZE(py_retval) == 3); BLI_assert(PyTuple_GET_SIZE(py_retval) == 3);
PyTuple_SET_ITEM(py_retval, 0, Vector_CreatePyObject((float *)nearest->co, 3, NULL)); PyTuple_SET_ITEMS(py_retval,
PyTuple_SET_ITEM(py_retval, 1, PyLong_FromLong(nearest->index)); Vector_CreatePyObject((float *)nearest->co, 3, NULL),
PyTuple_SET_ITEM(py_retval, 2, PyFloat_FromDouble(nearest->dist)); PyLong_FromLong(nearest->index),
PyFloat_FromDouble(nearest->dist));
} }
static PyObject *kdtree_nearest_to_py(const KDTreeNearest *nearest) static PyObject *kdtree_nearest_to_py(const KDTreeNearest *nearest)