File tree Expand file tree Collapse file tree 3 files changed +33
-13
lines changed Expand file tree Collapse file tree 3 files changed +33
-13
lines changed Original file line number Diff line number Diff line change @@ -335,9 +335,15 @@ def initfp(self, file):
335
335
336
336
def __init__ (self , f ):
337
337
if isinstance (f , str ):
338
- f = builtins .open (f , 'rb' )
339
- # else, assume it is an open file object already
340
- self .initfp (f )
338
+ file_object = builtins .open (f , 'rb' )
339
+ try :
340
+ self .initfp (file_object )
341
+ except :
342
+ file_object .close ()
343
+ raise
344
+ else :
345
+ # assume it is an open file object already
346
+ self .initfp (f )
341
347
342
348
def __enter__ (self ):
343
349
return self
@@ -534,16 +540,19 @@ class Aifc_write:
534
540
535
541
def __init__ (self , f ):
536
542
if isinstance (f , str ):
537
- filename = f
538
- f = builtins .open (f , 'wb' )
539
- else :
540
- # else, assume it is an open file object already
541
- filename = '???'
542
- self .initfp (f )
543
- if filename [- 5 :] == '.aiff' :
544
- self ._aifc = 0
543
+ file_object = builtins .open (f , 'wb' )
544
+ try :
545
+ self .initfp (file_object )
546
+ except :
547
+ file_object .close ()
548
+ raise
549
+
550
+ # treat .aiff file extensions as non-compressed audio
551
+ if f .endswith ('.aiff' ):
552
+ self ._aifc = 0
545
553
else :
546
- self ._aifc = 1
554
+ # assume it is an open file object already
555
+ self .initfp (f )
547
556
548
557
def initfp (self , file ):
549
558
self ._file = file
Original file line number Diff line number Diff line change 1
- from test .support import findfile , TESTFN , unlink
1
+ from test .support import check_no_resource_warning , findfile , TESTFN , unlink
2
2
import unittest
3
3
from test import audiotests
4
4
from audioop import byteswap
@@ -150,6 +150,14 @@ def test_skipunknown(self):
150
150
#This file contains chunk types aifc doesn't recognize.
151
151
self .f = aifc .open (findfile ('Sine-1000Hz-300ms.aif' ))
152
152
153
+ def test_close_opened_files_on_error (self ):
154
+ non_aifc_file = findfile ('pluck-pcm8.wav' , subdir = 'audiodata' )
155
+ with check_no_resource_warning (self ):
156
+ with self .assertRaises (aifc .Error ):
157
+ # Try opening a non-AIFC file, with the expectation that
158
+ # `aifc.open` will fail (without raising a ResourceWarning)
159
+ f = self .f = aifc .open (non_aifc_file , 'rb' )
160
+
153
161
def test_params_added (self ):
154
162
f = self .f = aifc .open (TESTFN , 'wb' )
155
163
f .aiff ()
Original file line number Diff line number Diff line change @@ -32,6 +32,9 @@ Extension Modules
32
32
Library
33
33
-------
34
34
35
+ - bpo-29110: Fix file object leak in aifc.open() when file is given as a
36
+ filesystem path and is not in valid AIFF format. Patch by Anthony Zhang.
37
+
35
38
- bpo-29532: Altering a kwarg dictionary passed to functools.partial()
36
39
no longer affects a partial object after creation.
37
40
You can’t perform that action at this time.
0 commit comments