File tree 3 files changed +30
-4
lines changed 3 files changed +30
-4
lines changed Original file line number Diff line number Diff line change @@ -288,6 +288,8 @@ class Aifc_read:
288
288
# _ssnd_chunk -- instantiation of a chunk class for the SSND chunk
289
289
# _framesize -- size of one frame in the file
290
290
291
+ _file = None # Set here since __del__ checks it
292
+
291
293
def initfp (self , file ):
292
294
self ._version = 0
293
295
self ._decomp = None
@@ -341,10 +343,16 @@ def initfp(self, file):
341
343
self ._decomp .SetParams (params )
342
344
343
345
def __init__ (self , f ):
344
- if type ( f ) == type ( '' ):
346
+ if isinstance ( f , basestring ):
345
347
f = __builtin__ .open (f , 'rb' )
346
- # else, assume it is an open file object already
347
- self .initfp (f )
348
+ try :
349
+ self .initfp (f )
350
+ except :
351
+ f .close ()
352
+ raise
353
+ else :
354
+ # assume it is an open file object already
355
+ self .initfp (f )
348
356
349
357
#
350
358
# User visible methods.
@@ -562,8 +570,10 @@ class Aifc_write:
562
570
# _datalength -- the size of the audio samples written to the header
563
571
# _datawritten -- the size of the audio samples actually written
564
572
573
+ _file = None # Set here since __del__ checks it
574
+
565
575
def __init__ (self , f ):
566
- if type ( f ) == type ( '' ):
576
+ if isinstance ( f , basestring ):
567
577
filename = f
568
578
f = __builtin__ .open (f , 'wb' )
569
579
else :
Original file line number Diff line number Diff line change @@ -129,6 +129,18 @@ def test_skipunknown(self):
129
129
#This file contains chunk types aifc doesn't recognize.
130
130
self .f = aifc .open (findfile ('Sine-1000Hz-300ms.aif' ))
131
131
132
+ def test_close_opened_files_on_error (self ):
133
+ non_aifc_file = findfile ('pluck-pcm8.wav' , subdir = 'audiodata' )
134
+
135
+ class Aifc (aifc .Aifc_read ):
136
+ def __init__ (self ):
137
+ pass
138
+
139
+ a = Aifc ()
140
+ with self .assertRaises (aifc .Error ):
141
+ aifc .Aifc_read .__init__ (a , non_aifc_file )
142
+ self .assertTrue (a ._file .closed )
143
+
132
144
def test_write_markers_values (self ):
133
145
fout = aifc .open (io .BytesIO (), 'wb' )
134
146
self .assertEqual (fout .getmarkers (), None )
Original file line number Diff line number Diff line change @@ -36,6 +36,10 @@ Extension Modules
36
36
Library
37
37
-------
38
38
39
+ - bpo-29110: Fix file object leak in aifc.open() when file is given as a
40
+ filesystem path and is not in valid AIFF format.
41
+ Original patch by Anthony Zhang.
42
+
39
43
- Issue #29354: Fixed inspect.getargs() for parameters which are cell
40
44
variables.
41
45
You can’t perform that action at this time.
0 commit comments