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
58PyObject * new_py_fmat(uint_t height, uint_t length);58PyObject * new_py_fmat(uint_t height, uint_t length);
59 59
60// defined in aubio-proxy.c60// defined in aubio-proxy.c
61extern int PyAubio_IsValidVector (PyObject *input);
62
61extern PyObject *PyAubio_CFvecToArray (fvec_t * self);63extern PyObject *PyAubio_CFvecToArray (fvec_t * self);
62extern int PyAubio_ArrayToCFvec (PyObject * self, fvec_t *out);64extern int PyAubio_ArrayToCFvec (PyObject * self, fvec_t *out);
63 65
python/ext/aubioproxy.c+10−3
@@ -20,7 +20,7 @@ PyAubio_CFvecToArray (fvec_t * self) · 19 unmodified lines
20}20}
21 21
22int22int
23PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) {23PyAubio_IsValidVector (PyObject * input) {
24 if (input == NULL) {24 if (input == NULL) {
25 PyErr_SetString (PyExc_ValueError, "input array is not a python object");25 PyErr_SetString (PyExc_ValueError, "input array is not a python object");
26 return 0;26 return 0;
@@ -46,8 +46,6 @@ PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) { · 19 unmodified lines
46 return 0;46 return 0;
47 }47 }
48 48
49 // vec = new_fvec (vec->length);
50 // no need to really allocate fvec, just its struct member
51 long length = PyArray_SIZE ((PyArrayObject *)input);49 long length = PyArray_SIZE ((PyArrayObject *)input);
52 if (length <= 0) {50 if (length <= 0) {
53 PyErr_SetString (PyExc_ValueError, "input array size should be greater than 0");51 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
61 PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input");59 PyErr_SetString (PyExc_ValueError, "can only accept vector of float as input");
62 return 0;60 return 0;
63 }61 }
62 return 1;
63}
64
65int
66PyAubio_ArrayToCFvec (PyObject *input, fvec_t *out) {
67
68 if (!PyAubio_IsValidVector(input)){
69 return 0;
70 }
64 71
65 out->length = (uint_t) PyArray_SIZE ((PyArrayObject *)input);72 out->length = (uint_t) PyArray_SIZE ((PyArrayObject *)input);
66 out->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)input, 0);73 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
154static int154static int
155Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure)155Py_cvec_set_norm (Py_cvec * vec, PyObject *input, void * closure)
156{156{
157 PyArrayObject * array;157 if (!PyAubio_IsValidVector(input)) {
158 if (input == NULL) {158 return 1;
159 PyErr_SetString (PyExc_ValueError, "input array is not a python object");
160 goto fail;
161 }159 }
162 if (PyArray_Check(input)) {160 long length = PyArray_SIZE ((PyArrayObject *)input);
163 // we got an array, convert it to a cvec.norm161 if (length != vec->length) {
164 if (PyArray_NDIM ((PyArrayObject *)input) == 0) {162 PyErr_Format (PyExc_ValueError,
165 PyErr_SetString (PyExc_ValueError, "input array is a scalar");163 "input array has length %ld, but cvec has length %d", length,
166 goto fail;164 vec->length);
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");
203 return 1;165 return 1;
204 }166 }
205 167
168 Py_XDECREF(vec->norm);
169 vec->norm = input;
170 Py_INCREF(vec->norm);
206 return 0;171 return 0;
207
208fail:
209 return 1;
210}172}
211 173
212static int174static int
213Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure)175Py_cvec_set_phas (Py_cvec * vec, PyObject *input, void * closure)
214{176{
215 PyArrayObject * array;177 if (!PyAubio_IsValidVector(input)) {
216 if (input == NULL) {178 return 1;
217 PyErr_SetString (PyExc_ValueError, "input array is not a python object");
218 goto fail;
219 }179 }
220 if (PyArray_Check(input)) {180 long length = PyArray_SIZE ((PyArrayObject *)input);
221 181 if (length != vec->length) {
222 // we got an array, convert it to a cvec.phas182 PyErr_Format (PyExc_ValueError,
223 if (PyArray_NDIM ((PyArrayObject *)input) == 0) {183 "input array has length %ld, but cvec has length %d", length,
224 PyErr_SetString (PyExc_ValueError, "input array is a scalar");184 vec->length);
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");
262 return 1;185 return 1;
263 }186 }
264 187
188 Py_XDECREF(vec->phas);
189 vec->phas = input;
190 Py_INCREF(vec->phas);
265 return 0;191 return 0;
266
267fail:
268 return 1;
269}192}
270 193
271static PyMemberDef Py_cvec_members[] = {194static 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