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.
This commit is contained in:
2017-08-20 15:44:54 +10:00
parent a10a7f42de
commit 46cf33bf01
11 changed files with 70 additions and 98 deletions

View File

@@ -44,6 +44,7 @@
#include "bmesh_py_types.h" #include "bmesh_py_types.h"
#include "../generic/python_utildefines.h" #include "../generic/python_utildefines.h"
#include "../generic/py_capi_utils.h"
static int bpy_bm_op_as_py_error(BMesh *bm) static int bpy_bm_op_as_py_error(BMesh *bm)
{ {
@@ -152,11 +153,9 @@ static int bpy_slot_from_py(
switch (slot->slot_type) { switch (slot->slot_type) {
case BMO_OP_SLOT_BOOL: case BMO_OP_SLOT_BOOL:
{ {
int param; const int param = PyC_Long_AsBool(value);
param = PyLong_AsLong(value); if (param == -1) {
if (param < 0) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%.200s: keyword \"%.200s\" expected True/False or 0/1, not %.200s", "%.200s: keyword \"%.200s\" expected True/False or 0/1, not %.200s",
opname, slot_name, Py_TYPE(value)->tp_name); opname, slot_name, Py_TYPE(value)->tp_name);
@@ -170,23 +169,16 @@ static int bpy_slot_from_py(
} }
case BMO_OP_SLOT_INT: case BMO_OP_SLOT_INT:
{ {
int overflow; const int param = PyC_Long_AsI32(value);
long param = PyLong_AsLongAndOverflow(value, &overflow);
if (overflow || (param > INT_MAX) || (param < INT_MIN)) { if (param == -1 && PyErr_Occurred()) {
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()) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%.200s: keyword \"%.200s\" expected an int, not %.200s", "%.200s: keyword \"%.200s\" expected an int, not %.200s",
opname, slot_name, Py_TYPE(value)->tp_name); opname, slot_name, Py_TYPE(value)->tp_name);
return -1; return -1;
} }
else { else {
BMO_SLOT_AS_INT(slot) = (int)param; BMO_SLOT_AS_INT(slot) = param;
} }
break; break;
} }
@@ -209,25 +201,18 @@ static int bpy_slot_from_py(
/* XXX - BMesh operator design is crappy here, operator slot should define matrix size, /* XXX - BMesh operator design is crappy here, operator slot should define matrix size,
* not the caller! */ * not the caller! */
unsigned short size; unsigned short size;
if (!MatrixObject_Check(value)) { MatrixObject *pymat;
PyErr_Format(PyExc_TypeError, if (!Matrix_ParseAny(value, &pymat)) {
"%.200s: keyword \"%.200s\" expected a Matrix, not %.200s",
opname, slot_name, Py_TYPE(value)->tp_name);
return -1; return -1;
} }
else if (BaseMath_ReadCallback((MatrixObject *)value) == -1) { if ((size = (pymat->num_col) != pymat->num_row) || (!ELEM(size, 3, 4))) {
return -1;
}
else if (((size = ((MatrixObject *)value)->num_col) != ((MatrixObject *)value)->num_row) ||
(ELEM(size, 3, 4) == false))
{
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%.200s: keyword \"%.200s\" expected a 3x3 or 4x4 matrix Matrix", "%.200s: keyword \"%.200s\" expected a 3x3 or 4x4 matrix Matrix",
opname, slot_name); opname, slot_name);
return -1; 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; break;
} }
case BMO_OP_SLOT_VEC: 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() */ 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()) { if (value_i == -1 && PyErr_Occurred()) {
PyErr_Format(PyExc_TypeError, 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() */ 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()) { if (value_i == -1 && PyErr_Occurred()) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,

View File

@@ -124,24 +124,18 @@ static int bpy_bm_elem_hflag_set(BPy_BMElem *self, PyObject *value, void *flag)
BPY_BM_CHECK_INT(self); BPY_BM_CHECK_INT(self);
param = PyLong_AsLong(value); if ((param = PyC_Long_AsBool(value)) == -1) {
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);
return 0;
}
else {
PyErr_Format(PyExc_TypeError,
"expected True/False or 0/1, not %.200s",
Py_TYPE(value)->tp_name);
return -1; return -1;
} }
}
if (hflag == BM_ELEM_SELECT) {
BM_elem_select_set(self->bm, self->ele, param);
}
else {
BM_elem_flag_set(self->ele, hflag, param);
}
return -1;
}
PyDoc_STRVAR(bpy_bm_elem_index_doc, PyDoc_STRVAR(bpy_bm_elem_index_doc,
"Index of this element.\n" "Index of this element.\n"
@@ -169,21 +163,17 @@ static int bpy_bm_elem_index_set(BPy_BMElem *self, PyObject *value, void *UNUSED
BPY_BM_CHECK_INT(self); BPY_BM_CHECK_INT(self);
param = PyLong_AsLong(value); if (((param = PyC_Long_AsI32(value)) == -1) && PyErr_Occurred()) {
/* error is set */
if (param == -1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,
"expected an int type");
return -1; return -1;
} }
else {
BM_elem_index_set(self->ele, param); /* set_dirty! */
/* when setting the index assume its set invalid */ BM_elem_index_set(self->ele, param); /* set_dirty! */
self->bm->elem_index_dirty |= self->ele->head.htype;
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 /* 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); BPY_BM_CHECK_INT(self);
param = PyLong_AsLong(value); if (((param = PyC_Long_AsI32(value)) == -1) && PyErr_Occurred()) {
/* error is set */
if (param == -1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,
"expected an int type");
return -1; return -1;
} }
else if ((param < 0) || (param > MAXMAT)) {
if ((param < 0) || (param > MAXMAT)) {
/* normally we clamp but in this case raise an error */ /* normally we clamp but in this case raise an error */
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"material index outside of usable range (0 - 32766)"); "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); BPY_BM_CHECK_OBJ(self);
param = PyLong_AsLong(value); if ((param = PyC_Long_AsBool(value)) == -1) {
if (param != false && param != true) {
PyErr_SetString(PyExc_TypeError,
"expected a boolean type 0/1");
return NULL; return NULL;
} }
if (param) BM_mesh_select_flush(self->bm); if (param) {
else BM_mesh_deselect_flush(self->bm); BM_mesh_select_flush(self->bm);
}
else {
BM_mesh_deselect_flush(self->bm);
}
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@@ -1301,10 +1290,7 @@ static PyObject *bpy_bm_elem_select_set(BPy_BMElem *self, PyObject *value)
BPY_BM_CHECK_OBJ(self); BPY_BM_CHECK_OBJ(self);
param = PyLong_AsLong(value); if ((param = PyC_Long_AsBool(value)) == -1) {
if (param != false && param != true) {
PyErr_SetString(PyExc_TypeError,
"expected a boolean type 0/1");
return NULL; return NULL;
} }
@@ -1329,10 +1315,7 @@ static PyObject *bpy_bm_elem_hide_set(BPy_BMElem *self, PyObject *value)
BPY_BM_CHECK_OBJ(self); BPY_BM_CHECK_OBJ(self);
param = PyLong_AsLong(value); if ((param = PyC_Long_AsBool(value)) == -1) {
if (param != false && param != true) {
PyErr_SetString(PyExc_TypeError,
"expected a boolean type 0/1");
return NULL; return NULL;
} }

View File

@@ -43,6 +43,7 @@
#include "../mathutils/mathutils.h" #include "../mathutils/mathutils.h"
#include "../generic/python_utildefines.h" #include "../generic/python_utildefines.h"
#include "../generic/py_capi_utils.h"
#include "BKE_customdata.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: 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())) { 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; ret = -1;
} }
else { else {

View File

@@ -45,6 +45,7 @@
#include "bmesh_py_types_meshdata.h" #include "bmesh_py_types_meshdata.h"
#include "../generic/py_capi_utils.h"
#include "../generic/python_utildefines.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); const int flag = GET_INT_FROM_POINTER(flag_p);
switch (PyLong_AsLong(value)) { switch (PyC_Long_AsBool(value)) {
case true: case true:
self->data->flag |= flag; self->data->flag |= flag;
return 0; return 0;
@@ -196,8 +197,7 @@ static int bpy_bmloopuv_flag_set(BPy_BMLoopUV *self, PyObject *value, void *flag
self->data->flag &= ~flag; self->data->flag &= ~flag;
return 0; return 0;
default: default:
PyErr_SetString(PyExc_TypeError, /* error is set */
"expected a boolean type 0/1");
return -1; 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); const int flag = GET_INT_FROM_POINTER(flag_p);
switch (PyLong_AsLong(value)) { switch (PyC_Long_AsBool(value)) {
case true: case true:
self->data->flag |= flag; self->data->flag |= flag;
return 0; return 0;
@@ -305,8 +305,7 @@ static int bpy_bmvertskin_flag_set(BPy_BMVertSkin *self, PyObject *value, void *
self->data->flag &= ~flag; self->data->flag &= ~flag;
return 0; return 0;
default: default:
PyErr_SetString(PyExc_TypeError, /* error is set */
"expected a boolean type 0/1");
return -1; return -1;
} }
} }

View File

@@ -386,7 +386,7 @@ static IDProperty *idp_from_PyFloat(const char *name, PyObject *ob)
static IDProperty *idp_from_PyLong(const char *name, PyObject *ob) static IDProperty *idp_from_PyLong(const char *name, PyObject *ob)
{ {
IDPropertyTemplate val = {0}; IDPropertyTemplate val = {0};
val.i = _PyLong_AsInt(ob); val.i = PyC_Long_AsI32(ob);
if (val.i == -1 && PyErr_Occurred()) { if (val.i == -1 && PyErr_Occurred()) {
return NULL; return NULL;
} }
@@ -499,7 +499,7 @@ static IDProperty *idp_from_PySequence_Fast(const char *name, PyObject *ob)
prop_data = IDP_Array(prop); prop_data = IDP_Array(prop);
for (i = 0; i < val.array.len; i++) { for (i = 0; i < val.array.len; i++) {
item = ob_seq_fast_items[i]; item = ob_seq_fast_items[i];
if (((prop_data[i] = _PyLong_AsInt(item)) == -1) && PyErr_Occurred()) { if (((prop_data[i] = PyC_Long_AsI32(item)) == -1) && PyErr_Occurred()) {
return NULL; return NULL;
} }
} }
@@ -1337,7 +1337,7 @@ static int BPy_IDArray_SetItem(BPy_IDArray *self, int index, PyObject *value)
} }
case IDP_INT: case IDP_INT:
{ {
const int i = _PyLong_AsInt(value); const int i = PyC_Long_AsI32(value);
if (i == -1 && PyErr_Occurred()) { if (i == -1 && PyErr_Occurred()) {
return -1; return -1;
} }

View File

@@ -292,7 +292,7 @@ static PyObject *bpy_app_debug_value_get(PyObject *UNUSED(self), void *UNUSED(cl
static int bpy_app_debug_value_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure)) static int bpy_app_debug_value_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure))
{ {
int param = PyLong_AsLong(value); int param = PyC_Long_AsI32(value);
if (param == -1 && PyErr_Occurred()) { if (param == -1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "bpy.app.debug_value can only be set to a whole number"); PyErr_SetString(PyExc_TypeError, "bpy.app.debug_value can only be set to a whole number");

View File

@@ -320,7 +320,7 @@ static int bpy_prop_boolean_get_cb(struct PointerRNA *ptr, struct PropertyRNA *p
value = false; value = false;
} }
else { else {
value = PyLong_AsLong(ret); value = PyC_Long_AsI32(ret);
if (value == -1 && PyErr_Occurred()) { if (value == -1 && PyErr_Occurred()) {
printf_func_error(py_func); printf_func_error(py_func);
@@ -599,7 +599,7 @@ static int bpy_prop_int_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop)
value = 0.0f; value = 0.0f;
} }
else { else {
value = PyLong_AsLong(ret); value = PyC_Long_AsI32(ret);
if (value == -1 && PyErr_Occurred()) { if (value == -1 && PyErr_Occurred()) {
printf_func_error(py_func); printf_func_error(py_func);
@@ -1249,7 +1249,7 @@ static int bpy_prop_enum_get_cb(struct PointerRNA *ptr, struct PropertyRNA *prop
value = RNA_property_enum_get_default(ptr, prop); value = RNA_property_enum_get_default(ptr, prop);
} }
else { else {
value = PyLong_AsLong(ret); value = PyC_Long_AsI32(ret);
if (value == -1 && PyErr_Occurred()) { if (value == -1 && PyErr_Occurred()) {
printf_func_error(py_func); printf_func_error(py_func);

View File

@@ -1643,7 +1643,7 @@ static int pyrna_py_to_prop(
param = PyObject_IsTrue(value); param = PyObject_IsTrue(value);
} }
else { else {
param = PyLong_AsLong(value); param = PyC_Long_AsI32(value);
if (UNLIKELY(param & ~1)) { /* only accept 0/1 */ if (UNLIKELY(param & ~1)) { /* only accept 0/1 */
param = -1; /* error out below */ param = -1; /* error out below */
@@ -2079,10 +2079,10 @@ static int pyrna_py_to_prop_array_index(BPy_PropertyArrayRNA *self, int index, P
switch (RNA_property_type(prop)) { switch (RNA_property_type(prop)) {
case PROP_BOOLEAN: case PROP_BOOLEAN:
{ {
int param = PyLong_AsLong(value); int param = PyC_Long_AsBool(value);
if (param < 0 || param > 1) { if (param == -1) {
PyErr_SetString(PyExc_TypeError, "expected True/False or 0/1"); /* error is set */
ret = -1; ret = -1;
} }
else { else {
@@ -2092,7 +2092,7 @@ static int pyrna_py_to_prop_array_index(BPy_PropertyArrayRNA *self, int index, P
} }
case PROP_INT: case PROP_INT:
{ {
int param = PyLong_AsLong(value); int param = PyC_Long_AsI32(value);
if (param == -1 && PyErr_Occurred()) { if (param == -1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "expected an int type"); PyErr_SetString(PyExc_TypeError, "expected an int type");
ret = -1; ret = -1;

View File

@@ -38,6 +38,8 @@
#include "RNA_access.h" #include "RNA_access.h"
#include "../generic/py_capi_utils.h"
#define USE_MATHUTILS #define USE_MATHUTILS
#ifdef USE_MATHUTILS #ifdef USE_MATHUTILS
@@ -550,7 +552,7 @@ static void py_to_float(const struct ItemConvertArgData *arg, PyObject *py, char
static void py_to_int(const struct ItemConvertArgData *arg, PyObject *py, char *data) static void py_to_int(const struct ItemConvertArgData *arg, PyObject *py, char *data)
{ {
const int *range = arg->int_data.range; const int *range = arg->int_data.range;
int value = (int)PyLong_AsLong(py); int value = PyC_Long_AsI32(py);
CLAMP(value, range[0], range[1]); CLAMP(value, range[0], range[1]);
*(int *)data = value; *(int *)data = value;
} }

View File

@@ -32,6 +32,8 @@
#include "BLI_math.h" #include "BLI_math.h"
#include "BLI_utildefines.h" #include "BLI_utildefines.h"
#include "../generic/py_capi_utils.h"
#ifndef MATH_STANDALONE #ifndef MATH_STANDALONE
# include "BLI_dynstr.h" # include "BLI_dynstr.h"
#endif #endif
@@ -403,7 +405,7 @@ static PyObject *Vector_resize(VectorObject *self, PyObject *value)
return NULL; return NULL;
} }
if ((size = PyLong_AsLong(value)) == -1) { if ((size = PyC_Long_AsI32(value)) == -1) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"Vector.resize(size): " "Vector.resize(size): "
"expected size argument to be an integer"); "expected size argument to be an integer");

View File

@@ -761,7 +761,7 @@ static PyObject *C_BVHTree_FromPolygons(PyObject *UNUSED(cls), PyObject *args, P
py_tricoords_fast_items = PySequence_Fast_ITEMS(py_tricoords_fast); py_tricoords_fast_items = PySequence_Fast_ITEMS(py_tricoords_fast);
for (j = 0; j < 3; j++) { for (j = 0; j < 3; j++) {
tri[j] = (unsigned int)_PyLong_AsInt(py_tricoords_fast_items[j]); tri[j] = PyC_Long_AsU32(py_tricoords_fast_items[j]);
if (UNLIKELY(tri[j] >= (unsigned int)coords_len)) { if (UNLIKELY(tri[j] >= (unsigned int)coords_len)) {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"%s: index %d must be less than %d", "%s: index %d must be less than %d",
@@ -812,7 +812,7 @@ static PyObject *C_BVHTree_FromPolygons(PyObject *UNUSED(cls), PyObject *args, P
p_plink_prev = &plink->next; p_plink_prev = &plink->next;
for (j = 0; j < py_tricoords_len; j++) { for (j = 0; j < py_tricoords_len; j++) {
plink->poly[j] = (unsigned int)_PyLong_AsInt(py_tricoords_fast_items[j]); plink->poly[j] = PyC_Long_AsU32(py_tricoords_fast_items[j]);
if (UNLIKELY(plink->poly[j] >= (unsigned int)coords_len)) { if (UNLIKELY(plink->poly[j] >= (unsigned int)coords_len)) {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"%s: index %d must be less than %d", "%s: index %d must be less than %d",