diff -r ff2dac607b41 Modules/ossaudiodev.c --- a/Modules/ossaudiodev.c Fri Mar 20 00:27:28 2015 +0100 +++ b/Modules/ossaudiodev.c Fri Mar 20 00:29:44 2015 +0100 @@ -48,12 +48,12 @@ typedef unsigned long uint32_t; typedef struct { PyObject_HEAD - char *devicename; /* name of the device file */ - int fd; /* file descriptor */ - int mode; /* file mode (O_RDONLY, etc.) */ - int icount; /* input count */ - int ocount; /* output count */ - uint32_t afmts; /* audio formats supported by hardware */ + char *devicename; /* name of the device file */ + int fd; /* file descriptor */ + int mode; /* file mode (O_RDONLY, etc.) */ + Py_ssize_t icount; /* input count */ + Py_ssize_t ocount; /* output count */ + uint32_t afmts; /* audio formats supported by hardware */ } oss_audio_t; typedef struct { @@ -399,15 +399,14 @@ oss_post(oss_audio_t *self, PyObject *ar static PyObject * oss_read(oss_audio_t *self, PyObject *args) { - int size, count; + Py_ssize_t size, count; PyObject *rv; if (!_is_fd_valid(self->fd)) return NULL; - if (!PyArg_ParseTuple(args, "i:read", &size)) + if (!PyArg_ParseTuple(args, "n:read", &size)) return NULL; - rv = PyBytes_FromStringAndSize(NULL, size); if (rv == NULL) return NULL; @@ -426,17 +425,17 @@ oss_read(oss_audio_t *self, PyObject *ar static PyObject * oss_write(oss_audio_t *self, PyObject *args) { - char *cp; - int rv, size; + Py_buffer buf; + Py_ssize_t rv; if (!_is_fd_valid(self->fd)) return NULL; - if (!PyArg_ParseTuple(args, "y#:write", &cp, &size)) { + if (!PyArg_ParseTuple(args, "y*:write", &buf)) return NULL; - } - rv = _Py_write(self->fd, cp, size); + rv = _Py_write(self->fd, buf.buf, buf.len); + PyBuffer_Release(&buf); if (rv == -1) return NULL; @@ -447,10 +446,12 @@ oss_write(oss_audio_t *self, PyObject *a static PyObject * oss_writeall(oss_audio_t *self, PyObject *args) { + Py_buffer buf; char *cp; - int rv, size; + Py_ssize_t size, rv; fd_set write_set_fds; int select_rv; + PyObject *res = NULL; /* NB. writeall() is only useful in non-blocking mode: according to Guenter Geiger on the linux-audio-dev list @@ -462,13 +463,15 @@ oss_writeall(oss_audio_t *self, PyObject if (!_is_fd_valid(self->fd)) return NULL; - if (!PyArg_ParseTuple(args, "y#:write", &cp, &size)) + if (!PyArg_ParseTuple(args, "y*:write", &buf)) return NULL; + cp = buf.buf; + size = buf.len; if (!_PyIsSelectable_fd(self->fd)) { PyErr_SetString(PyExc_ValueError, "file descriptor out of range for select"); - return NULL; + goto finally; } /* use select to wait for audio device to be available */ FD_ZERO(&write_set_fds); @@ -479,9 +482,11 @@ oss_writeall(oss_audio_t *self, PyObject select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL); Py_END_ALLOW_THREADS - assert(select_rv != 0); /* no timeout, can't expire */ - if (select_rv == -1) - return PyErr_SetFromErrno(PyExc_IOError); + assert(select_rv != 0); /* no timeout, can't expire */ + if (select_rv == -1) { + PyErr_SetFromErrno(PyExc_IOError); + goto finally; + } rv = _Py_write(self->fd, cp, size); if (rv == -1) { @@ -491,7 +496,7 @@ oss_writeall(oss_audio_t *self, PyObject continue; } /* it's a real error */ - return NULL; + goto finally; } /* wrote rv bytes */ @@ -499,8 +504,14 @@ oss_writeall(oss_audio_t *self, PyObject size -= rv; cp += rv; } + PyBuffer_Release(&buf); + Py_INCREF(Py_None); - return Py_None; + res = Py_None; + +finally: + PyBuffer_Release(&buf); + return res; } static PyObject *