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

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


http://hg.python.org/cpython/rev/e3c4e9f4ea0f
changeset:   81140:e3c4e9f4ea0f
branch:      3.2
parent:      81129:ee8d999b6e05
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Dec 29 22:30:56 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           |   8 ++++++--
  Lib/test/test_aifc.py |  11 ++++++++++-
  Misc/NEWS             |   3 +++
  3 files changed, 19 insertions(+), 3 deletions(-)


diff --git a/Lib/aifc.py b/Lib/aifc.py
--- a/Lib/aifc.py
+++ b/Lib/aifc.py
@@ -692,7 +692,9 @@
             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 @@
                   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.
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
@@ -112,6 +112,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
 
     def test_write_header_comptype_sampwidth(self):
         for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
@@ -291,11 +298,13 @@
     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'):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -189,6 +189,9 @@
 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.
 

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


More information about the Python-checkins mailing list