File tree 3 files changed +33
-13
lines changed
3 files changed +33
-13
lines changed Original file line number Diff line number Diff line change @@ -344,9 +344,15 @@ def initfp(self, file):
344
344
345
345
def __init__ (self , f ):
346
346
if isinstance (f , str ):
347
- f = builtins .open (f , 'rb' )
348
- # else, assume it is an open file object already
349
- self .initfp (f )
347
+ file_object = builtins .open (f , 'rb' )
348
+ try :
349
+ self .initfp (file_object )
350
+ except :
351
+ file_object .close ()
352
+ raise
353
+ else :
354
+ # assume it is an open file object already
355
+ self .initfp (f )
350
356
351
357
def __enter__ (self ):
352
358
return self
@@ -543,16 +549,19 @@ class Aifc_write:
543
549
544
550
def __init__ (self , f ):
545
551
if isinstance (f , str ):
546
- filename = f
547
- f = builtins .open (f , 'wb' )
548
- else :
549
- # else, assume it is an open file object already
550
- filename = '???'
551
- self .initfp (f )
552
- if filename [- 5 :] == '.aiff' :
553
- self ._aifc = 0
552
+ file_object = builtins .open (f , 'wb' )
553
+ try :
554
+ self .initfp (file_object )
555
+ except :
556
+ file_object .close ()
557
+ raise
558
+
559
+ # treat .aiff file extensions as non-compressed audio
560
+ if f .endswith ('.aiff' ):
561
+ self ._aifc = 0
554
562
else :
555
- self ._aifc = 1
563
+ # assume it is an open file object already
564
+ self .initfp (f )
556
565
557
566
def initfp (self , file ):
558
567
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
@@ -149,6 +149,14 @@ def test_skipunknown(self):
149
149
#This file contains chunk types aifc doesn't recognize.
150
150
self .f = aifc .open (findfile ('Sine-1000Hz-300ms.aif' ))
151
151
152
+ def test_close_opened_files_on_error (self ):
153
+ non_aifc_file = findfile ('pluck-pcm8.wav' , subdir = 'audiodata' )
154
+ with check_no_resource_warning (self ):
155
+ with self .assertRaises (aifc .Error ):
156
+ # Try opening a non-AIFC file, with the expectation that
157
+ # `aifc.open` will fail (without raising a ResourceWarning)
158
+ f = self .f = aifc .open (non_aifc_file , 'rb' )
159
+
152
160
def test_params_added (self ):
153
161
f = self .f = aifc .open (TESTFN , 'wb' )
154
162
f .aiff ()
Original file line number Diff line number Diff line change @@ -242,6 +242,9 @@ Library
242
242
- bpo-29532: Altering a kwarg dictionary passed to functools.partial()
243
243
no longer affects a partial object after creation.
244
244
245
+ - bpo-29110: Fix file object leak in aifc.open() when file is given as a
246
+ filesystem path and is not in valid AIFF format. Patch by Anthony Zhang.
247
+
245
248
- bpo-22807: Add uuid.SafeUUID and uuid.UUID.is_safe to relay information from
246
249
the platform about whether generated UUIDs are generated with a
247
250
multiprocessing safe method.
You can’t perform that action at this time.
0 commit comments