diff -r cb612c5f30cb Doc/library/aifc.rst --- a/Doc/library/aifc.rst Thu Nov 15 18:22:23 2012 +0000 +++ b/Doc/library/aifc.rst Fri Nov 16 15:09:24 2012 +0200 @@ -51,6 +51,10 @@ used for writing, the file object should be seekable, unless you know ahead of time how many samples you are going to write in total and use :meth:`writeframesraw` and :meth:`setnframes`. + Objects returned by :func:`.open` also supports the :keyword:`with` statement. + +.. versionchanged:: 3.4 + Support for the :keyword:`with` statement was added. Objects returned by :func:`.open` when a file is opened for reading have the following methods: diff -r cb612c5f30cb Lib/aifc.py --- a/Lib/aifc.py Thu Nov 15 18:22:23 2012 +0000 +++ b/Lib/aifc.py Fri Nov 16 15:09:24 2012 +0200 @@ -334,6 +334,12 @@ # else, assume it is an open file object already self.initfp(f) + def __enter__(self): + return self + + def __exit__(self, *args): + self.close() + # # User visible methods. # @@ -553,6 +559,12 @@ def __del__(self): self.close() + def __enter__(self): + return self + + def __exit__(self, *args): + self.close() + # # User visible methods. # diff -r cb612c5f30cb Lib/test/test_aifc.py --- a/Lib/test/test_aifc.py Thu Nov 15 18:22:23 2012 +0000 +++ b/Lib/test/test_aifc.py Fri Nov 16 15:09:24 2012 +0200 @@ -43,6 +43,14 @@ (2, 2, 48000, 14400, b'NONE', b'not compressed'), ) + def test_context_manager(self): + with aifc.open(self.sndfilepath) as f: + pass + with self.assertRaises(aifc.Error): + with aifc.open(TESTFN, 'wb') as fout: + pass + fout.close() + def test_read(self): f = self.f = aifc.open(self.sndfilepath) self.assertEqual(f.readframes(0), b'')