diff options
author | INADA Naoki <methane@users.noreply.github.com> | 2017-02-26 19:09:32 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-26 19:09:32 (GMT) |
commit | c9131b61fa060a51ec181053cade9f0a7ee91e4f (patch) | |
tree | 498e94e480b82021f1fcad2cb068f4f13f66d973 /Lib/aifc.py | |
parent | 6b81003bdbd9375886bae54f876650bcdccfe6c7 (diff) | |
download | cpython-c9131b61fa060a51ec181053cade9f0a7ee91e4f.zip cpython-c9131b61fa060a51ec181053cade9f0a7ee91e4f.tar.gz cpython-c9131b61fa060a51ec181053cade9f0a7ee91e4f.tar.bz2 |
[3.6] bpo-29110: Fix file object leak in `aifc.open` (#310)
(cherry picked from commit 03f68b60e17b57f6f13729ff73245dbb37b30a4c) (GH-162)
(cherry picked from commit 5dc33eea538361f8a218255f83db2e9298dd8c53) (GH-293)
Diffstat (limited to 'Lib/aifc.py')
-rw-r--r-- | Lib/aifc.py | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/Lib/aifc.py b/Lib/aifc.py index 692d0bf..13ad7dc 100644 --- a/Lib/aifc.py +++ b/Lib/aifc.py @@ -303,6 +303,8 @@ class Aifc_read: # _ssnd_chunk -- instantiation of a chunk class for the SSND chunk # _framesize -- size of one frame in the file + _file = None # Set here since __del__ checks it + def initfp(self, file): self._version = 0 self._convert = None @@ -344,9 +346,15 @@ class Aifc_read: def __init__(self, f): if isinstance(f, str): - f = builtins.open(f, 'rb') - # else, assume it is an open file object already - self.initfp(f) + file_object = builtins.open(f, 'rb') + try: + self.initfp(file_object) + except: + file_object.close() + raise + else: + # assume it is an open file object already + self.initfp(f) def __enter__(self): return self @@ -541,18 +549,23 @@ class Aifc_write: # _datalength -- the size of the audio samples written to the header # _datawritten -- the size of the audio samples actually written + _file = None # Set here since __del__ checks it + def __init__(self, f): if isinstance(f, str): - filename = f - f = builtins.open(f, 'wb') - else: - # else, assume it is an open file object already - filename = '???' - self.initfp(f) - if filename[-5:] == '.aiff': - self._aifc = 0 + file_object = builtins.open(f, 'wb') + try: + self.initfp(file_object) + except: + file_object.close() + raise + + # treat .aiff file extensions as non-compressed audio + if f.endswith('.aiff'): + self._aifc = 0 else: - self._aifc = 1 + # assume it is an open file object already + self.initfp(f) def initfp(self, file): self._file = file |