diff --git a/alsaaudio.c b/alsaaudio.c index 75e628b..c0051ae 100644 --- a/alsaaudio.c +++ b/alsaaudio.c @@ -1051,6 +1051,7 @@ alsapcm_read(alsapcm_t *self, PyObject *args) { int res; int size = self->framesize * self->periodsize; + int sizeout; PyObject *buffer_obj, *tuple_obj, *res_obj; char *buffer; @@ -1106,16 +1107,20 @@ alsapcm_read(alsapcm_t *self, PyObject *args) } if (res <= 0) { + sizeout = 0; + } else { + sizeout = res * self->framesize; + } + #if PY_MAJOR_VERSION < 3 /* If the following fails, it will free the object */ - if (_PyString_Resize(&buffer_obj, 0)) + if (_PyString_Resize(&buffer_obj, sizeout)) return NULL; #else /* If the following fails, it will free the object */ - if (_PyBytes_Resize(&buffer_obj, 0)) + if (_PyBytes_Resize(&buffer_obj, sizeout)) return NULL; #endif - } res_obj = PyLong_FromLong(res); if (!res_obj) {