[Bf-blender-cvs] [46cf33bf014] master: PyAPI: Make use of PyC_LongAs... API

Campbell Barton noreply at git.blender.org
Sun Aug 20 07:45:37 CEST 2017


Commit: 46cf33bf0146946ed05756a9b8bfa1318538f020
Author: Campbell Barton
Date:   Sun Aug 20 15:44:54 2017 +1000
Branches: master
https://developer.blender.org/rB46cf33bf0146946ed05756a9b8bfa1318538f020

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.

===================================================================

M	source/blender/python/bmesh/bmesh_py_ops_call.c
M	source/blender/python/bmesh/bmesh_py_types.c
M	source/blender/python/bmesh/bmesh_py_types_customdata.c
M	source/blender/python/bmesh/bmesh_py_types_meshdata.c
M	source/blender/python/generic/idprop_py_api.c
M	source/blender/python/intern/bpy_app.c
M	source/blender/python/intern/bpy_props.c
M	source/blender/python/intern/bpy_rna.c
M	source/blender/python/intern/bpy_rna_array.c
M	source/blender/python/mathutils/mathutils_Vector.c
M	source/blender/python/mathutils/mathutils_bvhtree.c

===================================================================

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;
 	}
 }
diff --git a/source/blender/python/generic/idprop_py_api.c b/source/blender/python/generic/idprop_py_api.c
index 5d6a7c578a2..1153e0176df 100644
--- a/source/blender/python/generic/idprop_py_api.c
+++ b/source/blender/python/generic/idprop_py_api.c
@@ -386,7 +386,7 @@ static IDProperty *idp_from_PyFloat(const char *name, PyObject 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list