diff options
author | Campbell Barton <ideasman42@gmail.com> | 2017-08-22 11:02:58 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2017-08-22 11:10:57 +0300 |
commit | 691ed21842397e52445fa8368fa81d757ba66123 (patch) | |
tree | 4270a707b23291e47135e6a65a65b0ac83670806 | |
parent | 049932c4c3b5b833df7e8b6be777d641d73a99f7 (diff) |
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.
-rw-r--r-- | source/blender/python/generic/py_capi_utils.c | 76 | ||||
-rw-r--r-- | source/blender/python/generic/py_capi_utils.h | 17 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_app.c | 3 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_app_alembic.c | 6 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_app_ffmpeg.c | 5 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_app_ocio.c | 7 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_app_oiio.c | 7 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_app_opensubdiv.c | 7 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_app_openvdb.c | 7 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_app_sdl.c | 6 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_props.c | 24 | ||||
-rw-r--r-- | source/blender/python/mathutils/mathutils_geometry.c | 2 |
12 files changed, 87 insertions, 80 deletions
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 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_openvdb.c b/source/blender/python/intern/bpy_app_openvdb.c index 8a24aaf0555..0b385206d7b 100644 --- a/source/blender/python/intern/bpy_app_openvdb.c +++ b/source/blender/python/intern/bpy_app_openvdb.c @@ -34,6 +34,8 @@ #include "bpy_app_openvdb.h" +#include "../generic/py_capi_utils.h" + #ifdef WITH_OPENVDB # include "openvdb_capi.h" #endif @@ -79,13 +81,12 @@ static PyObject *make_openvdb_info(void) #ifdef WITH_OPENVDB curversion = OpenVDB_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_sdl.c b/source/blender/python/intern/bpy_app_sdl.c index 76dab775953..816ad2833cc 100644 --- a/source/blender/python/intern/bpy_app_sdl.c +++ b/source/blender/python/intern/bpy_app_sdl.c @@ -29,6 +29,8 @@ #include "bpy_app_sdl.h" +#include "../generic/py_capi_utils.h" + #ifdef WITH_SDL /* SDL force defines __SSE__ and __SSE2__ flags, which generates warnings * because we pass those defines via command line as well. For until there's @@ -103,7 +105,7 @@ static PyObject *make_sdl_info(void) # endif # endif - SetObjItem(Py_BuildValue("(iii)", version.major, version.minor, version.patch)); + SetObjItem(PyC_Tuple_Pack_I32(version.major, version.minor, version.patch)); if (sdl_available) { SetObjItem(PyUnicode_FromFormat("%d.%d.%d", version.major, version.minor, version.patch)); } @@ -114,7 +116,7 @@ static PyObject *make_sdl_info(void) #else // WITH_SDL=OFF SetObjItem(PyBool_FromLong(0)); - SetObjItem(Py_BuildValue("(iii)", 0, 0, 0)); + SetObjItem(PyC_Tuple_Pack_I32(0, 0, 0)); SetStrItem("Unknown"); SetObjItem(PyBool_FromLong(0)); #endif diff --git a/source/blender/python/intern/bpy_props.c b/source/blender/python/intern/bpy_props.c index 611d70b8ed8..ade7bbe7452 100644 --- a/source/blender/python/intern/bpy_props.c +++ b/source/blender/python/intern/bpy_props.c @@ -530,12 +530,8 @@ static void bpy_prop_boolean_array_set_cb(struct PointerRNA *ptr, struct Propert self = pyrna_struct_as_instance(ptr); PyTuple_SET_ITEM(args, 0, self); - py_values = PyC_FromArray(values, len, &PyBool_Type, false, "BoolVectorProperty set"); - if (!py_values) { - printf_func_error(py_func); - } - else - PyTuple_SET_ITEM(args, 1, py_values); + py_values = PyC_Tuple_PackArray_I32FromBool(values, len); + PyTuple_SET_ITEM(args, 1, py_values); ret = PyObject_CallObject(py_func, args); @@ -764,12 +760,8 @@ static void bpy_prop_int_array_set_cb(struct PointerRNA *ptr, struct PropertyRNA self = pyrna_struct_as_instance(ptr); PyTuple_SET_ITEM(args, 0, self); - py_values = PyC_FromArray(values, len, &PyLong_Type, false, "IntVectorProperty set"); - if (!py_values) { - printf_func_error(py_func); - } - else - PyTuple_SET_ITEM(args, 1, py_values); + py_values = PyC_Tuple_PackArray_I32(values, len); + PyTuple_SET_ITEM(args, 1, py_values); ret = PyObject_CallObject(py_func, args); @@ -998,12 +990,8 @@ static void bpy_prop_float_array_set_cb(struct PointerRNA *ptr, struct PropertyR self = pyrna_struct_as_instance(ptr); PyTuple_SET_ITEM(args, 0, self); - py_values = PyC_FromArray(values, len, &PyFloat_Type, false, "FloatVectorProperty set"); - if (!py_values) { - printf_func_error(py_func); - } - else - PyTuple_SET_ITEM(args, 1, py_values); + py_values = PyC_Tuple_PackArray_F32(values, len); + PyTuple_SET_ITEM(args, 1, py_values); ret = PyObject_CallObject(py_func, args); diff --git a/source/blender/python/mathutils/mathutils_geometry.c b/source/blender/python/mathutils/mathutils_geometry.c index 868e4b38408..1dc18dbe509 100644 --- a/source/blender/python/mathutils/mathutils_geometry.c +++ b/source/blender/python/mathutils/mathutils_geometry.c @@ -1279,7 +1279,7 @@ static PyObject *M_Geometry_tessellate_polygon(PyObject *UNUSED(self), PyObject index = 0; dl_face = dl->index; while (index < dl->parts) { - PyList_SET_ITEM(tri_list, index, Py_BuildValue("iii", dl_face[0], dl_face[1], dl_face[2])); + PyList_SET_ITEM(tri_list, index, PyC_Tuple_Pack_I32(dl_face[0], dl_face[1], dl_face[2])); dl_face += 3; index++; } |