From: Paul Brossier Date: Wed, 11 May 2016 02:54:06 +0000 (+0200) Subject: python/ext/aubioproxy.c: factorize input checks into PyAubio_IsValidVector X-Git-Tag: 0.4.4~300^2~78 X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=34d0c257cd6adea857415c783d9a3f1d9bf42ac3;p=aubio.git python/ext/aubioproxy.c: factorize input checks into PyAubio_IsValidVector --- diff --git a/python/ext/aubio-types.h b/python/ext/aubio-types.h index 71ea0cfb..dfd5aa35 100644 --- a/python/ext/aubio-types.h +++ b/python/ext/aubio-types.h @@ -58,6 +58,8 @@ PyObject * new_py_cvec(uint_t length); PyObject * new_py_fmat(uint_t height, uint_t length); // defined in aubio-proxy.c +extern int PyAubio_IsValidVector (PyObject *input); + extern PyObject *PyAubio_CFvecToArray (fvec_t * self); extern int PyAubio_ArrayToCFvec (PyObject * self, fvec_t *out); diff --git a/python/ext/aubioproxy.c b/python/ext/aubioproxy.c index f3fd8727..aa10c0a0 100644 --- a/python/ext/aubioproxy.c +++ b/python/ext/aubioproxy.c @@ -20,7 +20,7 @@ PyAubio_CFvecToArray (fvec_t * self) } int -PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) { +PyAubio_IsValidVector (PyObject * input) { if (input == NULL) { PyErr_SetString (PyExc_ValueError, "input array is not a python object"); return 0; @@ -46,8 +46,6 @@ PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) { return 0; } - // vec = new_fvec (vec->length); - // no need to really allocate fvec, just its struct member long length = PyArray_SIZE ((PyArrayObject *)input); if (length <= 0) { PyErr_SetString (PyExc_ValueError, "input array size should be greater than 0"); @@ -61,6 +59,15 @@ PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) { PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input"); return 0; } + return 1; +} + +int +PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) { + + if (!PyAubio_IsValidVector(input)){ + return 0; + } out->length = (uint_t) PyArray_SIZE ((PyArrayObject *)input); out->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)input, 0); diff --git a/python/ext/py-cvec.c b/python/ext/py-cvec.c index 2c433d0c..9cfe8982 100644 --- a/python/ext/py-cvec.c +++ b/python/ext/py-cvec.c @@ -154,118 +154,41 @@ Py_cvec_get_phas (Py_cvec * self, void *closure) static int Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure) { - PyArrayObject * array; - if (input == NULL) { - PyErr_SetString (PyExc_ValueError, "input array is not a python object"); - goto fail; + if (!PyAubio_IsValidVector(input)) { + return 1; } - if (PyArray_Check(input)) { - // we got an array, convert it to a cvec.norm - if (PyArray_NDIM ((PyArrayObject *)input) == 0) { - PyErr_SetString (PyExc_ValueError, "input array is a scalar"); - goto fail; - } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { - PyErr_SetString (PyExc_ValueError, - "input array has more than two dimensions"); - goto fail; - } - - if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { - PyErr_SetString (PyExc_ValueError, "input array should be float"); - goto fail; - } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { - PyErr_SetString (PyExc_ValueError, "input array should be float32"); - goto fail; - } - array = (PyArrayObject *)input; - - // check input array dimensions - if (PyArray_NDIM (array) != 1) { - PyErr_Format (PyExc_ValueError, - "input array has %d dimensions, not 1", - PyArray_NDIM (array)); - goto fail; - } else { - if (vec->length != PyArray_SIZE (array)) { - PyErr_Format (PyExc_ValueError, - "input array has length %d, but cvec has length %d", - (int)PyArray_SIZE (array), vec->length); - goto fail; - } - } - - Py_XDECREF(vec->norm); - vec->norm = input; - Py_INCREF(vec->norm); - - } else { - PyErr_SetString (PyExc_ValueError, "can only accept array as input"); + long length = PyArray_SIZE ((PyArrayObject *)input); + if (length != vec->length) { + PyErr_Format (PyExc_ValueError, + "input array has length %ld, but cvec has length %d", length, + vec->length); return 1; } + Py_XDECREF(vec->norm); + vec->norm = input; + Py_INCREF(vec->norm); return 0; - -fail: - return 1; } static int Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure) { - PyArrayObject * array; - if (input == NULL) { - PyErr_SetString (PyExc_ValueError, "input array is not a python object"); - goto fail; + if (!PyAubio_IsValidVector(input)) { + return 1; } - if (PyArray_Check(input)) { - - // we got an array, convert it to a cvec.phas - if (PyArray_NDIM ((PyArrayObject *)input) == 0) { - PyErr_SetString (PyExc_ValueError, "input array is a scalar"); - goto fail; - } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) { - PyErr_SetString (PyExc_ValueError, - "input array has more than two dimensions"); - goto fail; - } - - if (!PyArray_ISFLOAT ((PyArrayObject *)input)) { - PyErr_SetString (PyExc_ValueError, "input array should be float"); - goto fail; - } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) { - PyErr_SetString (PyExc_ValueError, "input array should be float32"); - goto fail; - } - array = (PyArrayObject *)input; - - // check input array dimensions - if (PyArray_NDIM (array) != 1) { - PyErr_Format (PyExc_ValueError, - "input array has %d dimensions, not 1", - PyArray_NDIM (array)); - goto fail; - } else { - if (vec->length != PyArray_SIZE (array)) { - PyErr_Format (PyExc_ValueError, - "input array has length %d, but cvec has length %d", - (int)PyArray_SIZE (array), vec->length); - goto fail; - } - } - - Py_XDECREF(vec->phas); - vec->phas = input; - Py_INCREF(vec->phas); - - } else { - PyErr_SetString (PyExc_ValueError, "can only accept array as input"); + long length = PyArray_SIZE ((PyArrayObject *)input); + if (length != vec->length) { + PyErr_Format (PyExc_ValueError, + "input array has length %ld, but cvec has length %d", length, + vec->length); return 1; } + Py_XDECREF(vec->phas); + vec->phas = input; + Py_INCREF(vec->phas); return 0; - -fail: - return 1; } static PyMemberDef Py_cvec_members[] = {