Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e908af

Browse files
committed
#6518: enable context manager protocol for ossaudiodev types.
1 parent 1cae8b5 commit 1e908af

4 files changed

Lines changed: 42 additions & 4 deletions

File tree

Doc/library/ossaudiodev.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ and (read-only) attributes:
159159
is only useful in non-blocking mode. Has no return value, since the amount of
160160
data written is always equal to the amount of data supplied.
161161

162+
.. versionchanged:: 3.2
163+
Audio device objects also support the context manager protocol, i.e. they can
164+
be used in a :keyword:`with` statement.
165+
166+
162167
The following methods each map to exactly one :func:`ioctl` system call. The
163168
correspondence is obvious: for example, :meth:`setfmt` corresponds to the
164169
``SNDCTL_DSP_SETFMT`` ioctl, and :meth:`sync` to ``SNDCTL_DSP_SYNC`` (this can
@@ -346,6 +351,10 @@ The mixer object provides two file-like methods:
346351

347352
Returns the file handle number of the open mixer device file.
348353

354+
.. versionchanged:: 3.2
355+
Mixer objects also support the context manager protocol.
356+
357+
349358
The remaining methods are specific to audio mixing:
350359

351360

Lib/test/test_ossaudiodev.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,13 @@ def test_set_parameters(self):
162162
def test_mixer_methods(self):
163163
# Issue #8139: ossaudiodev didn't initialize its types properly,
164164
# therefore some methods were unavailable.
165-
mixer = ossaudiodev.openmixer()
166-
try:
165+
with ossaudiodev.openmixer() as mixer:
167166
self.assertGreaterEqual(mixer.fileno(), 0)
168-
finally:
169-
mixer.close()
167+
168+
def test_with(self):
169+
with ossaudiodev.open('w') as dsp:
170+
pass
171+
self.assertTrue(dsp.closed)
170172

171173

172174
def test_main():

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ Library
126126
Extensions
127127
----------
128128

129+
- Issue #6518: Support context manager protcol for ossaudiodev types.
130+
129131
- Issue #678250: Make mmap flush a noop on ACCESS_READ and ACCESS_COPY.
130132

131133
- Issue #9054: Fix a crash occurring when using the pyexpat module

Modules/ossaudiodev.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
472489
static PyObject *
473490
oss_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

Comments
 (0)