summaryrefslogtreecommitdiffstats
path: root/Lib/mailbox.py
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2010-10-30 14:33:28 (GMT)
committerGeorg Brandl <georg@python.org>2010-10-30 14:33:28 (GMT)
commit6ce29fa7a89cd266ca9dd3e21ed40f9100eb70f6 (patch)
tree0a848c45edb3c61a90c828794897a7b455bc53c4 /Lib/mailbox.py
parent849e12bfe943be4f18f5d0e21087b1cb08d472dd (diff)
downloadcpython-6ce29fa7a89cd266ca9dd3e21ed40f9100eb70f6.zip
cpython-6ce29fa7a89cd266ca9dd3e21ed40f9100eb70f6.tar.gz
cpython-6ce29fa7a89cd266ca9dd3e21ed40f9100eb70f6.tar.bz2
Fix test_mailbox by supporting context manager protocol for get_file() returns.
Diffstat (limited to 'Lib/mailbox.py')
-rw-r--r--Lib/mailbox.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/Lib/mailbox.py b/Lib/mailbox.py
index e6f1735..520463a 100644
--- a/Lib/mailbox.py
+++ b/Lib/mailbox.py
@@ -1827,6 +1827,8 @@ class _ProxyFile:
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 @@ class _ProxyFile:
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 @@ 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,
+ # 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."""