diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2012-12-29 22:38:37 +0200 |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2012-12-29 22:38:37 +0200 |
commit | ff2840a76f04c8846e8ad5b7e8379998ab3e87a6 (patch) | |
tree | 66b2d597ba355335032f7bf2b635fd68c4cd3b45 | |
parent | d07db96ab6942acef2e95194043d6d30ac48604e (diff) | |
parent | f1b63c6f0ef4074ea12bdbc535c8bc9738c9a78e (diff) | |
download | cpython-git-ff2840a76f04c8846e8ad5b7e8379998ab3e87a6.tar.gz |
Issue #16485: Fix file descriptor not being closed if file header patching fails on closing of aifc file.
-rw-r--r-- | Lib/aifc.py | 8 | ||||
-rw-r--r-- | Lib/test/test_aifc.py | 11 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
3 files changed, 19 insertions, 3 deletions
diff --git a/Lib/aifc.py b/Lib/aifc.py index ec4f8223aa..a19b38f4aa 100644 --- a/Lib/aifc.py +++ b/Lib/aifc.py @@ -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. diff --git a/Lib/test/test_aifc.py b/Lib/test/test_aifc.py index ad6f6105ec..9c0e7b96ce 100644 --- a/Lib/test/test_aifc.py +++ b/Lib/test/test_aifc.py @@ -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'): @@ -286,11 +293,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'): @@ -200,6 +200,9 @@ Core and Builtins Library ------- +- Issue #16485: Fix file descriptor not being closed if file header patching + fails on closing of aifc file. + - Issue #16640: Run less code under a lock in sched module. - Issue #16165: Fix sched.scheduler.run() method was block a scheduler for |