diff --git a/Lib/mailbox.py b/Lib/mailbox.py --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -1919,9 +1919,9 @@ def close(self): """Close the file.""" - if hasattr(self._file, 'close'): + if hasattr(self, '_file'): self._file.close() - del self._file + del self._file def _read(self, size, read_method): """Read size bytes using read_method.""" @@ -1953,7 +1953,7 @@ @property def closed(self): - return self._file.closed + return not hasattr(self, '_file') class _PartialFile(_ProxyFile): @@ -1991,7 +1991,8 @@ def close(self): # do *not* close the underlying file object for partial files, # since it's global to the mailbox object - del self._file + if hasattr(self, '_file'): + del self._file def _lock_file(f, dotlock=True): diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -1834,9 +1834,13 @@ def _test_close(self, proxy): # Close a file + self.assertFalse(proxy.closed) proxy.close() - self.assertRaises(AttributeError, lambda: proxy.close()) - + self.assertTrue(proxy.closed) + try: + proxy.close() + except: + self.fail('Proxy.close() failure') class TestProxyFile(TestProxyFileBase):