@@ -469,6 +469,23 @@ oss_close(oss_audio_t *self, PyObject *unused)
469469 return Py_None ;
470470}
471471
472+ static PyObject *
473+ oss_self (PyObject * self )
474+ {
475+ Py_INCREF (self );
476+ return self ;
477+ }
478+
479+ static PyObject *
480+ oss_exit (PyObject * self , PyObject * unused )
481+ {
482+ PyObject * ret = PyObject_CallMethod (self , "close" , NULL );
483+ if (!ret )
484+ return NULL ;
485+ Py_DECREF (ret );
486+ Py_RETURN_NONE ;
487+ }
488+
472489static PyObject *
473490oss_fileno (oss_audio_t * self , PyObject * unused )
474491{
@@ -782,6 +799,10 @@ static PyMethodDef oss_methods[] = {
782799 /* Aliases for backwards compatibility */
783800 { "flush" , (PyCFunction )oss_sync , METH_VARARGS },
784801
802+ /* Support for the context manager protocol */
803+ { "__enter__" , oss_self , METH_NOARGS },
804+ { "__exit__" , oss_exit , METH_VARARGS },
805+
785806 { NULL , NULL } /* sentinel */
786807};
787808
@@ -790,6 +811,10 @@ static PyMethodDef oss_mixer_methods[] = {
790811 { "close" , (PyCFunction )oss_mixer_close , METH_NOARGS },
791812 { "fileno" , (PyCFunction )oss_mixer_fileno , METH_NOARGS },
792813
814+ /* Support for the context manager protocol */
815+ { "__enter__" , oss_self , METH_NOARGS },
816+ { "__exit__" , oss_exit , METH_VARARGS },
817+
793818 /* Simple ioctl wrappers */
794819 { "controls" , (PyCFunction )oss_mixer_controls , METH_VARARGS },
795820 { "stereocontrols" , (PyCFunction )oss_mixer_stereocontrols , METH_VARARGS },
0 commit comments