[Bf-blender-cvs] [691ed218423] master: PyAPI: replace PyC_FromArray with typed functions

Campbell Barton noreply at git.blender.org
Tue Aug 22 10:07:30 CEST 2017


Commit: 691ed21842397e52445fa8368fa81d757ba66123
Author: Campbell Barton
Date:   Tue Aug 22 18:02:58 2017 +1000
Branches: master
https://developer.blender.org/rB691ed21842397e52445fa8368fa81d757ba66123

PyAPI: replace PyC_FromArray with typed functions

This was meant to be generic but introduced possible type errors
and unnecessary complication.
Replace with typed PyC_Tuple_PackArray_* functions.

Also add PyC_Tuple_Pack_* macro which replaces some uses of
Py_BuildValue, with the advantage of not having to parse a string.

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

M	source/blender/python/generic/py_capi_utils.c
M	source/blender/python/generic/py_capi_utils.h
M	source/blender/python/intern/bpy_app.c
M	source/blender/python/intern/bpy_app_alembic.c
M	source/blender/python/intern/bpy_app_ffmpeg.c
M	source/blender/python/intern/bpy_app_ocio.c
M	source/blender/python/intern/bpy_app_oiio.c
M	source/blender/python/intern/bpy_app_opensubdiv.c
M	source/blender/python/intern/bpy_app_openvdb.c
M	source/blender/python/intern/bpy_app_sdl.c
M	source/blender/python/intern/bpy_props.c
M	source/blender/python/mathutils/mathutils_geometry.c

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

diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c
index abc2da9e4c7..d49f9514b8c 100644
--- a/source/blender/python/generic/py_capi_utils.c
+++ b/source/blender/python/generic/py_capi_utils.c
@@ -127,54 +127,52 @@ int PyC_AsArray(
 	return ret;
 }
 
+/* -------------------------------------------------------------------- */
+/** \name Typed Tuple Packing
+ *
+ * \note See #PyC_Tuple_Pack_* macros that take multiple arguments.
+ *
+ * \{ */
+
 /* array utility function */
