cpython: b89d193cbca5 (original) (raw)
Mercurial > cpython
changeset 70856:b89d193cbca5 2.7
#11700: proxy object close methods can now be called multiple times This makes them work like the close provided by regular file objects. This patch also backports the close-the-underlying-file code for _ProxyFile objects that was introduced along with context manager support in the 3.x branch. [#11700]
R David Murray rdmurray@bitdance.com | |
---|---|
date | Fri, 17 Jun 2011 22:23:04 -0400 |
parents | 18e6ccc332d5 |
children | dc78ab3f7bc5 |
files | Lib/mailbox.py Lib/test/test_mailbox.py Misc/NEWS |
diffstat | 3 files changed, 22 insertions(+), 2 deletions(-)[+] [-] Lib/mailbox.py 11 Lib/test/test_mailbox.py 10 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -1854,7 +1854,10 @@ class _ProxyFile: def close(self): """Close the file."""
del self._file[](#l1.7)
if hasattr(self, '_file'):[](#l1.8)
if hasattr(self._file, 'close'):[](#l1.9)
self._file.close()[](#l1.10)
del self._file[](#l1.11)
def _read(self, size, read_method): """Read size bytes using read_method.""" @@ -1898,6 +1901,12 @@ class _PartialFile(_ProxyFile): size = remaining return _ProxyFile._read(self, size, read_method)
- def close(self):
# do *not* close the underlying file object for partial files,[](#l1.20)
# since it's global to the mailbox object[](#l1.21)
if hasattr(self, '_file'):[](#l1.22)
del self._file[](#l1.23)
+ def _lock_file(f, dotlock=True): """Lock file f using lockf and dot locking."""
--- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -174,6 +174,13 @@ class TestMailbox(TestBase): self.assertEqual(self._box.get_file(key1).read().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.iterkeys, do_keys=True, do_values=False) @@ -1670,7 +1677,8 @@ class TestProxyFileBase(TestBase): def _test_close(self, proxy): # Close a file proxy.close()
self.assertRaises(AttributeError, lambda: proxy.close())[](#l2.21)
# Issue 11700 subsequent closes should be a no-op, not an error.[](#l2.22)
proxy.close()[](#l2.23)
class TestProxyFile(TestProxyFileBase):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -16,6 +16,9 @@ Core and Builtins Library ------- +- Issue #11700: mailbox proxy object close methods can now be called multiple