summaryrefslogtreecommitdiffstats
path: root/Lib/telnetlib.py
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2015-11-28 17:24:52 (GMT)
committerR David Murray <rdmurray@bitdance.com>2015-11-28 17:24:52 (GMT)
commit4f09806e662928c5524ab5d792d73297c50494b3 (patch)
tree74a8b786e7c43de01493bc1b84ad1e38367219c4 /Lib/telnetlib.py
parent37f54219543f9cb6ebf6a94cfe1eb402bc9b5580 (diff)
downloadcpython-4f09806e662928c5524ab5d792d73297c50494b3.zip
cpython-4f09806e662928c5524ab5d792d73297c50494b3.tar.gz
cpython-4f09806e662928c5524ab5d792d73297c50494b3.tar.bz2
#25485: Add context manager support to Telnet class.
Patch by Stéphane Wirtel.
Diffstat (limited to 'Lib/telnetlib.py')
-rw-r--r--Lib/telnetlib.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/Lib/telnetlib.py b/Lib/telnetlib.py
index 72dabc7..b0863b1 100644
--- a/Lib/telnetlib.py
+++ b/Lib/telnetlib.py
@@ -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()