Issue #16485: Fix file descriptor not being closed if file header patching fails on closing of aifc file.

This commit is contained in:
Serhiy Storchaka 2012-12-29 22:36:23 +02:00
commit f1b63c6f0e
3 changed files with 19 additions and 3 deletions

View File

@ -692,7 +692,9 @@ class Aifc_write:
self._patchheader() self._patchheader()
def close(self): def close(self):
if self._file: if self._file is None:
return
try:
self._ensure_header_written(0) self._ensure_header_written(0)
if self._datawritten & 1: if self._datawritten & 1:
# quick pad to even size # quick pad to even size
@ -703,10 +705,12 @@ class Aifc_write:
self._datalength != self._datawritten or \ self._datalength != self._datawritten or \
self._marklength: self._marklength:
self._patchheader() self._patchheader()
finally:
# Prevent ref cycles # Prevent ref cycles
self._convert = None self._convert = None
self._file.close() f = self._file
self._file = None self._file = None
f.close()
# #
# Internal methods. # Internal methods.

View File

@ -112,6 +112,13 @@ class AIFCTest(unittest.TestCase):
self.assertEqual(testfile.closed, False) self.assertEqual(testfile.closed, False)
f.close() f.close()
self.assertEqual(testfile.closed, True) 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): def test_write_header_comptype_sampwidth(self):
for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'): 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): def test_write_header_raises(self):
fout = aifc.open(io.BytesIO(), 'wb') fout = aifc.open(io.BytesIO(), 'wb')
self.assertRaises(aifc.Error, fout.close) self.assertRaises(aifc.Error, fout.close)
fout = aifc.open(io.BytesIO(), 'wb')
fout.setnchannels(1) fout.setnchannels(1)
self.assertRaises(aifc.Error, fout.close) self.assertRaises(aifc.Error, fout.close)
fout = aifc.open(io.BytesIO(), 'wb')
fout.setnchannels(1)
fout.setsampwidth(1) fout.setsampwidth(1)
self.assertRaises(aifc.Error, fout.close) self.assertRaises(aifc.Error, fout.close)
fout.initfp(None)
def test_write_header_comptype_raises(self): def test_write_header_comptype_raises(self):
for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'): for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):

View File

@ -124,6 +124,9 @@ Core and Builtins
Library Library
------- -------
- Issue #16485: Fix file descriptor not being closed if file header patching
fails on closing of aifc file.
- Issue #16165: Fix sched.scheduler.run() method was block a scheduler for - Issue #16165: Fix sched.scheduler.run() method was block a scheduler for
other threads. other threads.