cpython: 8319db2dd342 (original) (raw)
Mercurial > cpython
changeset 70857:8319db2dd342 3.2
#11700: proxy object close methods can now be called multiple times This makes them work like the close provided by regular file objects. [#11700]
R David Murray rdmurray@bitdance.com | |
---|---|
date | Fri, 17 Jun 2011 22:24:05 -0400 |
parents | fea1920ae75f |
children | 0705b9037b20 d62e5682a8ac |
files | Lib/mailbox.py Lib/test/test_mailbox.py Misc/NEWS |
diffstat | 3 files changed, 25 insertions(+), 5 deletions(-)[+] [-] Lib/mailbox.py 14 Lib/test/test_mailbox.py 13 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -1923,9 +1923,10 @@ class _ProxyFile: def close(self): """Close the file."""
if hasattr(self._file, 'close'):[](#l1.7)
self._file.close()[](#l1.8)
del self._file[](#l1.9)
if hasattr(self, '_file'):[](#l1.10)
if hasattr(self._file, 'close'):[](#l1.11)
self._file.close()[](#l1.12)
del self._file[](#l1.13)
def _read(self, size, read_method): """Read size bytes using read_method.""" @@ -1957,6 +1958,10 @@ class _ProxyFile: @property def closed(self):
if not hasattr(self, '_file'):[](#l1.21)
return True[](#l1.22)
if not hasattr(self._file, 'closed'):[](#l1.23)
return False[](#l1.24) return self._file.closed[](#l1.25)
@@ -1995,7 +2000,8 @@ class _PartialFile(_ProxyFile): def close(self): # do not close the underlying file object for partial files, # since it's global to the mailbox object
del self._file[](#l1.32)
if hasattr(self, '_file'):[](#l1.33)
del self._file[](#l1.34)
def _lock_file(f, dotlock=True):
--- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -297,6 +297,13 @@ class TestMailbox(TestBase): self.assertEqual(data1.decode('ascii').replace(os.linesep, '\n'), _sample_message)
- def test_get_file_can_be_closed_twice(self):
# Issue 11700[](#l2.8)
key = self._box.add(_sample_message)[](#l2.9)
f = self._box.get_file(key)[](#l2.10)
f.close()[](#l2.11)
f.close()[](#l2.12)
+ def test_iterkeys(self): # Get keys using iterkeys() self._check_iteration(self._box.keys, do_keys=True, do_values=False) @@ -1862,8 +1869,12 @@ class TestProxyFileBase(TestBase): def _test_close(self, proxy): # Close a file
self.assertFalse(proxy.closed)[](#l2.21) proxy.close()[](#l2.22)
self.assertRaises(AttributeError, lambda: proxy.close())[](#l2.23)
self.assertTrue(proxy.closed)[](#l2.24)
# Issue 11700 subsequent closes should be a no-op.[](#l2.25)
proxy.close()[](#l2.26)
self.assertTrue(proxy.closed)[](#l2.27)
class TestProxyFile(TestProxyFileBase):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -25,6 +25,9 @@ Core and Builtins Library ------- +- Issue #11700: mailbox proxy object close methods can now be called multiple