diff --git a/Lib/aifc.py b/Lib/aifc.py index 8e4f864495d..e0b76d6dd2f 100644 --- a/Lib/aifc.py +++ b/Lib/aifc.py @@ -282,10 +282,11 @@ class Aifc_read: self._convert = None self._markers = [] self._soundpos = 0 - self._file = Chunk(file) - if self._file.getname() != 'FORM': + self._file = file + chunk = Chunk(file) + if chunk.getname() != 'FORM': raise Error, 'file does not start with FORM id' - formdata = self._file.read(4) + formdata = chunk.read(4) if formdata == 'AIFF': self._aifc = 0 elif formdata == 'AIFC': @@ -347,7 +348,7 @@ class Aifc_read: if self._decomp: self._decomp.CloseDecompressor() self._decomp = None - self._file = None + self._file.close() def tell(self): return self._soundpos @@ -732,8 +733,7 @@ class Aifc_write: if self._comp: self._comp.CloseCompressor() self._comp = None - self._file.flush() - self._file = None + self._file.close() # # Internal methods. diff --git a/Lib/test/test_aifc.py b/Lib/test/test_aifc.py index 7e5b2e96be0..2f4350d0ea8 100644 --- a/Lib/test/test_aifc.py +++ b/Lib/test/test_aifc.py @@ -45,6 +45,21 @@ class AIFCTest(unittest.TestCase): #XXX Need more tests! + def test_close(self): + class Wrapfile(object): + def __init__(self, file): + self.file = open(file) + self.closed = False + def close(self): + self.file.close() + self.closed = True + def __getattr__(self, attr): return getattr(self.file, attr) + testfile = Wrapfile(self.sndfilepath) + f = self.f = aifc.open(testfile) + self.assertEqual(testfile.closed, False) + f.close() + self.assertEqual(testfile.closed, True) + def test_main(): run_unittest(AIFCTest) diff --git a/Misc/NEWS b/Misc/NEWS index f5c7a5e5a07..361950b95d0 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -40,6 +40,9 @@ Core and Builtins Library ------- +- Issue 5955: aifc's close method did not close the file it wrapped, + now it does. This also means getfp method now returns the real fp. + - Issue #4875: On win32, ctypes.util.find_library does no longer return directories.