summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2012-12-29 20:25:59 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2012-12-29 20:25:59 (GMT)
commit4ed797efbc2401d34c95bbb0cea921535c868d16 (patch)
tree0ea4ac643b5384bb4ee32959b1df1feb5499ed77
parent40f12ab0c5c5adc4a8b4a03a57ffa94c87ecc2cb (diff)
downloadcpython-4ed797efbc2401d34c95bbb0cea921535c868d16.zip
cpython-4ed797efbc2401d34c95bbb0cea921535c868d16.tar.gz
cpython-4ed797efbc2401d34c95bbb0cea921535c868d16.tar.bz2
Issue #16485: Fix file descriptor not being closed if file header patching fails on closing of aifc file.
-rw-r--r--Lib/aifc.py38
-rw-r--r--Lib/test/test_aifc.py7
-rw-r--r--Misc/NEWS3
3 files changed, 32 insertions, 16 deletions
diff --git a/Lib/aifc.py b/Lib/aifc.py
index b8adc85..a0cfe5f 100644
--- a/Lib/aifc.py
+++ b/Lib/aifc.py
@@ -732,22 +732,28 @@ class Aifc_write:
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
index e492838..62b3d18 100644
--- a/Lib/test/test_aifc.py
+++ b/Lib/test/test_aifc.py
@@ -106,6 +106,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
class AIFCLowLevelTest(unittest.TestCase):
diff --git a/Misc/NEWS b/Misc/NEWS
index f087f41..8ec9675 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -175,6 +175,9 @@ Core and Builtins
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).