History

python/ext/aubioproxy.c: factorize input checks into PyAubio_IsValidVector

Paul Brossier committed 10 years ago

Commit 34d0c251 parent ece990f3 files · +32 −100Browse files

Showing the changes to python (3 of 3 files). Show all

Unified · Split

3 files
  1. python/ext/aubio-types.h +2 −0
  2. python/ext/aubioproxy.c +10 −3
  3. python/ext/py-cvec.c +20 −97
python/ext/aubio-types.h+2−0
@@ -58,6 +58,8 @@ PyObject * new_py_cvec(uint_t length); · 57 unmodified lines
5858 PyObject * new_py_fmat(uint_t height, uint_t length);
5959
6060 // defined in aubio-proxy.c
61+extern int PyAubio_IsValidVector (PyObject *input);
62+
6163 extern PyObject *PyAubio_CFvecToArray (fvec_t * self);
6264 extern int PyAubio_ArrayToCFvec (PyObject * self, fvec_t *out);
6365
python/ext/aubioproxy.c+10−3
@@ -20,7 +20,7 @@ PyAubio_CFvecToArray (fvec_t * self) · 19 unmodified lines
2020 }
2121
2222 int
23−PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) {
23+PyAubio_IsValidVector (PyObject * input) {
2424 if (input == NULL) {
2525 PyErr_SetString (PyExc_ValueError, "input array is not a python object");
2626 return 0;
@@ -46,8 +46,6 @@ PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) { · 19 unmodified lines
4646 return 0;
4747 }
4848
49− // vec = new_fvec (vec->length);
50− // no need to really allocate fvec, just its struct member
5149 long length = PyArray_SIZE ((PyArrayObject *)input);
5250 if (length <= 0) {
5351 PyErr_SetString (PyExc_ValueError, "input array size should be greater than 0");
@@ -61,6 +59,15 @@ PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) { · 7 unmodified lines
6159 PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input");
6260 return 0;
6361 }
62+ return 1;
63+}
64+
65+int
66+PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) {
67+
68+ if (!PyAubio_IsValidVector(input)){
69+ return 0;
70+ }
6471
6572 out->length = (uint_t) PyArray_SIZE ((PyArrayObject *)input);
6673 out->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)input, 0);
python/ext/py-cvec.c+20−97
@@ -154,118 +154,41 @@ Py_cvec_get_phas (Py_cvec * self, void *closure) · 153 unmodified lines
154154 static int
155155 Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure)
156156 {
157− PyArrayObject * array;
158− if (input == NULL) {
159− PyErr_SetString (PyExc_ValueError, "input array is not a python object");
160− goto fail;
157+ if (!PyAubio_IsValidVector(input)) {
158+ return 1;
161159 }
162− if (PyArray_Check(input)) {
163− // we got an array, convert it to a cvec.norm
164− if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
165− PyErr_SetString (PyExc_ValueError, "input array is a scalar");
166− goto fail;
167− } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
168− PyErr_SetString (PyExc_ValueError,
169− "input array has more than two dimensions");
170− goto fail;
171− }
172−
173− if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
174− PyErr_SetString (PyExc_ValueError, "input array should be float");
175− goto fail;
176− } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
177− PyErr_SetString (PyExc_ValueError, "input array should be float32");
178− goto fail;
179− }
180− array = (PyArrayObject *)input;
181−
182− // check input array dimensions
183− if (PyArray_NDIM (array) != 1) {
184− PyErr_Format (PyExc_ValueError,
185− "input array has %d dimensions, not 1",
186− PyArray_NDIM (array));
187− goto fail;
188− } else {
189− if (vec->length != PyArray_SIZE (array)) {
190− PyErr_Format (PyExc_ValueError,
191− "input array has length %d, but cvec has length %d",
192− (int)PyArray_SIZE (array), vec->length);
193− goto fail;
194− }
195− }
196−
197− Py_XDECREF(vec->norm);
198− vec->norm = input;
199− Py_INCREF(vec->norm);
200−
201− } else {
202− PyErr_SetString (PyExc_ValueError, "can only accept array as input");
160+ long length = PyArray_SIZE ((PyArrayObject *)input);
161+ if (length != vec->length) {
162+ PyErr_Format (PyExc_ValueError,
163+ "input array has length %ld, but cvec has length %d", length,
164+ vec->length);
203165 return 1;
204166 }
205167
168+ Py_XDECREF(vec->norm);
169+ vec->norm = input;
170+ Py_INCREF(vec->norm);
206171 return 0;
207−
208−fail:
209− return 1;
210172 }
211173
212174 static int
213175 Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure)
214176 {
215− PyArrayObject * array;
216− if (input == NULL) {
217− PyErr_SetString (PyExc_ValueError, "input array is not a python object");
218− goto fail;
177+ if (!PyAubio_IsValidVector(input)) {
178+ return 1;
219179 }
220− if (PyArray_Check(input)) {
221−
222− // we got an array, convert it to a cvec.phas
223− if (PyArray_NDIM ((PyArrayObject *)input) == 0) {
224− PyErr_SetString (PyExc_ValueError, "input array is a scalar");
225− goto fail;
226− } else if (PyArray_NDIM ((PyArrayObject *)input) > 2) {
227− PyErr_SetString (PyExc_ValueError,
228− "input array has more than two dimensions");
229− goto fail;
230− }
231−
232− if (!PyArray_ISFLOAT ((PyArrayObject *)input)) {
233− PyErr_SetString (PyExc_ValueError, "input array should be float");
234− goto fail;
235− } else if (PyArray_TYPE ((PyArrayObject *)input) != AUBIO_NPY_SMPL) {
236− PyErr_SetString (PyExc_ValueError, "input array should be float32");
237− goto fail;
238− }
239− array = (PyArrayObject *)input;
240−
241− // check input array dimensions
242− if (PyArray_NDIM (array) != 1) {
243− PyErr_Format (PyExc_ValueError,
244− "input array has %d dimensions, not 1",
245− PyArray_NDIM (array));
246− goto fail;
247− } else {
248− if (vec->length != PyArray_SIZE (array)) {
249− PyErr_Format (PyExc_ValueError,
250− "input array has length %d, but cvec has length %d",
251− (int)PyArray_SIZE (array), vec->length);
252− goto fail;
253− }
254− }
255−
256− Py_XDECREF(vec->phas);
257− vec->phas = input;
258− Py_INCREF(vec->phas);
259−
260− } else {
261− PyErr_SetString (PyExc_ValueError, "can only accept array as input");
180+ long length = PyArray_SIZE ((PyArrayObject *)input);
181+ if (length != vec->length) {
182+ PyErr_Format (PyExc_ValueError,
183+ "input array has length %ld, but cvec has length %d", length,
184+ vec->length);
262185 return 1;
263186 }
264187
188+ Py_XDECREF(vec->phas);
189+ vec->phas = input;
190+ Py_INCREF(vec->phas);
265191 return 0;
266−
267−fail:
268− return 1;
269192 }
270193
271194 static PyMemberDef Py_cvec_members[] = {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t