[Python-checkins] cpython: Issue #16486: Make aifc files work with 'with' as context managers.

serhiy.storchaka python-checkins at python.org
Sat Dec 29 21:56:00 CET 2012


http://hg.python.org/cpython/rev/9beb11071dd1
changeset:   81143:9beb11071dd1
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Dec 29 22:54:49 2012 +0200
summary:
  Issue #16486: Make aifc files work with 'with' as context managers.

files:
  Doc/library/aifc.rst  |   4 ++++
  Lib/aifc.py           |  12 ++++++++++++
  Lib/test/test_aifc.py |  12 ++++++++++++
  Misc/NEWS             |   2 ++
  4 files changed, 30 insertions(+), 0 deletions(-)


diff --git a/Doc/library/aifc.rst b/Doc/library/aifc.rst
--- a/Doc/library/aifc.rst
+++ b/Doc/library/aifc.rst
@@ -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 --git a/Lib/aifc.py b/Lib/aifc.py
--- a/Lib/aifc.py
+++ b/Lib/aifc.py
@@ -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 --git a/Lib/test/test_aifc.py b/Lib/test/test_aifc.py
--- a/Lib/test/test_aifc.py
+++ b/Lib/test/test_aifc.py
@@ -43,6 +43,18 @@
             (2, 2, 48000, 14400, b'NONE', b'not compressed'),
             )
 
+    def test_context_manager(self):
+        with open(self.sndfilepath, 'rb') as testfile:
+            with aifc.open(testfile) as f:
+                pass
+            self.assertEqual(testfile.closed, True)
+        with open(TESTFN, 'wb') as testfile:
+            with self.assertRaises(aifc.Error):
+                with aifc.open(testfile, 'wb') as fout:
+                    pass
+            self.assertEqual(testfile.closed, True)
+            fout.close() # do nothing
+
     def test_read(self):
         f = self.f = aifc.open(self.sndfilepath)
         self.assertEqual(f.readframes(0), b'')
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -200,6 +200,8 @@
 Library
 -------
 
+- Issue #16486: Make aifc files work with 'with' as context managers.
+
 - Issue #16485: Fix file descriptor not being closed if file header patching
   fails on closing of aifc file.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list