diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2013-07-31 19:54:18 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2013-07-31 19:54:18 (GMT) |
commit | c27cd71cd71e5b3f464f6994e2a73f201eb430ca (patch) | |
tree | b973bb782096772fc8a38a0c3bd534acc867c10a /Lib | |
parent | 5c30a75722f1fb42f4e49dcc5dd30d90d7717086 (diff) | |
parent | c91d5eea106d191e931f953bdbdb1db25e051767 (diff) | |
download | cpython-c27cd71cd71e5b3f464f6994e2a73f201eb430ca.zip cpython-c27cd71cd71e5b3f464f6994e2a73f201eb430ca.tar.gz cpython-c27cd71cd71e5b3f464f6994e2a73f201eb430ca.tar.bz2 |
Merge
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_wave.py | 29 | ||||
-rw-r--r-- | Lib/wave.py | 13 |
2 files changed, 32 insertions, 10 deletions
diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py index 581d973..e9ee15c 100644 --- a/Lib/test/test_wave.py +++ b/Lib/test/test_wave.py @@ -1,7 +1,5 @@ -from test.support import TESTFN, run_unittest -import os +from test.support import TESTFN, unlink import wave -import struct import unittest nchannels = 2 @@ -17,10 +15,7 @@ class TestWave(unittest.TestCase): def tearDown(self): if self.f is not None: self.f.close() - try: - os.remove(TESTFN) - except OSError: - pass + unlink(TESTFN) def test_it(self, test_rounding=False): self.f = wave.open(TESTFN, 'wb') @@ -74,9 +69,23 @@ 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() + + 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) -def test_main(): - run_unittest(TestWave) if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/wave.py b/Lib/wave.py index ea410c1..695a4be 100644 --- a/Lib/wave.py +++ b/Lib/wave.py @@ -167,6 +167,13 @@ class Wave_read: def __del__(self): self.close() + + def __enter__(self): + return self + + def __exit__(self, *args): + self.close() + # # User visible methods. # @@ -323,6 +330,12 @@ class Wave_write: def __del__(self): self.close() + def __enter__(self): + return self + + def __exit__(self, *args): + self.close() + # # User visible methods. # |