diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-01-06 08:42:22 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-01-06 11:09:11 +0300 |
commit | 9fd569a654ded46901c7f20c5fe080972cbb10d2 (patch) | |
tree | 933b893aeaa0a8bffe230933523512340dfa016d /source/blender/python/mathutils | |
parent | ee58d449455df9470c4a0a902056b8c2001128bf (diff) |
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.
Diffstat (limited to 'source/blender/python/mathutils')
7 files changed, 60 insertions, 47 deletions
diff --git a/source/blender/python/mathutils/mathutils.c b/source/blender/python/mathutils/mathutils.c index 0a550105124..6fe8dc3866b 100644 --- a/source/blender/python/mathutils/mathutils.c +++ b/source/blender/python/mathutils/mathutils.c @@ -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"; diff --git a/source/blender/python/mathutils/mathutils_Color.c b/source/blender/python/mathutils/mathutils_Color.c index 5bc6f6177de..8426038f2d4 100644 --- a/source/blender/python/mathutils/mathutils_Color.c +++ b/source/blender/python/mathutils/mathutils_Color.c @@ -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; } diff --git a/source/blender/python/mathutils/mathutils_Euler.c b/source/blender/python/mathutils/mathutils_Euler.c index 1c45d5e88ac..9c0ced39403 100644 --- a/source/blender/python/mathutils/mathutils_Euler.c +++ b/source/blender/python/mathutils/mathutils_Euler.c @@ -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------------------------ */ diff --git a/source/blender/python/mathutils/mathutils_Matrix.c b/source/blender/python/mathutils/mathutils_Matrix.c index 5be4fdedff1..4706c09176e 100644 --- a/source/blender/python/mathutils/mathutils_Matrix.c +++ b/source/blender/python/mathutils/mathutils_Matrix.c @@ -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------------------------ diff --git a/source/blender/python/mathutils/mathutils_Quaternion.c b/source/blender/python/mathutils/mathutils_Quaternion.c index 7e15a3fc604..d422634a496 100644 --- a/source/blender/python/mathutils/mathutils_Quaternion.c +++ b/source/blender/python/mathutils/mathutils_Quaternion.c @@ -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------------------------ */ diff --git a/source/blender/python/mathutils/mathutils_geometry.c b/source/blender/python/mathutils/mathutils_geometry.c index c5368381e2f..261234e6aae 100644 --- a/source/blender/python/mathutils/mathutils_geometry.c +++ b/source/blender/python/mathutils/mathutils_geometry.c @@ -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; } diff --git a/source/blender/python/mathutils/mathutils_kdtree.c b/source/blender/python/mathutils/mathutils_kdtree.c index 677fa9830f1..199c2e02da4 100644 --- a/source/blender/python/mathutils/mathutils_kdtree.c +++ b/source/blender/python/mathutils/mathutils_kdtree.c @@ -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) |