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

Skip to content

Commit 4d98489

Browse files
committed
Issue #10265: Close file objects explicitly in sunau. Patch by Brian Brazil.
1 parent b373799 commit 4d98489

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

Lib/sunau.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ def __init__(self, f):
154154
if type(f) == type(''):
155155
import builtins
156156
f = builtins.open(f, 'rb')
157+
self._opened = True
158+
else:
159+
self._opened = False
157160
self.initfp(f)
158161

159162
def __del__(self):
@@ -275,6 +278,8 @@ def setpos(self, pos):
275278
self._soundpos = pos
276279

277280
def close(self):
281+
if self._opened and self._file:
282+
self._file.close()
278283
self._file = None
279284

280285
class Au_write:
@@ -283,11 +288,15 @@ def __init__(self, f):
283288
if type(f) == type(''):
284289
import builtins
285290
f = builtins.open(f, 'wb')
291+
self._opened = True
292+
else:
293+
self._opened = False
286294
self.initfp(f)
287295

288296
def __del__(self):
289297
if self._file:
290298
self.close()
299+
self._file = None
291300

292301
def initfp(self, file):
293302
self._file = file
@@ -401,6 +410,8 @@ def close(self):
401410
self._datalength != self._datawritten:
402411
self._patchheader()
403412
self._file.flush()
413+
if self._opened and self._file:
414+
self._file.close()
404415
self._file = None
405416

406417
#

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Core and Builtins
5959
Library
6060
-------
6161

62+
- Issue #10265: Close file objects explicitly in sunau. Patch by Brian Brazil.
63+
6264
- Issue #10266: uu.decode didn't close in_file explicitly when it was given
6365
as a filename. Patch by Brian Brazil.
6466

0 commit comments

Comments
 (0)