imaplib.IMAP4 now supports the context manager protocol.

Original patch by Tarek Ziadé.
This commit is contained in:
Serhiy Storchaka 2014-09-09 19:07:49 +03:00
parent bb1e3f1ebe
commit 38684c3663
5 changed files with 67 additions and 0 deletions

View file

@ -238,6 +238,14 @@ class IMAP4:
return getattr(self, attr.lower())
raise AttributeError("Unknown IMAP4 command: '%s'" % attr)
def __enter__(self):
return self
def __exit__(self, *args):
try:
self.logout()
except OSError:
pass
# Overridable methods