diff options
Diffstat (limited to 'Modules/ossaudiodev.c')
-rw-r--r-- | Modules/ossaudiodev.c | 92 |
1 files changed, 46 insertions, 46 deletions
diff --git a/Modules/ossaudiodev.c b/Modules/ossaudiodev.c index 2d0dd09..d2fd5c8 100644 --- a/Modules/ossaudiodev.c +++ b/Modules/ossaudiodev.c @@ -19,6 +19,7 @@ * $Id$ */ +#define PY_SSIZE_T_CLEAN #include "Python.h" #include "structmember.h" @@ -51,8 +52,8 @@ typedef struct { 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 */ + Py_ssize_t icount; /* input count */ + Py_ssize_t ocount; /* output count */ uint32_t afmts; /* audio formats supported by hardware */ } oss_audio_t; @@ -116,11 +117,8 @@ newossobject(PyObject *arg) provides a special ioctl() for non-blocking read/write, which is exposed via oss_nonblock() below. */ fd = _Py_open(devicename, imode|O_NONBLOCK); - - if (fd == -1) { - PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename); + if (fd == -1) return NULL; - } /* And (try to) put it back in blocking mode so we get the expected write() semantics. */ @@ -180,10 +178,8 @@ newossmixerobject(PyObject *arg) } fd = _Py_open(devicename, O_RDWR); - if (fd == -1) { - PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename); + if (fd == -1) return NULL; - } if ((self = PyObject_New(oss_mixer_t, &OSSMixerType)) == NULL) { close(fd); @@ -404,29 +400,25 @@ oss_post(oss_audio_t *self, PyObject *args) static PyObject * oss_read(oss_audio_t *self, PyObject *args) { - int size, count; - char *cp; + 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; - cp = PyBytes_AS_STRING(rv); - Py_BEGIN_ALLOW_THREADS - count = read(self->fd, cp, size); - Py_END_ALLOW_THREADS - - if (count < 0) { - PyErr_SetFromErrno(PyExc_IOError); + count = _Py_read(self->fd, PyBytes_AS_STRING(rv), size); + if (count == -1) { Py_DECREF(rv); return NULL; } + self->icount += count; _PyBytes_Resize(&rv, count); return rv; @@ -435,33 +427,32 @@ oss_read(oss_audio_t *self, PyObject *args) static PyObject * oss_write(oss_audio_t *self, PyObject *args) { - char *cp; - int rv, size; + Py_buffer data; + 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", &data)) { return NULL; } - Py_BEGIN_ALLOW_THREADS - rv = write(self->fd, cp, size); - Py_END_ALLOW_THREADS + rv = _Py_write(self->fd, data.buf, data.len); + PyBuffer_Release(&data); + if (rv == -1) + return NULL; - if (rv == -1) { - return PyErr_SetFromErrno(PyExc_IOError); - } else { - self->ocount += rv; - } + self->ocount += rv; return PyLong_FromLong(rv); } static PyObject * oss_writeall(oss_audio_t *self, PyObject *args) { - char *cp; - int rv, size; + Py_buffer data; + const char *cp; + Py_ssize_t size; + Py_ssize_t rv; fd_set write_set_fds; int select_rv; @@ -475,41 +466,50 @@ oss_writeall(oss_audio_t *self, PyObject *args) if (!_is_fd_valid(self->fd)) return NULL; - if (!PyArg_ParseTuple(args, "y#:write", &cp, &size)) + if (!PyArg_ParseTuple(args, "y*:writeall", &data)) return NULL; if (!_PyIsSelectable_fd(self->fd)) { PyErr_SetString(PyExc_ValueError, "file descriptor out of range for select"); + PyBuffer_Release(&data); return NULL; } /* use select to wait for audio device to be available */ FD_ZERO(&write_set_fds); FD_SET(self->fd, &write_set_fds); + cp = (const char *)data.buf; + size = data.len; while (size > 0) { Py_BEGIN_ALLOW_THREADS 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) + + assert(select_rv != 0); /* no timeout, can't expire */ + if (select_rv == -1) { + PyBuffer_Release(&data); return PyErr_SetFromErrno(PyExc_IOError); + } - Py_BEGIN_ALLOW_THREADS - rv = write(self->fd, cp, size); - Py_END_ALLOW_THREADS + rv = _Py_write(self->fd, cp, Py_MIN(size, INT_MAX)); if (rv == -1) { - if (errno == EAGAIN) { /* buffer is full, try again */ - errno = 0; + /* buffer is full, try again */ + if (errno == EAGAIN) { + PyErr_Clear(); continue; - } else /* it's a real error */ - return PyErr_SetFromErrno(PyExc_IOError); - } else { /* wrote rv bytes */ - self->ocount += rv; - size -= rv; - cp += rv; + } + /* it's a real error */ + PyBuffer_Release(&data); + return NULL; } + + /* wrote rv bytes */ + self->ocount += rv; + size -= rv; + cp += rv; } + PyBuffer_Release(&data); Py_INCREF(Py_None); return Py_None; } |