ref: 34d0c257cd6adea857415c783d9a3f1d9bf42ac3
parent: ece990f15b62b331460b649b9e96d3d0b96a14b9
author: Paul Brossier <piem@piem.org>
date: Wed May 11 00:54:06 EDT 2016
python/ext/aubioproxy.c: factorize input checks into PyAubio_IsValidVector
--- a/python/ext/aubio-types.h
+++ b/python/ext/aubio-types.h
@@ -58,6 +58,8 @@
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);
--- a/python/ext/aubioproxy.c
+++ b/python/ext/aubioproxy.c
@@ -20,7 +20,7 @@
}
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 @@
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");
@@ -59,6 +57,15 @@
return 0;
} else {
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;
}
--- a/python/ext/py-cvec.c
+++ b/python/ext/py-cvec.c
@@ -154,118 +154,41 @@
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[] = {