diff options
author | Anthony Zhang <azhang9@gmail.com> | 2017-02-22 07:23:30 (GMT) |
---|---|---|
committer | INADA Naoki <methane@users.noreply.github.com> | 2017-02-22 07:23:30 (GMT) |
commit | 03f68b60e17b57f6f13729ff73245dbb37b30a4c (patch) | |
tree | 59dc7d31f6b135159861f2e8af6e511a58340b96 /Lib/aifc.py | |
parent | 0899b9809547ec2894dcf88cf4bba732c5d47d0d (diff) | |
download | cpython-03f68b60e17b57f6f13729ff73245dbb37b30a4c.zip cpython-03f68b60e17b57f6f13729ff73245dbb37b30a4c.tar.gz cpython-03f68b60e17b57f6f13729ff73245dbb37b30a4c.tar.bz2 |
bpo-29110: Fix file object leak in `aifc.open` when given invalid AIFF file. (GH-162)
Diffstat (limited to 'Lib/aifc.py')
-rw-r--r-- | Lib/aifc.py | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/Lib/aifc.py b/Lib/aifc.py index 692d0bf..380adc8 100644 --- a/Lib/aifc.py +++ b/Lib/aifc.py @@ -344,9 +344,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 @@ -543,16 +549,19 @@ class Aifc_write: 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 |