#25485: Add context manager support to Telnet class.

Patch by Stéphane Wirtel.
This commit is contained in:
R David Murray 2015-11-28 12:24:52 -05:00
parent 37f5421954
commit 4f09806e66
5 changed files with 35 additions and 5 deletions

View file

@ -637,6 +637,12 @@ class Telnet:
raise EOFError
return (-1, None, text)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
def test():
"""Test program for telnetlib.
@ -660,11 +666,10 @@ def test():
port = int(portstr)
except ValueError:
port = socket.getservbyname(portstr, 'tcp')
tn = Telnet()
tn.set_debuglevel(debuglevel)
tn.open(host, port, timeout=0.5)
tn.interact()
tn.close()
with Telnet() as tn:
tn.set_debuglevel(debuglevel)
tn.open(host, port, timeout=0.5)
tn.interact()
if __name__ == '__main__':
test()