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

Skip to content

Commit c91d5ee

Browse files
committed
#17616: wave.open now supports the 'with' statement.
Feature and tests by ClClaudiu.Popa, I added the doc changes.
1 parent 0ce642e commit c91d5ee

6 files changed

Lines changed: 45 additions & 12 deletions

File tree

Doc/library/aifc.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ Module :mod:`aifc` defines the following function:
5151
used for writing, the file object should be seekable, unless you know ahead of
5252
time how many samples you are going to write in total and use
5353
:meth:`writeframesraw` and :meth:`setnframes`.
54-
Objects returned by :func:`.open` also supports the :keyword:`with` statement.
54+
The :func:`.open` function may be used in a :keyword:`with` statement. When
55+
the :keyword:`with` block completes, the :meth:`~aifc.close` method is called.
5556

5657
.. versionchanged:: 3.4
5758
Support for the :keyword:`with` statement was added.

Doc/library/wave.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ The :mod:`wave` module defines the following function and exception:
3939
:meth:`close` method is called; it is the caller's responsibility to close
4040
the file object.
4141

42+
The :func:`.open` function may be used in a :keyword:`with` statement. When
43+
the :keyword:`with` block completes, the :meth:`Wave_read.close()
44+
<wave.Wave_read.close>` or :meth:`Wave_write.close()
45+
<wave.Wave_write.close()>` method is called.
46+
4247

4348
.. function:: openfp(file, mode)
4449

Doc/whatsnew/3.4.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,11 @@ wave
239239
The :meth:`~wave.getparams` method now returns a namedtuple rather than a
240240
plain tuple. (Contributed by Claudiu Popa in :issue:`17487`.)
241241

242+
:meth:`wave.open` now supports the context manager protocol. (Contributed
243+
by Claudiu Popa in :issue:`17616`.)
244+
242245
stat
243-
---
246+
----
244247

245248
The stat module is now backed by a C implementation in :mod:`_stat`. A C
246249
implementation is required as most of the values aren't standardized and

Lib/test/test_wave.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
from test.support import TESTFN, run_unittest
2-
import os
1+
from test.support import TESTFN, unlink
32
import wave
4-
import struct
53
import unittest
64

75
nchannels = 2
@@ -17,10 +15,7 @@ def setUp(self):
1715
def tearDown(self):
1816
if self.f is not None:
1917
self.f.close()
20-
try:
21-
os.remove(TESTFN)
22-
except OSError:
23-
pass
18+
unlink(TESTFN)
2419

2520
def test_it(self, test_rounding=False):
2621
self.f = wave.open(TESTFN, 'wb')
@@ -74,9 +69,23 @@ def test_getparams(self):
7469
self.assertEqual(params.comptype, self.f.getcomptype())
7570
self.assertEqual(params.compname, self.f.getcompname())
7671

72+
def test_context_manager(self):
73+
self.f = wave.open(TESTFN, 'wb')
74+
self.f.setnchannels(nchannels)
75+
self.f.setsampwidth(sampwidth)
76+
self.f.setframerate(framerate)
77+
self.f.close()
78+
79+
with wave.open(TESTFN) as f:
80+
self.assertFalse(f.getfp().closed)
81+
self.assertIs(f.getfp(), None)
82+
83+
with open(TESTFN, 'wb') as testfile:
84+
with self.assertRaises(wave.Error):
85+
with wave.open(testfile, 'wb'):
86+
pass
87+
self.assertEqual(testfile.closed, False)
7788

78-
def test_main():
79-
run_unittest(TestWave)
8089

8190
if __name__ == '__main__':
82-
test_main()
91+
unittest.main()

Lib/wave.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ def __init__(self, f):
167167

168168
def __del__(self):
169169
self.close()
170+
171+
def __enter__(self):
172+
return self
173+
174+
def __exit__(self, *args):
175+
self.close()
176+
170177
#
171178
# User visible methods.
172179
#
@@ -323,6 +330,12 @@ def initfp(self, file):
323330
def __del__(self):
324331
self.close()
325332

333+
def __enter__(self):
334+
return self
335+
336+
def __exit__(self, *args):
337+
self.close()
338+
326339
#
327340
# User visible methods.
328341
#

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ Core and Builtins
173173
Library
174174
-------
175175

176+
- Issue #17616: wave.open now supports the context manager protocol.
177+
176178
- Issue #18599: Fix name attribute of _sha1.sha1() object. It now returns
177179
'SHA1' instead of 'SHA'.
178180

0 commit comments

Comments
 (0)