diff options
Diffstat (limited to 'Modules/ossaudiodev.c')
| -rw-r--r-- | Modules/ossaudiodev.c | 163 |
1 files changed, 121 insertions, 42 deletions
diff --git a/Modules/ossaudiodev.c b/Modules/ossaudiodev.c index 647a21e..b38ce52 100644 --- a/Modules/ossaudiodev.c +++ b/Modules/ossaudiodev.c @@ -236,7 +236,7 @@ _do_ioctl_1(int fd, PyObject *args, char *fname, int cmd) if (ioctl(fd, cmd, &arg) == -1) return PyErr_SetFromErrno(PyExc_IOError); - return PyInt_FromLong(arg); + return PyLong_FromLong(arg); } @@ -261,7 +261,7 @@ _do_ioctl_1_internal(int fd, PyObject *args, char *fname, int cmd) if (ioctl(fd, cmd, &arg) == -1) return PyErr_SetFromErrno(PyExc_IOError); - return PyInt_FromLong(arg); + return PyLong_FromLong(arg); } @@ -321,7 +321,7 @@ oss_getfmts(oss_audio_t *self, PyObject *unused) int mask; if (ioctl(self->fd, SNDCTL_DSP_GETFMTS, &mask) == -1) return PyErr_SetFromErrno(PyExc_IOError); - return PyInt_FromLong(mask); + return PyLong_FromLong(mask); } static PyObject * @@ -367,10 +367,10 @@ oss_read(oss_audio_t *self, PyObject *args) if (!PyArg_ParseTuple(args, "i:read", &size)) return NULL; - rv = PyString_FromStringAndSize(NULL, size); + rv = PyBytes_FromStringAndSize(NULL, size); if (rv == NULL) return NULL; - cp = PyString_AS_STRING(rv); + cp = PyBytes_AS_STRING(rv); Py_BEGIN_ALLOW_THREADS count = read(self->fd, cp, size); @@ -382,7 +382,7 @@ oss_read(oss_audio_t *self, PyObject *args) return NULL; } self->icount += count; - _PyString_Resize(&rv, count); + _PyBytes_Resize(&rv, count); return rv; } @@ -392,7 +392,7 @@ oss_write(oss_audio_t *self, PyObject *args) char *cp; int rv, size; - if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) { + if (!PyArg_ParseTuple(args, "y#:write", &cp, &size)) { return NULL; } @@ -405,7 +405,7 @@ oss_write(oss_audio_t *self, PyObject *args) } else { self->ocount += rv; } - return PyInt_FromLong(rv); + return PyLong_FromLong(rv); } static PyObject * @@ -423,7 +423,7 @@ oss_writeall(oss_audio_t *self, PyObject *args) mode, the behaviour of write() and writeall() from Python is indistinguishable. */ - if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) + if (!PyArg_ParseTuple(args, "y#:write", &cp, &size)) return NULL; if (!_PyIsSelectable_fd(self->fd)) { @@ -476,9 +476,26 @@ oss_close(oss_audio_t *self, PyObject *unused) } static PyObject * +oss_self(PyObject *self, PyObject *unused) +{ + Py_INCREF(self); + return self; +} + +static PyObject * +oss_exit(PyObject *self, PyObject *unused) +{ + PyObject *ret = PyObject_CallMethod(self, "close", NULL); + if (!ret) + return NULL; + Py_DECREF(ret); + Py_RETURN_NONE; +} + +static PyObject * oss_fileno(oss_audio_t *self, PyObject *unused) { - return PyInt_FromLong(self->fd); + return PyLong_FromLong(self->fd); } @@ -535,9 +552,9 @@ oss_setparameters(oss_audio_t *self, PyObject *args) rv = PyTuple_New(3); if (rv == NULL) return NULL; - PyTuple_SET_ITEM(rv, 0, PyInt_FromLong(fmt)); - PyTuple_SET_ITEM(rv, 1, PyInt_FromLong(channels)); - PyTuple_SET_ITEM(rv, 2, PyInt_FromLong(rate)); + PyTuple_SET_ITEM(rv, 0, PyLong_FromLong(fmt)); + PyTuple_SET_ITEM(rv, 1, PyLong_FromLong(channels)); + PyTuple_SET_ITEM(rv, 2, PyLong_FromLong(rate)); return rv; } @@ -590,7 +607,7 @@ oss_bufsize(oss_audio_t *self, PyObject *unused) PyErr_SetFromErrno(PyExc_IOError); return NULL; } - return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize)); + return PyLong_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize)); } /* obufcount returns the number of samples that are available in the @@ -609,7 +626,7 @@ oss_obufcount(oss_audio_t *self, PyObject *unused) PyErr_SetFromErrno(PyExc_IOError); return NULL; } - return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) / + return PyLong_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) / (ssize * nchannels)); } @@ -629,7 +646,7 @@ oss_obuffree(oss_audio_t *self, PyObject *unused) PyErr_SetFromErrno(PyExc_IOError); return NULL; } - return PyInt_FromLong(ai.bytes / (ssize * nchannels)); + return PyLong_FromLong(ai.bytes / (ssize * nchannels)); } static PyObject * @@ -668,7 +685,7 @@ oss_mixer_close(oss_mixer_t *self, PyObject *unused) static PyObject * oss_mixer_fileno(oss_mixer_t *self, PyObject *unused) { - return PyInt_FromLong(self->fd); + return PyLong_FromLong(self->fd); } /* Simple mixer interface methods */ @@ -788,6 +805,10 @@ static PyMethodDef oss_methods[] = { /* Aliases for backwards compatibility */ { "flush", (PyCFunction)oss_sync, METH_VARARGS }, + /* Support for the context manager protocol */ + { "__enter__", oss_self, METH_NOARGS }, + { "__exit__", oss_exit, METH_VARARGS }, + { NULL, NULL} /* sentinel */ }; @@ -796,6 +817,10 @@ static PyMethodDef oss_mixer_methods[] = { { "close", (PyCFunction)oss_mixer_close, METH_NOARGS }, { "fileno", (PyCFunction)oss_mixer_fileno, METH_NOARGS }, + /* Support for the context manager protocol */ + { "__enter__", oss_self, METH_NOARGS }, + { "__exit__", oss_exit, METH_VARARGS }, + /* Simple ioctl wrappers */ { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS }, { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS}, @@ -809,43 +834,42 @@ static PyMethodDef oss_mixer_methods[] = { }; static PyObject * -oss_getattr(oss_audio_t *self, char *name) +oss_getattro(oss_audio_t *self, PyObject *nameobj) { + char *name = ""; PyObject * rval = NULL; + + if (PyUnicode_Check(nameobj)) + name = _PyUnicode_AsString(nameobj); + if (strcmp(name, "closed") == 0) { rval = (self->fd == -1) ? Py_True : Py_False; Py_INCREF(rval); } else if (strcmp(name, "name") == 0) { - rval = PyString_FromString(self->devicename); + rval = PyUnicode_FromString(self->devicename); } else if (strcmp(name, "mode") == 0) { /* No need for a "default" in this switch: from newossobject(), self->mode can only be one of these three values. */ switch(self->mode) { case O_RDONLY: - rval = PyString_FromString("r"); + rval = PyUnicode_FromString("r"); break; case O_RDWR: - rval = PyString_FromString("rw"); + rval = PyUnicode_FromString("rw"); break; case O_WRONLY: - rval = PyString_FromString("w"); + rval = PyUnicode_FromString("w"); break; } } else { - rval = Py_FindMethod(oss_methods, (PyObject *)self, name); + rval = PyObject_GenericGetAttr((PyObject *)self, nameobj); } return rval; } -static PyObject * -oss_mixer_getattr(oss_mixer_t *self, char *name) -{ - return Py_FindMethod(oss_mixer_methods, (PyObject *)self, name); -} - static PyTypeObject OSSAudioType = { PyVarObject_HEAD_INIT(&PyType_Type, 0) "ossaudiodev.oss_audio_device", /*tp_name*/ @@ -854,10 +878,28 @@ static PyTypeObject OSSAudioType = { /* methods */ (destructor)oss_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ - (getattrfunc)oss_getattr, /*tp_getattr*/ + 0, /*tp_getattr*/ 0, /*tp_setattr*/ - 0, /*tp_compare*/ + 0, /*tp_reserved*/ 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + (getattrofunc)oss_getattro, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + 0, /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + oss_methods, /*tp_methods*/ }; static PyTypeObject OSSMixerType = { @@ -868,10 +910,28 @@ static PyTypeObject OSSMixerType = { /* methods */ (destructor)oss_mixer_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ - (getattrfunc)oss_mixer_getattr, /*tp_getattr*/ + 0, /*tp_getattr*/ 0, /*tp_setattr*/ - 0, /*tp_compare*/ + 0, /*tp_reserved*/ 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + 0, /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + oss_mixer_methods, /*tp_methods*/ }; @@ -895,7 +955,7 @@ static PyMethodDef ossaudiodev_methods[] = { #define _EXPORT_INT(mod, name) \ - if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return; + if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return NULL; static char *control_labels[] = SOUND_DEVICE_LABELS; @@ -919,12 +979,12 @@ build_namelists (PyObject *module) if (labels == NULL || names == NULL) goto error2; for (i = 0; i < num_controls; i++) { - s = PyString_FromString(control_labels[i]); + s = PyUnicode_FromString(control_labels[i]); if (s == NULL) goto error2; PyList_SET_ITEM(labels, i, s); - s = PyString_FromString(control_names[i]); + s = PyUnicode_FromString(control_names[i]); if (s == NULL) goto error2; PyList_SET_ITEM(names, i, s); @@ -945,17 +1005,35 @@ error1: } -void -initossaudiodev(void) +static struct PyModuleDef ossaudiodevmodule = { + PyModuleDef_HEAD_INIT, + "ossaudiodev", + NULL, + -1, + ossaudiodev_methods, + NULL, + NULL, + NULL, + NULL +}; + +PyMODINIT_FUNC +PyInit_ossaudiodev(void) { PyObject *m; - m = Py_InitModule("ossaudiodev", ossaudiodev_methods); + if (PyType_Ready(&OSSAudioType) < 0) + return NULL; + + if (PyType_Ready(&OSSMixerType) < 0) + return NULL; + + m = PyModule_Create(&ossaudiodevmodule); if (m == NULL) - return; + return NULL; OSSAudioError = PyErr_NewException("ossaudiodev.OSSAudioError", - NULL, NULL); + NULL, NULL); if (OSSAudioError) { /* Each call to PyModule_AddObject decrefs it; compensate: */ Py_INCREF(OSSAudioError); @@ -967,7 +1045,7 @@ initossaudiodev(void) /* Build 'control_labels' and 'control_names' lists and add them to the module. */ if (build_namelists(m) == -1) /* XXX what to do here? */ - return; + return NULL; /* Expose the audio format numbers -- essential! */ _EXPORT_INT(m, AFMT_QUERY); @@ -1139,4 +1217,5 @@ initossaudiodev(void) _EXPORT_INT(m, SNDCTL_TMR_STOP); _EXPORT_INT(m, SNDCTL_TMR_TEMPO); _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE); + return m; } |
