Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cfc4178

Browse files
committed
When the classes in wave.py opened files themselves, their .close() methods
didn't bother to close the files. This caused the new test_wave test to fail under Windows, as Windows won't let you delete a file that's open. Fixed that by ensuring the wave read & write classes' .close() and __del__ methods close files that were opened by their constructors.
1 parent e8d2f55 commit cfc4178

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

Lib/wave.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,15 @@ def initfp(self, file):
152152
raise Error, 'fmt chunk and/or data chunk missing'
153153

154154
def __init__(self, f):
155+
self._i_opened_the_file = None
155156
if type(f) == type(''):
156157
f = __builtin__.open(f, 'rb')
158+
self._i_opened_the_file = f
157159
# else, assume it is an open file object already
158160
self.initfp(f)
159161

162+
def __del__(self):
163+
self.close()
160164
#
161165
# User visible methods.
162166
#
@@ -168,6 +172,9 @@ def rewind(self):
168172
self._soundpos = 0
169173

170174
def close(self):
175+
if self._i_opened_the_file:
176+
self._i_opened_the_file.close()
177+
self._i_opened_the_file = None
171178
self._file = None
172179

173180
def tell(self):
@@ -284,8 +291,10 @@ class Wave_write:
284291
"""
285292

286293
def __init__(self, f):
294+
self._i_opened_the_file = None
287295
if type(f) == type(''):
288296
f = __builtin__.open(f, 'wb')
297+
self._i_opened_the_file = f
289298
self.initfp(f)
290299

291300
def initfp(self, file):
@@ -300,8 +309,7 @@ def initfp(self, file):
300309
self._datalength = 0
301310

302311
def __del__(self):
303-
if self._file:
304-
self.close()
312+
self.close()
305313

306314
#
307315
# User visible methods.
@@ -413,11 +421,15 @@ def writeframes(self, data):
413421
self._patchheader()
414422

415423
def close(self):
416-
self._ensure_header_written(0)
417-
if self._datalength != self._datawritten:
418-
self._patchheader()
419-
self._file.flush()
420-
self._file = None
424+
if self._file:
425+
self._ensure_header_written(0)
426+
if self._datalength != self._datawritten:
427+
self._patchheader()
428+
self._file.flush()
429+
self._file = None
430+
if self._i_opened_the_file:
431+
self._i_opened_the_file.close()
432+
self._i_opened_the_file = None
421433

422434
#
423435
# Internal methods.

0 commit comments

Comments
 (0)