mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
Issue 9795: adds context manager protocol to nntplib.NNTP class so that it can used with the 'with' statement.
This commit is contained in:
parent
4db28d3343
commit
424298a155
4 changed files with 64 additions and 0 deletions
|
@ -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__()).
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue