Use PyC_ParseBool to parse bools
authorCampbell Barton <ideasman42@gmail.com>
Tue, 4 Aug 2015 08:34:20 +0000 (18:34 +1000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 4 Aug 2015 08:49:42 +0000 (18:49 +1000)
This could cause problems since they could be any int,
then passed directly to internal functions that assume bools.

source/blender/python/bmesh/bmesh_py_api.c
source/blender/python/bmesh/bmesh_py_types.c
source/blender/python/bmesh/bmesh_py_utils.c
source/blender/python/intern/bpy.c
source/blender/python/intern/bpy_library.c
source/blender/python/intern/bpy_operator.c
source/blender/python/intern/bpy_props.c
source/blender/python/intern/bpy_utils_units.c
source/blender/python/mathutils/mathutils_geometry.c

index f74737b..7c5d196 100644 (file)
@@ -116,11 +116,14 @@ static PyObject *bpy_bm_update_edit_mesh(PyObject *UNUSED(self), PyObject *args,
        static const char *kwlist[] = {"mesh", "tessface", "destructive", NULL};
        PyObject *py_me;
        Mesh *me;
-       int do_tessface = true;
-       int is_destructive = true;
-
-       if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:update_edit_mesh", (char **)kwlist,
-                                        &py_me, &do_tessface, &is_destructive))
+       bool do_tessface = true;
+       bool is_destructive = true;
+
+       if (!PyArg_ParseTupleAndKeywords(
+               args, kw, "O|O&O&:update_edit_mesh", (char **)kwlist,
+               &py_me,
+               PyC_ParseBool, &do_tessface,
+               PyC_ParseBool, &is_destructive))
        {
                return NULL;
        }
index e834c83..728332d 100644 (file)
@@ -943,16 +943,22 @@ static PyObject *bpy_bmesh_from_object(BPy_BMesh *self, PyObject *args)
        Object *ob;
        struct Scene *scene;
        BMesh *bm;
-       int use_deform = true;
-       int use_render = false;
-       int use_cage   = false;
-       int use_fnorm  = true;
+       bool use_deform = true;
+       bool use_render = false;
+       bool use_cage   = false;
+       bool use_fnorm  = true;
        DerivedMesh *dm;
        const int mask = CD_MASK_BMESH;
 
        BPY_BM_CHECK_OBJ(self);
 