-PyObject *PyC_FromArray(const void *array, int length, const PyTypeObject *type,
-                        const bool is_double, const char *error_prefix)
+PyObject *PyC_Tuple_PackArray_F32(const float *array, uint len)
 {
-	PyObject *tuple;
-	int i;
-
-	tuple = PyTuple_New(length);
-
-	/* for each type */
-	if (type == &PyFloat_Type) {
-		if (is_double) {
-			const double *array_double = array;
-			for (i = 0; i < length; ++i) {
-				PyTuple_SET_ITEM(tuple, i, PyFloat_FromDouble(array_double[i]));
-			}
-		}
-		else {
-			const float *array_float = array;
-			for (i = 0; i < length; ++i) {
-				PyTuple_SET_ITEM(tuple, i, PyFloat_FromDouble(array_float[i]));
-			}
-		}
+	PyObject *tuple = PyTuple_New(len);
+	for (uint i = 0; i < len; i++) {
+		PyTuple_SET_ITEM(tuple, i, PyFloat_FromDouble(array[i]));
 	}
-	else if (type == &PyLong_Type) {
-		/* could use is_double for 'long int' but no use now */
-		const int *array_int = array;
-		for (i = 0; i < length; ++i) {
-			PyTuple_SET_ITEM(tuple, i, PyLong_FromLong(array_int[i]));
-		}
-	}
-	else if (type == &PyBool_Type) {
-		const int *array_bool = array;
-		for (i = 0; i < length; ++i) {
-			PyTuple_SET_ITEM(tuple, i, PyBool_FromLong(array_bool[i]));
-		}
+	return tuple;
+}
+
+PyObject *PyC_Tuple_PackArray_I32(const int *array, uint len)
+{
+	PyObject *tuple = PyTuple_New(len);
+	for (uint i = 0; i < len; i++) {
+		PyTuple_SET_ITEM(tuple, i, PyLong_FromLong(array[i]));
 	}
-	else {
-		Py_DECREF(tuple);
-		PyErr_Format(PyExc_TypeError,
-		             "%s: internal error %s is invalid",
-		             error_prefix, type->tp_name);
-		return NULL;
+	return tuple;
+}
+
+PyObject *PyC_Tuple_PackArray_I32FromBool(const int *array, uint len)
+{
+	PyObject *tuple = PyTuple_New(len);
+	for (uint i = 0; i < len; i++) {
+		PyTuple_SET_ITEM(tuple, i, PyBool_FromLong(array[i]));
 	}
+	return tuple;
+}
 
+PyObject *PyC_Tuple_PackArray_Bool(const bool *array, uint len)
+{
+	PyObject *tuple = PyTuple_New(len);
+	for (uint i = 0; i < len; i++) {
+		PyTuple_SET_ITEM(tuple, i, PyBool_FromLong(array[i]));
+	}
 	return tuple;
 }
 
+/** \} */
+
 /**
  * Caller needs to ensure tuple is uninitialized.
  * Handy for filling a tuple with None for eg.
diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h
index fb16fd5a314..9f500f4c76b 100644
--- a/source/blender/python/generic/py_capi_utils.h
+++ b/source/blender/python/generic/py_capi_utils.h
@@ -45,8 +45,21 @@ int             PyC_AsArray_FAST(
 int             PyC_AsArray(
         void *array, PyObject *value, const Py_ssize_t length,
         const PyTypeObject *type, const bool is_double, const char *error_prefix);
-PyObject *      PyC_FromArray(const void *array, int length, const PyTypeObject *type,
-                              const bool is_double, const char *error_prefix);
+
+PyObject       *PyC_Tuple_PackArray_F32(const float *array, uint len);
+PyObject       *PyC_Tuple_PackArray_I32(const int *array, uint len);
+PyObject       *PyC_Tuple_PackArray_I32FromBool(const int *array, uint len);
+PyObject       *PyC_Tuple_PackArray_Bool(const bool *array, uint len);
+
+#define PyC_Tuple_Pack_F32(...) \
+	PyC_Tuple_PackArray_F32(((const float []){__VA_ARGS__}), (sizeof((const float []){__VA_ARGS__}) / sizeof(float)))
+#define PyC_Tuple_Pack_I32(...) \
+	PyC_Tuple_PackArray_I32(((const int []){__VA_ARGS__}), (sizeof((const int []){__VA_ARGS__}) / sizeof(int)))
+#define PyC_Tuple_Pack_I32FromBool(...) \
+	PyC_Tuple_PackArray_I32FromBool(((const int []){__VA_ARGS__}), (sizeof((const int []){__VA_ARGS__}) / sizeof(int)))
+#define PyC_Tuple_Pack_Bool(...) \
+	PyC_Tuple_PackArray_Bool(((const bool []){__VA_ARGS__}), (sizeof((const bool []){__VA_ARGS__}) / sizeof(bool)))
+
 void            PyC_Tuple_Fill(PyObject *tuple, PyObject *value);
 void            PyC_List_Fill(PyObject *list, PyObject *value);
 
diff --git a/source/blender/python/intern/bpy_app.c b/source/blender/python/intern/bpy_app.c
index 3693e2d22a0..f44401afd7d 100644
--- a/source/blender/python/intern/bpy_app.c
+++ b/source/blender/python/intern/bpy_app.c
@@ -157,8 +157,7 @@ static PyObject *make_app_info(void)
 #define SetObjItem(obj) \
 	PyStructSequence_SET_ITEM(app_info, pos++, obj)
 
-	SetObjItem(Py_BuildValue("(iii)",
-	                         BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION));
+	SetObjItem(PyC_Tuple_Pack_I32(BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION));
 	SetObjItem(PyUnicode_FromFormat("%d.%02d (sub %d)",
 	                                BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION));
 
diff --git a/source/blender/python/intern/bpy_app_alembic.c b/source/blender/python/intern/bpy_app_alembic.c
index 90e6a02b418..2a1a031a629 100644
--- a/source/blender/python/intern/bpy_app_alembic.c
+++ b/source/blender/python/intern/bpy_app_alembic.c
@@ -34,6 +34,8 @@
 
 #include "bpy_app_alembic.h"
 
+#include "../generic/py_capi_utils.h"
+
 #ifdef WITH_ALEMBIC
 #  include "ABC_alembic.h"
 #endif
@@ -79,11 +81,11 @@ static PyObject *make_alembic_info(void)
 	const int patch = curversion - ((curversion / 100 ) * 100);
 
 	SetObjItem(PyBool_FromLong(1));
-	SetObjItem(Py_BuildValue("(iii)", major, minor, patch));
+	SetObjItem(PyC_Tuple_Pack_I32(major, minor, patch));
 	SetObjItem(PyUnicode_FromFormat("%2d, %2d, %2d", major, minor, patch));
 #else
 	SetObjItem(PyBool_FromLong(0));
-	SetObjItem(Py_BuildValue("(iii)", 0, 0, 0));
+	SetObjItem(PyC_Tuple_Pack_I32(0, 0, 0));
 	SetStrItem("Unknown");
 #endif
 
diff --git a/source/blender/python/intern/bpy_app_ffmpeg.c b/source/blender/python/intern/bpy_app_ffmpeg.c
index fd516e4547f..9f8355db72b 100644
--- a/source/blender/python/intern/bpy_app_ffmpeg.c
+++ b/source/blender/python/intern/bpy_app_ffmpeg.c
@@ -29,6 +29,8 @@
 
 #include "bpy_app_ffmpeg.h"
 
+#include "../generic/py_capi_utils.h"
+
 #ifdef WITH_FFMPEG
 #include <libavcodec/avcodec.h>
 #include <libavdevice/avdevice.h>
@@ -91,8 +93,7 @@ static PyObject *make_ffmpeg_info(void)
 #ifdef WITH_FFMPEG
 #  define FFMPEG_LIB_VERSION(lib) { \
 		curversion = lib ## _version(); \
-		SetObjItem(Py_BuildValue("(iii)", \
-		                         curversion >> 16, (curversion >> 8) % 256, curversion % 256)); \
+		SetObjItem(PyC_Tuple_Pack_I32(curversion >> 16, (curversion >> 8) % 256, curversion % 256)); \
 		SetObjItem(PyUnicode_FromFormat("%2d, %2d, %2d", \
 		                                curversion >> 16, (curversion >> 8) % 256, curversion % 256)); \
 } (void)0
diff --git a/source/blender/python/intern/bpy_app_ocio.c b/source/blender/python/intern/bpy_app_ocio.c
index 02e4044219a..9997e6b87f1 100644
--- a/source/blender/python/intern/bpy_app_ocio.c
+++ b/source/blender/python/intern/bpy_app_ocio.c
@@ -29,6 +29,8 @@
 
 #include "bpy_app_ocio.h"
 
+#include "../generic/py_capi_utils.h"
+
 #ifdef WITH_OCIO
 #  include "ocio_capi.h"
 #endif
@@ -74,13 +76,12 @@ static PyObject *make_ocio_info(void)
 #ifdef WITH_OCIO
 	curversion = OCIO_getVersionHex();
 	SetObjItem(PyBool_FromLong(1));
-	SetObjItem(Py_BuildValue("(iii)",
-	                         curversion >> 24, (curversion >> 16) % 256, (curversion >> 8) % 256));
+	SetObjItem(PyC_Tuple_Pack_I32(curversion >> 24, (curversion >> 16) % 256, (curversion >> 8) % 256));
 	SetObjItem(PyUnicode_FromFormat("%2d, %2d, %2d",
 	                                curversion >> 24, (curversion >> 16) % 256, (curversion >> 8) % 256));
 #else
 	SetObjItem(PyBool_FromLong(0));
-	SetObjItem(Py_BuildValue("(iii)", 0, 0, 0));
+	SetObjItem(PyC_Tuple_Pack_I32(0, 0, 0));
 	SetStrItem("Unknown");
 #endif
 
diff --git a/source/blender/python/intern/bpy_app_oiio.c b/source/blender/python/intern/bpy_app_oiio.c
index 60daf3ddd8b..e14b48ff7cf 100644
--- a/source/blender/python/intern/bpy_app_oiio.c
+++ b/source/blender/python/intern/bpy_app_oiio.c
@@ -29,6 +29,8 @@
 
 #include "bpy_app_oiio.h"
 
+#include "../generic/py_capi_utils.h"
+
 #ifdef WITH_OPENIMAGEIO
 #  include "openimageio_api.h"
 #endif
@@ -74,13 +76,12 @@ static PyObject *make_oiio_info(void)
 #ifdef WITH_OPENIMAGEIO
 	curversion = OIIO_getVersionHex();
 	SetObjItem(PyBool_FromLong(1));
-	SetObjItem(Py_BuildValue("(iii)",
-	                         curversion / 10000, (curversion / 100) % 100, curversion % 100));
+	SetObjItem(PyC_Tuple_Pack_I32(curversion / 10000, (curversion / 100) % 100, curversion % 100));
 	SetObjItem(PyUnicode_FromFormat("%2d, %2d, %2d",
 	                                curversion / 10000, (curversion / 100) % 100, curversion % 100));
 #else
 	SetObjItem(PyBool_FromLong(0));
-	SetObjItem(Py_BuildValue("(iii)", 0, 0, 0));
+	SetObjItem(PyC_Tuple_Pack_I32(0, 0, 0));
 	SetStrItem("Unknown");
 #endif
 
diff --git a/source/blender/python/intern/bpy_app_opensubdiv.c b/source/blender/python/intern/bpy_app_opensubdiv.c
index 7f269baf2b0..096374794c9 100644
--- a/source/blender/python/intern/bpy_app_opensubdiv.c
+++ b/source/blender/python/intern/bpy_app_opensubdiv.c
@@ -29,6 +29,8 @@
 
 #include "bpy_app_opensubdiv.h"
 
+#include "../generic/py_capi_utils.h"
+
 #ifdef WITH_OPENSUBDIV
 #  include "opensubdiv_capi.h"
 #endif
@@ -70,13 +72,12 @@ static PyObject *make_opensubdiv_info(void)
 #ifdef WITH_OPENSUBDIV
 	int curversion = openSubdiv_getVersionHex();
 	SetObjItem(PyBool_FromLong(1));
-	SetObjItem(Py_BuildValue("(iii)",
-	                         curversion / 10000, (curversion / 100) % 100, curversion % 100));
+	SetObjItem(PyC_Tuple_Pack_I32(curversion / 10000, (curversion / 100) % 100, curversion % 100));
 	SetObjItem(PyUnicode_FromFormat("%2d, %2d, %2d",
 	                                curversion / 10000, (curversion / 100) % 100, curversion % 100));
 #else
 	SetO

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list