python/ext/aubioproxy.c: factorize input checks into PyAubio_IsValidVector
authorPaul Brossier <piem@piem.org>
Wed, 11 May 2016 02:54:06 +0000 (04:54 +0200)
committerPaul Brossier <piem@piem.org>
Wed, 11 May 2016 02:54:06 +0000 (04:54 +0200)
python/ext/aubio-types.h
python/ext/aubioproxy.c
python/ext/py-cvec.c

index 71ea0cf..dfd5aa3 100644 (file)
@@ -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
 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);
 
 extern PyObject *PyAubio_CFvecToArray (fvec_t * self);
 extern int PyAubio_ArrayToCFvec (PyObject * self, fvec_t *out);
 
index f3fd872..aa10c0a 100644 (file)
@@ -20,7 +20,7 @@ PyAubio_CFvecToArray (fvec_t * self)
 }
 
 int
 }
 
 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;
   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;
     }
 
       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");
     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;
   }
     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);
 
   out->length = (uint_t) PyArray_SIZE ((PyArrayObject *)input);
   out->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)input, 0);
index 2c433d0..9cfe898 100644 (file)
@@ -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)
 {
 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;
   }
 
     return 1;
   }
 
+  Py_XDECREF(vec->norm);
+  vec->norm = input;
+  Py_INCREF(vec->norm);
   return 0;
   return 0;
-
-fail:
-  return 1;
 }
 
 static int
 Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure)
 {
 }
 
 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;
   }
 
     return 1;
   }
 
+  Py_XDECREF(vec->phas);
+  vec->phas = input;
+  Py_INCREF(vec->phas);
   return 0;
   return 0;
-
-fail:
-  return 1;
 }
 
 static PyMemberDef Py_cvec_members[] = {
 }
 
 static PyMemberDef Py_cvec_members[] = {