diff options
author | R David Murray <rdmurray@bitdance.com> | 2013-08-01 00:48:26 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2013-08-01 00:48:26 (GMT) |
commit | 536ffe161c014f3646cbf52bc527f2ba9ebd6478 (patch) | |
tree | fc1491b81c3db517bad24d0ec425b666cf37a426 /Lib/test/test_wave.py | |
parent | abe639f1150a770b2435417330c1c2a5a2e239ab (diff) | |
download | cpython-536ffe161c014f3646cbf52bc527f2ba9ebd6478.zip cpython-536ffe161c014f3646cbf52bc527f2ba9ebd6478.tar.gz cpython-536ffe161c014f3646cbf52bc527f2ba9ebd6478.tar.bz2 |
#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.
Diffstat (limited to 'Lib/test/test_wave.py')
-rw-r--r-- | Lib/test/test_wave.py | 53 |
1 files changed, 40 insertions, 13 deletions
diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py index e9ee15c..c505fb8 100644 --- a/Lib/test/test_wave.py +++ b/Lib/test/test_wave.py @@ -69,22 +69,49 @@ class TestWave(unittest.TestCase): self.assertEqual(params.comptype, self.f.getcomptype()) self.assertEqual(params.compname, self.f.getcompname()) - def test_context_manager(self): - self.f = wave.open(TESTFN, 'wb') - self.f.setnchannels(nchannels) - self.f.setsampwidth(sampwidth) - self.f.setframerate(framerate) - self.f.close() + def test_wave_write_context_manager_calls_close(self): + # Close checks for a minimum header and will raise an error + # if it is not set, so this proves that close is called. + with self.assertRaises(wave.Error): + with wave.open(TESTFN, 'wb') as f: + pass + print('in test:', f._file) + with self.assertRaises(wave.Error): + with open(TESTFN, 'wb') as testfile: + with wave.open(testfile): + pass + def test_context_manager_with_open_file(self): + with open(TESTFN, 'wb') as testfile: + with wave.open(testfile) as f: + f.setnchannels(nchannels) + f.setsampwidth(sampwidth) + f.setframerate(framerate) + self.assertFalse(testfile.closed) + with open(TESTFN, 'rb') as testfile: + with wave.open(testfile) as f: + self.assertFalse(f.getfp().closed) + params = f.getparams() + self.assertEqual(params.nchannels, nchannels) + self.assertEqual(params.sampwidth, sampwidth) + self.assertEqual(params.framerate, framerate) + self.assertIsNone(f.getfp()) + self.assertFalse(testfile.closed) + + def test_context_manager_with_filename(self): + # If the file doesn't get closed, this test won't fail, but it will + # produce a resource leak warning. + with wave.open(TESTFN, 'wb') as f: + f.setnchannels(nchannels) + f.setsampwidth(sampwidth) + f.setframerate(framerate) with wave.open(TESTFN) as f: self.assertFalse(f.getfp().closed) - self.assertIs(f.getfp(), None) - - with open(TESTFN, 'wb') as testfile: - with self.assertRaises(wave.Error): - with wave.open(testfile, 'wb'): - pass - self.assertEqual(testfile.closed, False) + params = f.getparams() + self.assertEqual(params.nchannels, nchannels) + self.assertEqual(params.sampwidth, sampwidth) + self.assertEqual(params.framerate, framerate) + self.assertIsNone(f.getfp()) if __name__ == '__main__': |