diff -r f6c4c178da56 Lib/aifc.py --- a/Lib/aifc.py Sun Nov 11 14:01:23 2012 +0100 +++ b/Lib/aifc.py Fri Nov 16 14:52:48 2012 +0200 @@ -732,22 +732,28 @@ self._patchheader() def close(self): - self._ensure_header_written(0) - if self._datawritten & 1: - # quick pad to even size - self._file.write(chr(0)) - self._datawritten = self._datawritten + 1 - self._writemarkers() - if self._nframeswritten != self._nframes or \ - self._datalength != self._datawritten or \ - self._marklength: - self._patchheader() - if self._comp: - self._comp.CloseCompressor() - self._comp = None - # Prevent ref cycles - self._convert = None - self._file.close() + if self._file is None: + return + try: + self._ensure_header_written(0) + if self._datawritten & 1: + # quick pad to even size + self._file.write(chr(0)) + self._datawritten = self._datawritten + 1 + self._writemarkers() + if self._nframeswritten != self._nframes or \ + self._datalength != self._datawritten or \ + self._marklength: + self._patchheader() + if self._comp: + self._comp.CloseCompressor() + self._comp = None + finally: + # Prevent ref cycles + self._convert = None + f = self._file + self._file = None + f.close() # # Internal methods. diff -r f6c4c178da56 Lib/test/test_aifc.py --- a/Lib/test/test_aifc.py Sun Nov 11 14:01:23 2012 +0100 +++ b/Lib/test/test_aifc.py Fri Nov 16 14:52:48 2012 +0200 @@ -40,6 +40,13 @@ self.assertEqual(f.getcompname(), 'not compressed') self.assertEqual(f.getparams(), (2, 2, 48000, 14400, 'NONE', 'not compressed')) + def test_close(self): + fout = aifc.open(TESTFN, 'wb') + with self.assertRaises(aifc.Error): + fout.close() + self.assertIsNone(fout.getfp()) + fout.close() # do nothing + def test_read(self): f = self.f = aifc.open(self.sndfilepath) self.assertEqual(f.tell(), 0)