FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[2.7] bpo-29110: Fix file object leak in aifc.open by methane · Pull Request #356 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) No extension  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
18 changes: 14 additions & 4 deletions Lib/aifc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,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._decomp = None
Expand Down Expand Up @@ -341,10 +343,16 @@ def initfp(self, file):
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.
Expand Down Expand Up @@ -562,8 +570,10 @@ 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 type(f) == type(''):
if isinstance(f, basestring):
filename = f
f = __builtin__.open(f, 'wb')
else:
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_aifc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ def test_skipunknown(self):
#This file contains chunk types aifc doesn't recognize.
self.f = aifc.open(findfile('Sine-1000Hz-300ms.aif'))

def test_close_opened_files_on_error(self):
non_aifc_file = findfile('pluck-pcm8.wav', subdir='audiodata')

class Aifc(aifc.Aifc_read):
def __init__(self):
pass

a = Aifc()
with self.assertRaises(aifc.Error):
aifc.Aifc_read.__init__(a, non_aifc_file)
self.assertTrue(a._file.closed)

def test_write_markers_values(self):
fout = aifc.open(io.BytesIO(), 'wb')
self.assertEqual(fout.getmarkers(), None)
Expand Down
4 changes: 4 additions & 0 deletions Misc/NEWS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ Extension Modules
Library
-------

- bpo-29110: Fix file object leak in aifc.open() when file is given as a
filesystem path and is not in valid AIFF format.
Original patch by Anthony Zhang.

- Issue #29354: Fixed inspect.getargs() for parameters which are cell
variables.

Expand Down

Back | FazBrowse Home | New Git URL