summaryrefslogtreecommitdiffstats
path: root/Lib/smtplib.py
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2011-03-15 19:04:44 (GMT)
committerBarry Warsaw <barry@python.org>2011-03-15 19:04:44 (GMT)
commit1f5c958721a1f9329cb23b17cf5e36548855d72f (patch)
tree04902ee636a9b629601bca831fbf1183c2db1c70 /Lib/smtplib.py
parent5eb3591a41aa7c871f99d8bc10c09fd1a4d67479 (diff)
downloadcpython-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-xLib/smtplib.py13
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.