summaryrefslogtreecommitdiffstats
path: root/Lib/nntplib.py
diff options
context:
space:
mode:
authorGiampaolo RodolĂ  <g.rodola@gmail.com>2011-03-03 18:34:06 (GMT)
committerGiampaolo RodolĂ  <g.rodola@gmail.com>2011-03-03 18:34:06 (GMT)
commit424298a155b5cc652ce1e539a1fedb658fba9cd1 (patch)
tree429c5ddf30e4c2cfcc9609fa8e5a2209de686127 /Lib/nntplib.py
parent4db28d3343da7e48946a62036058fc6f0ee7cd71 (diff)
downloadcpython-424298a155b5cc652ce1e539a1fedb658fba9cd1.zip
cpython-424298a155b5cc652ce1e539a1fedb658fba9cd1.tar.gz
cpython-424298a155b5cc652ce1e539a1fedb658fba9cd1.tar.bz2
Issue 9795: adds context manager protocol to nntplib.NNTP class so that it can used with the 'with' statement.
Diffstat (limited to 'Lib/nntplib.py')
-rw-r--r--Lib/nntplib.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/Lib/nntplib.py b/Lib/nntplib.py
index 70a75b6..ba70192 100644
--- a/Lib/nntplib.py
+++ b/Lib/nntplib.py
@@ -346,6 +346,20 @@ class _NNTPBase:
# Log in and encryption setup order is left to subclasses.
self.authenticated = False
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ is_connected = lambda: hasattr(self, "file")
+ if is_connected():
+ try:
+ self.quit()
+ except (socket.error, EOFError):
+ pass
+ finally:
+ if is_connected():
+ self._close()
+
def getwelcome(self):
"""Get the welcome message from the server
(this is read and squirreled away by __init__()).