commit | 38684c366364fcd4071534a1de62f728407224d8 | [log] [tgz] |
---|---|---|
author | Serhiy Storchaka <storchaka@gmail.com> | Tue Sep 09 19:07:49 2014 +0300 |
committer | Serhiy Storchaka <storchaka@gmail.com> | Tue Sep 09 19:07:49 2014 +0300 |
tree | e16b93cf0f5a416770a873331792e5d4d6611b9c | |
parent | bb1e3f1ebe99e1cb5a7c136991b8e8f41e4fa4bb [diff] [blame] |
imaplib.IMAP4 now supports the context manager protocol. Original patch by Tarek Ziadé.
diff --git a/Lib/imaplib.py b/Lib/imaplib.py index ad104fe..27445dd 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py
@@ -238,6 +238,14 @@ 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