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

Skip to content

Commit 536ffe1

Browse files
committed
#17616: Improve context manager tests, fix bugs in close method and mode docs.
'mode' docs fix: the file must always be opened in binary in Python3. Bug in Wave_write.close: when the close method calls the check that the header exists and it raises an error, the _file attribute never gets set to None, so the next close tries to close the file again and we get an ignored traceback in the __del__ method. The fix is to set _file to None in a finally clause. This represents a behavior change...in theory a program could be checking for the error on close and then doing a recovery action on the still open file and closing it again. But this change will only go into 3.4, so I think that behavior change is acceptable given that it would be pretty weird and unlikely logic to begin with.
1 parent abe639f commit 536ffe1

3 files changed

Lines changed: 54 additions & 26 deletions

File tree

Doc/library/wave.rst

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@ The :mod:`wave` module defines the following function and exception:
1919
.. function:: open(file, mode=None)
2020

2121
If *file* is a string, open the file by that name, otherwise treat it as a
22-
seekable file-like object. *mode* can be any of
22+
seekable file-like object. *mode* can be:
2323

24-
``'r'``, ``'rb'``
24+
``'rb'``
2525
Read only mode.
2626

27-
``'w'``, ``'wb'``
27+
``'wb'``
2828
Write only mode.
2929

3030
Note that it does not allow read/write WAV files.
3131

32-
A *mode* of ``'r'`` or ``'rb'`` returns a :class:`Wave_read` object, while a
33-
*mode* of ``'w'`` or ``'wb'`` returns a :class:`Wave_write` object. If
34-
*mode* is omitted and a file-like object is passed as *file*, ``file.mode``
35-
is used as the default value for *mode* (the ``'b'`` flag is still added if
36-
necessary).
32+
A *mode* of ``'rb'`` returns a :class:`Wave_read` object, while a *mode* of
33+
``'wb'`` returns a :class:`Wave_write` object. If *mode* is omitted and a
34+
file-like object is passed as *file*, ``file.mode`` is used as the default
35+
value for *mode*.
3736

3837
If you pass in a file-like object, the wave object will not close it when its
3938
:meth:`close` method is called; it is the caller's responsibility to close

Lib/test/test_wave.py

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,49 @@ def test_getparams(self):
6969
self.assertEqual(params.comptype, self.f.getcomptype())
7070
self.assertEqual(params.compname, self.f.getcompname())
7171

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()
72+
def test_wave_write_context_manager_calls_close(self):
73+
# Close checks for a minimum header and will raise an error
74+
# if it is not set, so this proves that close is called.
75+
with self.assertRaises(wave.Error):
76+
with wave.open(TESTFN, 'wb') as f:
77+
pass
78+
print('in test:', f._file)
79+
with self.assertRaises(wave.Error):
80+
with open(TESTFN, 'wb') as testfile:
81+
with wave.open(testfile):
82+
pass
7883

84+
def test_context_manager_with_open_file(self):
85+
with open(TESTFN, 'wb') as testfile:
86+
with wave.open(testfile) as f:
87+
f.setnchannels(nchannels)
88+
f.setsampwidth(sampwidth)
89+
f.setframerate(framerate)
90+
self.assertFalse(testfile.closed)
91+
with open(TESTFN, 'rb') as testfile:
92+
with wave.open(testfile) as f:
93+
self.assertFalse(f.getfp().closed)
94+
params = f.getparams()
95+
self.assertEqual(params.nchannels, nchannels)
96+
self.assertEqual(params.sampwidth, sampwidth)
97+
self.assertEqual(params.framerate, framerate)
98+
self.assertIsNone(f.getfp())
99+
self.assertFalse(testfile.closed)
100+
101+
def test_context_manager_with_filename(self):
102+
# If the file doesn't get closed, this test won't fail, but it will
103+
# produce a resource leak warning.
104+
with wave.open(TESTFN, 'wb') as f:
105+
f.setnchannels(nchannels)
106+
f.setsampwidth(sampwidth)
107+
f.setframerate(framerate)
79108
with wave.open(TESTFN) as f:
80109
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)
110+
params = f.getparams()
111+
self.assertEqual(params.nchannels, nchannels)
112+
self.assertEqual(params.sampwidth, sampwidth)
113+
self.assertEqual(params.framerate, framerate)
114+
self.assertIsNone(f.getfp())
88115

89116

90117
if __name__ == '__main__':

Lib/wave.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,13 @@ def writeframes(self, data):
448448

449449
def close(self):
450450
if self._file:
451-
self._ensure_header_written(0)
452-
if self._datalength != self._datawritten:
453-
self._patchheader()
454-
self._file.flush()
455-
self._file = None
451+
try:
452+
self._ensure_header_written(0)
453+
if self._datalength != self._datawritten:
454+
self._patchheader()
455+
self._file.flush()
456+
finally:
457+
self._file = None
456458
if self._i_opened_the_file:
457459
self._i_opened_the_file.close()
458460
self._i_opened_the_file = None

0 commit comments

Comments
 (0)