diff options
author | Barry Warsaw <barry@python.org> | 2011-03-15 19:04:44 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 2011-03-15 19:04:44 (GMT) |
commit | 1f5c958721a1f9329cb23b17cf5e36548855d72f (patch) | |
tree | 04902ee636a9b629601bca831fbf1183c2db1c70 /Lib/smtplib.py | |
parent | 5eb3591a41aa7c871f99d8bc10c09fd1a4d67479 (diff) | |
download | cpython-1f5c958721a1f9329cb23b17cf5e36548855d72f.zip cpython-1f5c958721a1f9329cb23b17cf5e36548855d72f.tar.gz cpython-1f5c958721a1f9329cb23b17cf5e36548855d72f.tar.bz2 |
- Issue #11289: `smtp.SMTP` class becomes a context manager so it can be used
in a `with` statement. Contributed by Giampaolo Rodola.
Diffstat (limited to 'Lib/smtplib.py')
-rwxr-xr-x | Lib/smtplib.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/Lib/smtplib.py b/Lib/smtplib.py index 14e6250..213138c 100755 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -269,6 +269,19 @@ class SMTP: pass self.local_hostname = '[%s]' % addr + def __enter__(self): + return self + + def __exit__(self, *args): + try: + code, message = self.docmd("QUIT") + if code != 221: + raise SMTPResponseException(code, message) + except SMTPServerDisconnected: + pass + finally: + self.close() + def set_debuglevel(self, debuglevel): """Set the debug output level. |