[py] fix compilation warning in py-source
[aubio.git] / python / ext / py-source.c
index c263473..7e9d48b 100644 (file)
@@ -18,7 +18,7 @@ typedef struct
 static char Py_source_doc[] = ""
 "source(path, samplerate=0, hop_size=512, channels=0)\n"
 "\n"
-"Create a new source, opening the given pathname for reading.\n"
+"Read audio samples from a media file.\n"
 "\n"
 "`source` open the file specified in `path` and creates a callable\n"
 "returning `hop_size` new audio samples at each invocation.\n"
@@ -155,7 +155,7 @@ static char Py_source_doc[] = ""
 "...     for samples in source:\n"
 "...         n_frames += len(samples)\n"
 "...     print('read', n_frames, 'samples in', samples.shape[0], 'channels',\n"
-"...         'from file \"\%s\"' \% source.uri)\n"
+"...         'from file \"%%s\"' %% source.uri)\n"
 "...\n"
 "read 239334 samples in 2 channels from file \"audiotrack.wav\"\n"
 "\n"
@@ -247,7 +247,7 @@ static char Py_source_do_doc[] = ""
 "\n"
 "Returns\n"
 "-------\n"
-"samples : numpy.ndarray, shape `(hop_size,)`, dtype aubio.float_type\n"
+"samples : numpy.ndarray\n"
 "    `fvec` of size `hop_size` containing the new samples.\n"
 "read : int\n"
 "    Number of samples read from the source, equals to `hop_size`\n"
@@ -283,7 +283,7 @@ static char Py_source_do_multi_doc[] = ""
 "\n"
 "Returns\n"
 "-------\n"
-"samples : np.ndarray([hop_size, channels], dtype=aubio.float_type)\n"
+"samples : numpy.ndarray\n"
 "    NumPy array of shape `(hop_size, channels)` containing the new\n"
 "    audio samples.\n"
 "read : int\n"
@@ -436,6 +436,10 @@ Py_source_do(Py_source * self, PyObject * args)
   /* compute _do function */
   aubio_source_do (self->o, &(self->c_read_to), &read);
 
+  if (PyErr_Occurred() != NULL) {
+    return NULL;
+  }
+
   outputs = PyTuple_New(2);
   PyTuple_SetItem( outputs, 0, self->read_to );
   PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
@@ -457,6 +461,10 @@ Py_source_do_multi(Py_source * self, PyObject * args)
   /* compute _do function */
   aubio_source_do_multi (self->o, &(self->c_mread_to), &read);
 
+  if (PyErr_Occurred() != NULL) {
+    return NULL;
+  }
+
   outputs = PyTuple_New(2);
   PyTuple_SetItem( outputs, 0, self->mread_to);
   PyTuple_SetItem( outputs, 1, (PyObject *)PyLong_FromLong(read));
@@ -573,7 +581,10 @@ static PyObject* Pyaubio_source_iter_next(Py_source *self) {
       return vec;
     } else if (PyLong_AsLong(size) > 0) {
       // short read, return a shorter array
-      PyArrayObject *shortread = (PyArrayObject*)PyTuple_GetItem(done, 0);
+      PyObject *vec = PyTuple_GetItem(done, 0);
+      // take a copy to prevent resizing internal arrays
+      PyArrayObject *shortread = (PyArrayObject*)PyArray_FROM_OTF(vec,
+          NPY_NOTYPE, NPY_ARRAY_ENSURECOPY);
       PyArray_Dims newdims;
       PyObject *reshaped;
       newdims.len = PyArray_NDIM(shortread);
@@ -586,6 +597,7 @@ static PyObject* Pyaubio_source_iter_next(Py_source *self) {
       }
       reshaped = PyArray_Newshape(shortread, &newdims, NPY_CORDER);
       Py_DECREF(shortread);
+      Py_DECREF(vec);
       return reshaped;
     } else {
       PyErr_SetNone(PyExc_StopIteration);