[Python-checkins] r85979 - in python/branches/py3k: Doc/library/mailbox.rst Lib/mailbox.py Misc/NEWS

georg.brandl python-checkins at python.org
Sat Oct 30 16:33:28 CEST 2010


Author: georg.brandl
Date: Sat Oct 30 16:33:28 2010
New Revision: 85979

Log:
Fix test_mailbox by supporting context manager protocol for get_file() returns.

Modified:
   python/branches/py3k/Doc/library/mailbox.rst
   python/branches/py3k/Lib/mailbox.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Doc/library/mailbox.rst
==============================================================================
--- python/branches/py3k/Doc/library/mailbox.rst	(original)
+++ python/branches/py3k/Doc/library/mailbox.rst	Sat Oct 30 16:33:28 2010
@@ -180,15 +180,19 @@
    .. method:: get_file(key)
 
       Return a file-like representation of the message corresponding to *key*,
-      or raise a :exc:`KeyError` exception if no such message exists. The
-      file-like object behaves as if open in binary mode. This file should be
+      or raise a :exc:`KeyError` exception if no such message exists.  The
+      file-like object behaves as if open in binary mode.  This file should be
       closed once it is no longer needed.
 
+      .. versionadded:: 3.2
+         The file-like object supports the context manager protocol, so that
+         you can use a :keyword:`with` statement to automatically close it.
+
       .. note::
 
          Unlike other representations of messages, file-like representations are
          not necessarily independent of the :class:`Mailbox` instance that
-         created them or of the underlying mailbox. More specific documentation
+         created them or of the underlying mailbox.  More specific documentation
          is provided by each subclass.
 
 

Modified: python/branches/py3k/Lib/mailbox.py
==============================================================================
--- python/branches/py3k/Lib/mailbox.py	(original)
+++ python/branches/py3k/Lib/mailbox.py	Sat Oct 30 16:33:28 2010
@@ -1827,6 +1827,8 @@
 
     def close(self):
         """Close the file."""
+        if hasattr(self._file, 'close'):
+            self._file.close()
         del self._file
 
     def _read(self, size, read_method):
@@ -1838,6 +1840,13 @@
         self._pos = self._file.tell()
         return result
 
+    def __enter__(self):
+        """Context manager protocol support."""
+        return self
+
+    def __exit__(self, *exc):
+        self.close()
+
 
 class _PartialFile(_ProxyFile):
     """A read-only wrapper of part of a file."""
@@ -1871,6 +1880,11 @@
             size = remaining
         return _ProxyFile._read(self, size, read_method)
 
+    def close(self):
+        # do *not* close the underlying file object for partial files,
+        # since it's global to the mailbox object
+        del self._file
+
 
 def _lock_file(f, dotlock=True):
     """Lock file f using lockf and dot locking."""

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sat Oct 30 16:33:28 2010
@@ -57,6 +57,9 @@
 Library
 -------
 
+- Support context manager protocol for file-like objects returned by
+  mailbox ``get_file()`` methods.
+
 - Issue #10246: uu.encode didn't close file objects explicitly when filenames
   were given to it.  Patch by Brian Brazil.
 


More information about the Python-checkins mailing list