diff options
author | Campbell Barton <ideasman42@gmail.com> | 2017-08-20 08:44:54 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2017-08-20 08:49:10 +0300 |
commit | 46cf33bf0146946ed05756a9b8bfa1318538f020 (patch) | |
tree | 951f8a09224e7c0c591cd7befdc99788e52a175d /source/blender/python/bmesh | |
parent | a10a7f42de29d251e27f6b85e7f3cac26e6843ed (diff) |
PyAPI: Make use of PyC_LongAs... API
Avoids setting exceptions inline,
also use Matrix_ParseAny for bmesh.ops.
Some inline exceptions are kept because they show useful details.
Diffstat (limited to 'source/blender/python/bmesh')
-rw-r--r-- | source/blender/python/bmesh/bmesh_py_ops_call.c | 41 | ||||
-rw-r--r-- | source/blender/python/bmesh/bmesh_py_types.c | 71 | ||||
-rw-r--r-- | source/blender/python/bmesh/bmesh_py_types_customdata.c | 5 | ||||
-rw-r--r-- | source/blender/python/bmesh/bmesh_py_types_meshdata.c | 11 |
4 files changed, 48 insertions, 80 deletions
diff --git a/source/blender/python/bmesh/bmesh_py_ops_call.c b/source/blender/python/bmesh/bmesh_py_ops_call.c index 8f287918a4a..b8ff0588581 100644 --- a/source/blender/python/bmesh/bmesh_py_ops_call.c +++ b/source/blender/python/bmesh/bmesh_py_ops_call.c @@ -44,6 +44,7 @@ #include "bmesh_py_types.h" #include "../generic/python_utildefines.h" +#include "../generic/py_capi_utils.h" static int bpy_bm_op_as_py_error(BMesh *bm) { @@ -152,11 +153,9 @@ static int bpy_slot_from_py( switch (slot->slot_type) { case BMO_OP_SLOT_BOOL: { - int param; + const int param = PyC_Long_AsBool(value); - param = PyLong_AsLong(value); - - if (param < 0) { + if (param == -1) { PyErr_Format(PyExc_TypeError, "%.200s: keyword \"%.200s\" expected True/False or 0/1, not %.200s", opname, slot_name, Py_TYPE(value)->tp_name); @@ -170,23 +169,16 @@ static int bpy_slot_from_py( } case BMO_OP_SLOT_INT: { - int overflow; - long param = PyLong_AsLongAndOverflow(value, &overflow); - if (overflow || (param > INT_MAX) || (param < INT_MIN)) { - PyErr_Format(PyExc_ValueError, - "%.200s: keyword \"%.200s\" value not in 'int' range " - "(" STRINGIFY(INT_MIN) ", " STRINGIFY(INT_MAX) ")", - opname, slot_name, Py_TYPE(value)->tp_name); - return -1; - } - else if (param == -1 && PyErr_Occurred()) { + const int param = PyC_Long_AsI32(value); + + if (param == -1 && PyErr_Occurred()) { PyErr_Format(PyExc_TypeError, "%.200s: keyword \"%.200s\" expected an int, not %.200s", opname, slot_name, Py_TYPE(value)->tp_name); return -1; } else { - BMO_SLOT_AS_INT(slot) = (int)param; + BMO_SLOT_AS_INT(slot) = param; } break; } @@ -209,25 +201,18 @@ static int bpy_slot_from_py( /* XXX - BMesh operator design is crappy here, operator slot should define matrix size, * not the caller! */ unsigned short size; - if (!MatrixObject_Check(value)) { - PyErr_Format(PyExc_TypeError, - "%.200s: keyword \"%.200s\" expected a Matrix, not %.200s", - opname, slot_name, Py_TYPE(value)->tp_name); + MatrixObject *pymat; + if (!Matrix_ParseAny(value, &pymat)) { return -1; } - else if (BaseMath_ReadCallback((MatrixObject *)value) == -1) { - return -1; - } - else if (((size = ((MatrixObject *)value)->num_col) != ((MatrixObject *)value)->num_row) || - (ELEM(size, 3, 4) == false)) - { + if ((size = (pymat->num_col) != pymat->num_row) || (!ELEM(size, 3, 4))) { PyErr_Format(PyExc_TypeError, "%.200s: keyword \"%.200s\" expected a 3x3 or 4x4 matrix Matrix", opname, slot_name); return -1; } - BMO_slot_mat_set(bmop, bmop->slots_in, slot_name, ((MatrixObject *)value)->matrix, size); + BMO_slot_mat_set(bmop, bmop->slots_in, slot_name, pymat->matrix, size); break; } case BMO_OP_SLOT_VEC: @@ -436,7 +421,7 @@ static int bpy_slot_from_py( return -1; /* error is set in bpy_slot_from_py_elem_check() */ } - value_i = PyLong_AsLong(arg_value); + value_i = PyC_Long_AsI32(arg_value); if (value_i == -1 && PyErr_Occurred()) { PyErr_Format(PyExc_TypeError, @@ -466,7 +451,7 @@ static int bpy_slot_from_py( return -1; /* error is set in bpy_slot_from_py_elem_check() */ } - value_i = PyLong_AsLong(arg_value); + value_i = PyC_Long_AsI32(arg_value); if (value_i == -1 && PyErr_Occurred()) { PyErr_Format(PyExc_TypeError, diff --git a/source/blender/python/bmesh/bmesh_py_types.c b/source/blender/python/bmesh/bmesh_py_types.c index b20c03bee28..faaa2aecb4c 100644 --- a/source/blender/python/bmesh/bmesh_py_types.c +++ b/source/blender/python/bmesh/bmesh_py_types.c @@ -124,25 +124,19 @@ static int bpy_bm_elem_hflag_set(BPy_BMElem *self, PyObject *value, void *flag) BPY_BM_CHECK_INT(self); - param = PyLong_AsLong(value); - - if ((unsigned int)param <= 1) { - if (hflag == BM_ELEM_SELECT) - BM_elem_select_set(self->bm, self->ele, param); - else - BM_elem_flag_set(self->ele, hflag, param); + if ((param = PyC_Long_AsBool(value)) == -1) { + return -1; + } - return 0; + if (hflag == BM_ELEM_SELECT) { + BM_elem_select_set(self->bm, self->ele, param); } else { - PyErr_Format(PyExc_TypeError, - "expected True/False or 0/1, not %.200s", - Py_TYPE(value)->tp_name); - return -1; + BM_elem_flag_set(self->ele, hflag, param); } + return -1; } - PyDoc_STRVAR(bpy_bm_elem_index_doc, "Index of this element.\n" "\n" @@ -169,21 +163,17 @@ static int bpy_bm_elem_index_set(BPy_BMElem *self, PyObject *value, void *UNUSED BPY_BM_CHECK_INT(self); - param = PyLong_AsLong(value); - - if (param == -1 && PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, - "expected an int type"); + if (((param = PyC_Long_AsI32(value)) == -1) && PyErr_Occurred()) { + /* error is set */ return -1; } - else { - BM_elem_index_set(self->ele, param); /* set_dirty! */ - /* when setting the index assume its set invalid */ - self->bm->elem_index_dirty |= self->ele->head.htype; + BM_elem_index_set(self->ele, param); /* set_dirty! */ - return 0; - } + /* when setting the index assume its set invalid */ + self->bm->elem_index_dirty |= self->ele->head.htype; + + return 0; } /* type specific get/sets @@ -506,14 +496,12 @@ static int bpy_bmface_material_index_set(BPy_BMFace *self, PyObject *value) BPY_BM_CHECK_INT(self); - param = PyLong_AsLong(value); - - if (param == -1 && PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, - "expected an int type"); + if (((param = PyC_Long_AsI32(value)) == -1) && PyErr_Occurred()) { + /* error is set */ return -1; } - else if ((param < 0) || (param > MAXMAT)) { + + if ((param < 0) || (param > MAXMAT)) { /* normally we clamp but in this case raise an error */ PyErr_SetString(PyExc_ValueError, "material index outside of usable range (0 - 32766)"); @@ -1113,15 +1101,16 @@ static PyObject *bpy_bmesh_select_flush(BPy_BMesh *self, PyObject *value) BPY_BM_CHECK_OBJ(self); - param = PyLong_AsLong(value); - if (param != false && param != true) { - PyErr_SetString(PyExc_TypeError, - "expected a boolean type 0/1"); + if ((param = PyC_Long_AsBool(value)) == -1) { return NULL; } - if (param) BM_mesh_select_flush(self->bm); - else BM_mesh_deselect_flush(self->bm); + if (param) { + BM_mesh_select_flush(self->bm); + } + else { + BM_mesh_deselect_flush(self->bm); + } Py_RETURN_NONE; } @@ -1301,10 +1290,7 @@ static PyObject *bpy_bm_elem_select_set(BPy_BMElem *self, PyObject *value) BPY_BM_CHECK_OBJ(self); - param = PyLong_AsLong(value); - if (param != false && param != true) { - PyErr_SetString(PyExc_TypeError, - "expected a boolean type 0/1"); + if ((param = PyC_Long_AsBool(value)) == -1) { return NULL; } @@ -1329,10 +1315,7 @@ static PyObject *bpy_bm_elem_hide_set(BPy_BMElem *self, PyObject *value) BPY_BM_CHECK_OBJ(self); - param = PyLong_AsLong(value); - if (param != false && param != true) { - PyErr_SetString(PyExc_TypeError, - "expected a boolean type 0/1"); + if ((param = PyC_Long_AsBool(value)) == -1) { return NULL; } diff --git a/source/blender/python/bmesh/bmesh_py_types_customdata.c b/source/blender/python/bmesh/bmesh_py_types_customdata.c index 908f6b5a734..cb95ded4f0d 100644 --- a/source/blender/python/bmesh/bmesh_py_types_customdata.c +++ b/source/blender/python/bmesh/bmesh_py_types_customdata.c @@ -43,6 +43,7 @@ #include "../mathutils/mathutils.h" #include "../generic/python_utildefines.h" +#include "../generic/py_capi_utils.h" #include "BKE_customdata.h" @@ -1074,9 +1075,9 @@ int BPy_BMLayerItem_SetItem(BPy_BMElem *py_ele, BPy_BMLayerItem *py_layer, PyObj } case CD_PROP_INT: { - int tmp_val = PyLong_AsLong(py_value); + int tmp_val = PyC_Long_AsI32(py_value); if (UNLIKELY(tmp_val == -1 && PyErr_Occurred())) { - PyErr_Format(PyExc_TypeError, "expected an int, not a %.200s", Py_TYPE(py_value)->tp_name); + /* error is set */ ret = -1; } else { diff --git a/source/blender/python/bmesh/bmesh_py_types_meshdata.c b/source/blender/python/bmesh/bmesh_py_types_meshdata.c index 92c11a03433..b01d3f89d4e 100644 --- a/source/blender/python/bmesh/bmesh_py_types_meshdata.c +++ b/source/blender/python/bmesh/bmesh_py_types_meshdata.c @@ -45,6 +45,7 @@ #include "bmesh_py_types_meshdata.h" +#include "../generic/py_capi_utils.h" #include "../generic/python_utildefines.h" @@ -188,7 +189,7 @@ static int bpy_bmloopuv_flag_set(BPy_BMLoopUV *self, PyObject *value, void *flag { const int flag = GET_INT_FROM_POINTER(flag_p); - switch (PyLong_AsLong(value)) { + switch (PyC_Long_AsBool(value)) { case true: self->data->flag |= flag; return 0; @@ -196,8 +197,7 @@ static int bpy_bmloopuv_flag_set(BPy_BMLoopUV *self, PyObject *value, void *flag self->data->flag &= ~flag; return 0; default: - PyErr_SetString(PyExc_TypeError, - "expected a boolean type 0/1"); + /* error is set */ return -1; } } @@ -297,7 +297,7 @@ static int bpy_bmvertskin_flag_set(BPy_BMVertSkin *self, PyObject *value, void * { const int flag = GET_INT_FROM_POINTER(flag_p); - switch (PyLong_AsLong(value)) { + switch (PyC_Long_AsBool(value)) { case true: self->data->flag |= flag; return 0; @@ -305,8 +305,7 @@ static int bpy_bmvertskin_flag_set(BPy_BMVertSkin *self, PyObject *value, void * self->data->flag &= ~flag; return 0; default: - PyErr_SetString(PyExc_TypeError, - "expected a boolean type 0/1"); + /* error is set */ return -1; } } |