[Python-checkins] cpython (2.7): Issue #16485: Fix file descriptor not being closed if file header patching

serhiy.storchaka python-checkins at python.org
Sat Dec 29 21:46:33 CET 2012


http://hg.python.org/cpython/rev/cf8d692cc847
changeset:   81139:cf8d692cc847
branch:      2.7
parent:      81128:3436769a7964
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Dec 29 22:25:59 2012 +0200
summary:
  Issue #16485: Fix file descriptor not being closed if file header patching fails on closing of aifc file.

files:
  Lib/aifc.py           |  38 +++++++++++++++++-------------
  Lib/test/test_aifc.py |   7 +++++
  Misc/NEWS             |   3 ++
  3 files changed, 32 insertions(+), 16 deletions(-)


diff --git a/Lib/aifc.py b/Lib/aifc.py
--- a/Lib/aifc.py
+++ b/Lib/aifc.py
@@ -732,22 +732,28 @@
             self._patchheader()
 
     def close(self):
-        self._ensure_header_written(0)
-        if self._datawritten & 1:
-            # quick pad to even size
-            self._file.write(chr(0))
-            self._datawritten = self._datawritten + 1
-        self._writemarkers()
-        if self._nframeswritten != self._nframes or \
-              self._datalength != self._datawritten or \
-              self._marklength:
-            self._patchheader()
-        if self._comp:
-            self._comp.CloseCompressor()
-            self._comp = None
-        # Prevent ref cycles
-        self._convert = None
-        self._file.close()
+        if self._file is None:
+            return
+        try:
+            self._ensure_header_written(0)
+            if self._datawritten & 1:
+                # quick pad to even size
+                self._file.write(chr(0))
+                self._datawritten = self._datawritten + 1
+            self._writemarkers()
+            if self._nframeswritten != self._nframes or \
+                  self._datalength != self._datawritten or \
+                  self._marklength:
+                self._patchheader()
+            if self._comp:
+                self._comp.CloseCompressor()
+                self._comp = None
+        finally:
+            # Prevent ref cycles
+            self._convert = None
+            f = self._file
+            self._file = None
+            f.close()
 
     #
     # Internal 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
@@ -106,6 +106,13 @@
         self.assertEqual(testfile.closed, False)
         f.close()
         self.assertEqual(testfile.closed, True)
+        testfile = open(TESTFN, 'wb')
+        fout = aifc.open(testfile, 'wb')
+        self.assertFalse(testfile.closed)
+        with self.assertRaises(aifc.Error):
+            fout.close()
+        self.assertTrue(testfile.closed)
+        fout.close() # do nothing
 
 
 class AIFCLowLevelTest(unittest.TestCase):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -175,6 +175,9 @@
 Library
 -------
 
+- Issue #16485: Fix file descriptor not being closed if file header patching
+  fails on closing of aifc file.
+
 - Issue #12065: connect_ex() on an SSL socket now returns the original errno
   when the socket's timeout expires (it used to return None).
 

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


More information about the Python-checkins mailing list