]> granicus.if.org Git - python/commitdiff
Issue #16485: Fix file descriptor not being closed if file header patching fails...
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 29 Dec 2012 20:30:56 +0000 (22:30 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 29 Dec 2012 20:30:56 +0000 (22:30 +0200)
Lib/aifc.py
Lib/test/test_aifc.py
Misc/NEWS

index 775f39c74fb5f554226712607df7fd7c80ba7bdd..841f5ae28730ea6ffe2eff3e82c03b514e285c57 100644 (file)
@@ -692,7 +692,9 @@ class Aifc_write:
             self._patchheader()
 
     def close(self):
-        if self._file:
+        if self._file is None:
+            return
+        try:
             self._ensure_header_written(0)
             if self._datawritten & 1:
                 # quick pad to even size
@@ -703,10 +705,12 @@ class Aifc_write:
                   self._datalength != self._datawritten or \
                   self._marklength:
                 self._patchheader()
+        finally:
             # Prevent ref cycles
             self._convert = None
-            self._file.close()
+            f = self._file
             self._file = None
+            f.close()
 
     #
     # Internal methods.
index ee4ad6b0a32d37b2ec3f83435ef5ba5d47aa2723..0b19af6a10a713abb7113d207a417122eb84c8a5 100644 (file)
@@ -112,6 +112,13 @@ class AIFCTest(unittest.TestCase):
         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
 
     def test_write_header_comptype_sampwidth(self):
         for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
@@ -291,11 +298,13 @@ class AIFCLowLevelTest(unittest.TestCase):
     def test_write_header_raises(self):
         fout = aifc.open(io.BytesIO(), 'wb')
         self.assertRaises(aifc.Error, fout.close)
+        fout = aifc.open(io.BytesIO(), 'wb')
         fout.setnchannels(1)
         self.assertRaises(aifc.Error, fout.close)
+        fout = aifc.open(io.BytesIO(), 'wb')
+        fout.setnchannels(1)
         fout.setsampwidth(1)
         self.assertRaises(aifc.Error, fout.close)
-        fout.initfp(None)
 
     def test_write_header_comptype_raises(self):
         for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
index 356e58681197d16e546e36220a4eb4ef68d3b07c..8ce30c529ae3608ae3a789805d3d5248b48eecdb 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -189,6 +189,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #16485: Fix file descriptor not being closed if file header patching
+  fails on closing of aifc file.
+
 - Issue #16504: IDLE now catches SyntaxErrors raised by tokenizer. Patch by
   Roger Serwy.