-       if (!PyArg_ParseTuple(args, "OO|iiii:from_object", &py_object, &py_scene, &use_render, &use_cage, &use_fnorm) ||
+       if (!PyArg_ParseTuple(
+               args, "OO|O&O&O&O&:from_object",
+               &py_object, &py_scene,
+               PyC_ParseBool, &use_deform,
+               PyC_ParseBool, &use_render,
+               PyC_ParseBool, &use_cage,
+               PyC_ParseBool, &use_fnorm) ||
            !(ob    = PyC_RNA_AsPointer(py_object, "Object")) ||
            !(scene = PyC_RNA_AsPointer(py_scene,  "Scene")))
        {
@@ -1044,14 +1050,18 @@ static PyObject *bpy_bmesh_from_mesh(BPy_BMesh *self, PyObject *args, PyObject *
        BMesh *bm;
        PyObject *py_mesh;
        Mesh *me;
-       int use_fnorm  = true;
-       int use_shape_key = false;
+       bool use_fnorm  = true;
+       bool use_shape_key = false;
        int shape_key_index = 0;
 
        BPY_BM_CHECK_OBJ(self);
 
-       if (!PyArg_ParseTupleAndKeywords(args, kw, "O|iii:from_mesh", (char **)kwlist,
-                                        &py_mesh, &use_fnorm, &use_shape_key, &shape_key_index) ||
+       if (!PyArg_ParseTupleAndKeywords(
+               args, kw, "O|O&O&i:from_mesh", (char **)kwlist,
+               &py_mesh,
+               PyC_ParseBool, &use_fnorm,
+               PyC_ParseBool, &use_shape_key,
+               &shape_key_index) ||
            !(me = PyC_RNA_AsPointer(py_mesh, "Mesh")))
        {
                return NULL;
@@ -1686,12 +1696,14 @@ PyDoc_STRVAR(bpy_bmface_copy_from_face_interp_doc,
 static PyObject *bpy_bmface_copy_from_face_interp(BPy_BMFace *self, PyObject *args)
 {
        BPy_BMFace *py_face = NULL;
-       int do_vertex   = true;
+       bool do_vertex   = true;
 
        BPY_BM_CHECK_OBJ(self);
 
-       if (!PyArg_ParseTuple(args, "O!|i:BMFace.copy_from_face_interp",
-                             &BPy_BMFace_Type, &py_face, &do_vertex))
+       if (!PyArg_ParseTuple(
+               args, "O!|O&:BMFace.copy_from_face_interp",
+               &BPy_BMFace_Type, &py_face,
+               PyC_ParseBool, &do_vertex))
        {
                return NULL;
        }
@@ -1724,16 +1736,17 @@ static PyObject *bpy_bmface_copy(BPy_BMFace *self, PyObject *args, PyObject *kw)
        static const char *kwlist[] = {"verts", "edges", NULL};
 
        BMesh *bm = self->bm;
-       int do_verts = true;
-       int do_edges = true;
+       bool do_verts = true;
+       bool do_edges = true;
 
        BMFace *f_cpy;
        BPY_BM_CHECK_OBJ(self);
 
-       if (!PyArg_ParseTupleAndKeywords(args, kw,
-                                        "|ii:BMFace.copy",
-                                        (char **)kwlist,
-                                        &do_verts, &do_edges))
+       if (!PyArg_ParseTupleAndKeywords(
+               args, kw,
+               "|O&O&:BMFace.copy", (char **)kwlist,
+               PyC_ParseBool, &do_verts,
+               PyC_ParseBool, &do_edges))
        {
                return NULL;
        }
@@ -1881,14 +1894,16 @@ PyDoc_STRVAR(bpy_bmloop_copy_from_face_interp_doc,
 static PyObject *bpy_bmloop_copy_from_face_interp(BPy_BMLoop *self, PyObject *args)
 {
        BPy_BMFace *py_face = NULL;
-       int do_vertex   = true;
-       int do_multires = true;
+       bool do_vertex   = true;
+       bool do_multires = true;
 
        BPY_BM_CHECK_OBJ(self);
 
-       if (!PyArg_ParseTuple(args, "O!|ii:BMLoop.copy_from_face_interp",
-                             &BPy_BMFace_Type, &py_face,
-                             &do_vertex, &do_multires))
+       if (!PyArg_ParseTuple(
+               args, "O!|O&O&:BMLoop.copy_from_face_interp",
+               &BPy_BMFace_Type, &py_face,
+               PyC_ParseBool, &do_vertex,
+               PyC_ParseBool, &do_multires))
        {
                return NULL;
        }
@@ -2461,7 +2476,7 @@ static PyObject *bpy_bmelemseq_sort(BPy_BMElemSeq *self, PyObject *args, PyObjec
 {
        static const char *kwlist[] = {"key", "reverse", NULL};
        PyObject *keyfunc = NULL; /* optional */
-       int do_reverse = false; /* optional */
+       bool do_reverse = false; /* optional */
 
        const char htype = bm_iter_itype_htype_map[self->itype];
        int n_elem;
@@ -2483,10 +2498,11 @@ static PyObject *bpy_bmelemseq_sort(BPy_BMElemSeq *self, PyObject *args, PyObjec
        BPY_BM_CHECK_OBJ(self);
 
        if (args != NULL) {
-               if (!PyArg_ParseTupleAndKeywords(args, kw,
-                                                "|Oi:BMElemSeq.sort",
-                                                (char **)kwlist,
-                                                &keyfunc, &do_reverse))
+               if (!PyArg_ParseTupleAndKeywords(
+                       args, kw,
+                       "|OO&:BMElemSeq.sort", (char **)kwlist,
+                       &keyfunc,
+                       PyC_ParseBool, &do_reverse))
                {
                        return NULL;
                }
index b789092..379aafa 100644 (file)
@@ -42,6 +42,7 @@
 #include "bmesh_py_types.h"
 #include "bmesh_py_utils.h" /* own include */
 
+#include "../generic/py_capi_utils.h"
 #include "../generic/python_utildefines.h"
 
 
@@ -397,14 +398,15 @@ PyDoc_STRVAR(bpy_bm_utils_edge_rotate_doc,
 static PyObject *bpy_bm_utils_edge_rotate(PyObject *UNUSED(self), PyObject *args)
 {
        BPy_BMEdge *py_edge;
-       int do_ccw = false;
+       bool do_ccw = false;
 
        BMesh *bm;
        BMEdge *e_new = NULL;
 
-       if (!PyArg_ParseTuple(args, "O!|i:edge_rotate",
-                             &BPy_BMEdge_Type, &py_edge,
-                             &do_ccw))
+       if (!PyArg_ParseTuple(
+               args, "O!|O&:edge_rotate",
+               &BPy_BMEdge_Type, &py_edge,
+               PyC_ParseBool, &do_ccw))
        {
                return NULL;
        }
@@ -455,7 +457,7 @@ static PyObject *bpy_bm_utils_face_split(PyObject *UNUSED(self), PyObject *args,
 
        /* optional */
        PyObject *py_coords = NULL;
-       int edge_exists = true;
+       bool edge_exists = true;
        BPy_BMEdge *py_edge_example = NULL;
 
        float *coords;
@@ -466,13 +468,15 @@ static PyObject *bpy_bm_utils_face_split(PyObject *UNUSED(self), PyObject *args,
        BMLoop *l_new = NULL;
        BMLoop *l_a, *l_b;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!O!|OiO!:face_split", (char **)kwlist,
-                                        &BPy_BMFace_Type, &py_face,
-                                        &BPy_BMVert_Type, &py_vert_a,
-                                        &BPy_BMVert_Type, &py_vert_b,
-                                        &py_coords,
-                                        &edge_exists,
-                                        &BPy_BMEdge_Type, &py_edge_example))
+       if (!PyArg_ParseTupleAndKeywords(
+               args, kw,
+               "O!O!O!|OO&O!:face_split", (char **)kwlist,
+               &BPy_BMFace_Type, &py_face,
+               &BPy_BMVert_Type, &py_vert_a,
+               &BPy_BMVert_Type, &py_vert_b,
+               &py_coords,
+               PyC_ParseBool, &edge_exists,
+               &BPy_BMEdge_Type, &py_edge_example))
        {
                return NULL;
        }
@@ -632,9 +636,13 @@ static PyObject *bpy_bm_utils_face_join(PyObject *UNUSED(self), PyObject *args)
        BMFace **face_array;
        Py_ssize_t face_seq_len = 0;
        BMFace *f_new;
-       int do_remove = true;
+       bool do_remove = true;
 
-       if (!PyArg_ParseTuple(args, "O|i:face_join", &py_face_array, &do_remove)) {
+       if (!PyArg_ParseTuple(
+               args, "O|i:face_join",
+               &py_face_array,
+               PyC_ParseBool, &do_remove))
+       {
                return NULL;
        }
 
index 9a5e488..fa2ad3a 100644 (file)
@@ -114,13 +114,16 @@ static PyObject *bpy_blend_paths(PyObject *UNUSED(self), PyObject *args, PyObjec
        int flag = 0;
        PyObject *list;
 
-       int absolute = false;
-       int packed   = false;
-       int local    = false;
+       bool absolute = false;
+       bool packed   = false;
+       bool local    = false;
        static const char *kwlist[] = {"absolute", "packed", "local", NULL};
 
-       if (!PyArg_ParseTupleAndKeywords(args, kw, "|iii:blend_paths",
-                                        (char **)kwlist, &absolute, &packed, &local))
+       if (!PyArg_ParseTupleAndKeywords(
+               args, kw, "|O&O&O&:blend_paths", (char **)kwlist,
+               PyC_ParseBool, &absolute,
+               PyC_ParseBool, &packed,
+               PyC_ParseBool, &local))
        {
                return NULL;
        }
index 3b95b99..a5879f1 100644 (file)
@@ -53,6 +53,7 @@
 #include "bpy_util.h"
 #include "bpy_library.h"
 
+#include "../generic/py_capi_utils.h"
 #include "../generic/python_utildefines.h"
 
 /* nifty feature. swap out strings for RNA data */
@@ -189,10 +190,17 @@ static PyObject *bpy_lib_load(PyObject *UNUSED(self), PyObject *args, PyObject *
        static const char *kwlist[] = {"filepath", "link", "relative", NULL};
        BPy_Library *ret;
        const char *filename = NULL;
-       int is_rel = 0, is_link = 0;
-
-       if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|ii:load", (char **)kwlist, &filename, &is_link, &is_rel))
+       bool is_rel = false, is_link = false;
+
+       if (!PyArg_ParseTupleAndKeywords(
+               args, kwds,
+               "s|O&O&:load", (char **)kwlist,
+               &filename,
+               PyC_ParseBool, &is_link,
+               PyC_ParseBool, &is_rel))
+       {
                return NULL;
+       }
 
        ret = PyObject_New(BPy_Library, &bpy_lib_Type);
 
index fc17173..7cccc20 100644 (file)
@@ -45,6 +45,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/py_capi_utils.h"
 #include "../generic/python_utildefines.h"
 
 #include "RNA_access.h"
@@ -311,8 +312,8 @@ static PyObject *pyop_as_string(PyObject *UNUSED(self), PyObject *args)
 
        const char *opname;
        PyObject *kw = NULL; /* optional args */
-       int all_args = 1;
-       int macro_args = 1;
+       bool all_args = true;
+       bool macro_args = true;
        int error_val = 0;
 
        char *buf = NULL;
@@ -325,8 +326,14 @@ static PyObject *pyop_as_string(PyObject *UNUSED(self), PyObject *args)
                return NULL;
        }
        
-       if (!PyArg_ParseTuple(args, "s|O!ii:_bpy.ops.as_string", &opname, &PyDict_Type, &kw, &all_args, &macro_args))
+       if (!PyArg_ParseTuple(
+               args, "s|O!O&O&:_bpy.ops.as_string",
+               &opname, &PyDict_Type, &kw,
+               PyC_ParseBool, &all_args,
+               PyC_ParseBool, &macro_args))
+       {
                return NULL;
+       }
 
        ot = WM_operatortype_find(opname, true);
 
index 6e70f97..19ded7f 100644 (file)
@@ -1951,7 +1951,7 @@ static PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
                                               "options", "subtype", "update", "get", "set", NULL};
                const char *id = NULL, *name = NULL, *description = "";
                int id_len;
-               int def = 0;
+               bool def = false;
                PropertyRNA *prop;
                PyObject *pyopts = NULL;
                int opts = 0;
@@ -1962,9 +1962,9 @@ static PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
                PyObject *set_cb = NULL;
 
                if (!PyArg_ParseTupleAndKeywords(args, kw,
-                                                "s#|ssiO!sOOO:BoolProperty",
+                                                "s#|ssO&O!sOOO:BoolProperty",
                                                 (char **)kwlist, &id, &id_len,
-                                                &name, &description, &def,
+                                                &name, &description, PyC_ParseBool, &def,
                                                 &PySet_Type, &pyopts, &pysubtype,
                                                 &update_cb, &get_cb, &set_cb))
                {
index d40e7e0..057df4c 100644 (file)
@@ -249,12 +249,17 @@ static PyObject *bpyunits_to_string(PyObject *UNUSED(self), PyObject *args, PyOb
 
        char *usys_str = NULL, *ucat_str = NULL;
        double value = 0.0;
-       int precision = 3, split_unit = false, compatible_unit = false;
+       int precision = 3;
+       bool split_unit = false, compatible_unit = false;
 
        int usys, ucat;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kw, "ssd|ipp:bpy.utils.units.to_string", (char **)kwlist,
-                                        &usys_str, &ucat_str, &value, &precision, &split_unit, &compatible_unit))
+       if (!PyArg_ParseTupleAndKeywords(
+               args, kw,
+               "ssd|iO&O&:bpy.utils.units.to_string", (char **)kwlist,
+               &usys_str, &ucat_str, &value, &precision,
+               PyC_ParseBool, &split_unit,
+               PyC_ParseBool, &compatible_unit))
        {
                return NULL;
        }
index ae5c058..2684371 100644 (file)
@@ -42,6 +42,8 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+
+#include "../generic/py_capi_utils.h"
 #include "../generic/python_utildefines.h"
 
 /*-------------------------DOC STRINGS ---------------------------*/
@@ -77,12 +79,14 @@ static PyObject *M_Geometry_intersect_ray_tri(PyObject *UNUSED(self), PyObject *
        PyObject *py_ray, *py_ray_off, *py_tri[3];
        float dir[3], orig[3], tri[3][3], e1[3], e2[3], pvec[3], tvec[3], qvec[3];
        float det, inv_det, u, v, t;
-       int clip = 1;
+       bool clip = true;
        int i;
 
        if (!PyArg_ParseTuple(
-               args, "OOOOO|i:intersect_ray_tri",
-               UNPACK3_EX(&, py_tri, ), &py_ray, &py_ray_off, &clip))
+               args, "OOOOO|O&:intersect_ray_tri",
+               UNPACK3_EX(&, py_tri, ),
+               &py_ray, &py_ray_off,
+               PyC_ParseBool, &clip))
        {
                return NULL;
        }
@@ -477,12 +481,12 @@ static PyObject *M_Geometry_intersect_line_plane(PyObject *UNUSED(self), PyObjec
        PyObject *py_line_a, *py_line_b, *py_plane_co, *py_plane_no;
        float line_a[3], line_b[3], plane_co[3], plane_no[3];
        float isect[3];
-       int no_flip = false;
+       bool no_flip = false;
 
        if (!PyArg_ParseTuple(
-               args, "OOOO|i:intersect_line_plane",
+               args, "OOOO|O&:intersect_line_plane",
                &py_line_a, &py_line_b, &py_plane_co, &py_plane_no,
-               &no_flip))
+               PyC_ParseBool, &no_flip))
        {
                return NULL;
        }
@@ -589,14 +593,15 @@ static PyObject *M_Geometry_intersect_line_sphere(PyObject *UNUSED(self), PyObje
        PyObject *py_line_a, *py_line_b, *py_sphere_co;
        float line_a[3], line_b[3], sphere_co[3];
        float sphere_radius;
-       int clip = true;
+       bool clip = true;
 
        float isect_a[3];
        float isect_b[3];
 
        if (!PyArg_ParseTuple(
-               args, "OOOf|i:intersect_line_sphere",
-               &py_line_a, &py_line_b, &py_sphere_co, &sphere_radius, &clip))
+               args, "OOOf|O&:intersect_line_sphere",
+               &py_line_a, &py_line_b, &py_sphere_co, &sphere_radius,
+               PyC_ParseBool, &clip))
        {
                return NULL;
        }
@@ -661,14 +666,15 @@ static PyObject *M_Geometry_intersect_line_sphere_2d(PyObject *UNUSED(self), PyO
        PyObject *py_line_a, *py_line_b, *py_sphere_co;
        float line_a[2], line_b[2], sphere_co[2];
        float sphere_radius;
-       int clip = true;
+       bool clip = true;
 
        float isect_a[2];
        float isect_b[2];
 
        if (!PyArg_ParseTuple(
-               args, "OOOf|i:intersect_line_sphere_2d",
-               &py_line_a, &py_line_b, &py_sphere_co, &sphere_radius, &clip))
+               args, "OOOf|O&:intersect_line_sphere_2d",
+               &py_line_a, &py_line_b, &py_sphere_co, &sphere_radius,
+               PyC_ParseBool, &clip))
        {
                return NULL;
        }