changeset: 81142:79a8f6e1dfb0 parent: 81138:1bed43c0a5af parent: 81141:9a571c4a16d1 user: Serhiy Storchaka date: Sat Dec 29 22:38:37 2012 +0200 files: Misc/NEWS description: Issue #16485: Fix file descriptor not being closed if file header patching fails on closing of aifc file. diff -r 1bed43c0a5af -r 79a8f6e1dfb0 Lib/aifc.py --- a/Lib/aifc.py Sat Dec 29 21:46:37 2012 +0200 +++ b/Lib/aifc.py Sat Dec 29 22:38:37 2012 +0200 @@ -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 -r 1bed43c0a5af -r 79a8f6e1dfb0 Lib/test/test_aifc.py --- a/Lib/test/test_aifc.py Sat Dec 29 21:46:37 2012 +0200 +++ b/Lib/test/test_aifc.py Sat Dec 29 22:38:37 2012 +0200 @@ -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'): @@ -286,11 +293,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 -r 1bed43c0a5af -r 79a8f6e1dfb0 Misc/NEWS --- a/Misc/NEWS Sat Dec 29 21:46:37 2012 +0200 +++ b/Misc/NEWS Sat Dec 29 22:38:37 2012 +0200 @@ -200,6 +200,9 @@ 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