bpo-29110: Fix file object leak in aifc.open (GH-356)
diff --git a/Lib/aifc.py b/Lib/aifc.py
index c9a021e..e678327 100644
--- a/Lib/aifc.py
+++ b/Lib/aifc.py
@@ -288,6 +288,8 @@
# _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._decomp = None
@@ -341,10 +343,16 @@
self._decomp.SetParams(params)
def __init__(self, f):
- if type(f) == type(''):
+ if isinstance(f, basestring):
f = __builtin__.open(f, 'rb')
- # else, assume it is an open file object already
- self.initfp(f)
+ try:
+ self.initfp(f)
+ except:
+ f.close()
+ raise
+ else:
+ # assume it is an open file object already
+ self.initfp(f)
#
# User visible methods.
@@ -562,8 +570,10 @@
# _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 type(f) == type(''):
+ if isinstance(f, basestring):
filename = f
f = __builtin__.open(f, 'wb')
else: