diff options
Diffstat (limited to 'source/blender/python/generic/py_capi_utils.c')
-rw-r--r-- | source/blender/python/generic/py_capi_utils.c | 73 |
1 files changed, 70 insertions, 3 deletions
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index f62fdaf09db..2876d7666f4 100644 --- a/source/blender/python/generic/py_capi_utils.c +++ b/source/blender/python/generic/py_capi_utils.c @@ -33,6 +33,8 @@ #include <Python.h> #include <frameobject.h> +#include "BLI_utildefines.h" /* for bool */ + #include "py_capi_utils.h" /* only for BLI_strncpy_wchar_from_utf8, should replace with py funcs but too late in release now */ @@ -44,7 +46,7 @@ /* array utility function */ int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length, - const PyTypeObject *type, const short is_double, const char *error_prefix) + const PyTypeObject *type, const bool is_double, const char *error_prefix) { PyObject *value_fast; Py_ssize_t value_len; @@ -112,6 +114,54 @@ int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length, return 0; } +/* array utility function */ +PyObject *PyC_FromArray(const void *array, int length, const PyTypeObject *type, + const bool is_double, const char *error_prefix) +{ + 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])); + } + } + } + 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])); + } + } + else { + Py_DECREF(tuple); + PyErr_Format(PyExc_TypeError, + "%s: internal error %s is invalid", + error_prefix, type->tp_name); + return NULL; + } + + return tuple; +} + /* for debugging */ void PyC_ObSpit(const char *name, PyObject *var) @@ -241,6 +291,23 @@ PyObject *PyC_Object_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...) return item; } +PyObject *PyC_FrozenSetFromStrings(const char **strings) +{ + const char **str; + PyObject *ret; + + ret = PyFrozenSet_New(NULL); + + for (str = strings; *str; str++) { + PyObject *py_str = PyUnicode_FromString(*str); + PySet_Add(ret, py_str); + Py_DECREF(py_str); + } + + return ret; +} + + /* similar to PyErr_Format(), * * implementation - we cant actually preprend the existing exception, @@ -684,7 +751,7 @@ void *PyC_RNA_AsPointer(PyObject *value, const char *type_name) PyObject *as_pointer; PyObject *pointer; - if (!strcmp(Py_TYPE(value)->tp_name, type_name) && + if (STREQ(Py_TYPE(value)->tp_name, type_name) && (as_pointer = PyObject_GetAttrString(value, "as_pointer")) != NULL && PyCallable_Check(as_pointer)) { @@ -737,7 +804,7 @@ char *PyC_FlagSet_AsString(PyC_FlagSet *item) int PyC_FlagSet_ValueFromID_int(PyC_FlagSet *item, const char *identifier, int *value) { for ( ; item->identifier; item++) { - if (strcmp(item->identifier, identifier) == 0) { + if (STREQ(item->identifier, identifier)) { *value = item->value; return 1; } |