Issue 9795: adds context manager protocol to nntplib.NNTP class so that it can used with the 'with' statement.

This commit is contained in:
Giampaolo Rodolà 2011-03-03 18:34:06 +00:00
parent 4db28d3343
commit 424298a155
4 changed files with 64 additions and 0 deletions

View file

@ -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__()).