Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f1b63c6

Browse files
Issue #16485: Fix file descriptor not being closed if file header patching fails on closing of aifc file.
2 parents f2b9cf4 + 051722d commit f1b63c6

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

Lib/aifc.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,9 @@ def writeframes(self, data):
692692
self._patchheader()
693693

694694
def close(self):
695-
if self._file:
695+
if self._file is None:
696+
return
697+
try:
696698
self._ensure_header_written(0)
697699
if self._datawritten & 1:
698700
# quick pad to even size
@@ -703,10 +705,12 @@ def close(self):
703705
self._datalength != self._datawritten or \
704706
self._marklength:
705707
self._patchheader()
708+
finally:
706709
# Prevent ref cycles
707710
self._convert = None
708-
self._file.close()
711+
f = self._file
709712
self._file = None
713+
f.close()
710714

711715
#
712716
# Internal methods.

Lib/test/test_aifc.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ def __getattr__(self, attr): return getattr(self.file, attr)
112112
self.assertEqual(testfile.closed, False)
113113
f.close()
114114
self.assertEqual(testfile.closed, True)
115+
testfile = open(TESTFN, 'wb')
116+
fout = aifc.open(testfile, 'wb')
117+
self.assertFalse(testfile.closed)
118+
with self.assertRaises(aifc.Error):
119+
fout.close()
120+
self.assertTrue(testfile.closed)
121+
fout.close() # do nothing
115122

116123
def test_write_header_comptype_sampwidth(self):
117124
for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
@@ -286,11 +293,13 @@ def test_write_params_bunch(self):
286293
def test_write_header_raises(self):
287294
fout = aifc.open(io.BytesIO(), 'wb')
288295
self.assertRaises(aifc.Error, fout.close)
296+
fout = aifc.open(io.BytesIO(), 'wb')
289297
fout.setnchannels(1)
290298
self.assertRaises(aifc.Error, fout.close)
299+
fout = aifc.open(io.BytesIO(), 'wb')
300+
fout.setnchannels(1)
291301
fout.setsampwidth(1)
292302
self.assertRaises(aifc.Error, fout.close)
293-
fout.initfp(None)
294303

295304
def test_write_header_comptype_raises(self):
296305
for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ Core and Builtins
124124
Library
125125
-------
126126

127+
- Issue #16485: Fix file descriptor not being closed if file header patching
128+
fails on closing of aifc file.
129+
127130
- Issue #16165: Fix sched.scheduler.run() method was block a scheduler for
128131
other threads.
129132

0 commit comments

Comments
 (0)