diff options
author | Georg Brandl <georg@python.org> | 2010-10-23 17:31:52 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-10-23 17:31:52 (GMT) |
commit | 1e908af3354e20e18dfdcf30bc8aee6ab53ab1ee (patch) | |
tree | e8667db40effeb63db33a46b849aee2e4ac9e836 /Modules/ossaudiodev.c | |
parent | 1cae8b58eb5929b22a469e7ebb65382b808f7d04 (diff) | |
download | cpython-1e908af3354e20e18dfdcf30bc8aee6ab53ab1ee.zip cpython-1e908af3354e20e18dfdcf30bc8aee6ab53ab1ee.tar.gz cpython-1e908af3354e20e18dfdcf30bc8aee6ab53ab1ee.tar.bz2 |
#6518: enable context manager protocol for ossaudiodev types.
Diffstat (limited to 'Modules/ossaudiodev.c')
-rw-r--r-- | Modules/ossaudiodev.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Modules/ossaudiodev.c b/Modules/ossaudiodev.c index 5c990ef..2c9d797 100644 --- a/Modules/ossaudiodev.c +++ b/Modules/ossaudiodev.c @@ -470,6 +470,23 @@ oss_close(oss_audio_t *self, PyObject *unused) } static PyObject * +oss_self(PyObject *self) +{ + 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 PyLong_FromLong(self->fd); @@ -782,6 +799,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 */ }; @@ -790,6 +811,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}, |