]> granicus.if.org Git - python/commitdiff
#25485: Add context manager support to Telnet class.
authorR David Murray <rdmurray@bitdance.com>
Sat, 28 Nov 2015 17:24:52 +0000 (12:24 -0500)
committerR David Murray <rdmurray@bitdance.com>
Sat, 28 Nov 2015 17:24:52 +0000 (12:24 -0500)
Patch by Stéphane Wirtel.

Doc/library/telnetlib.rst
Doc/whatsnew/3.6.rst
Lib/telnetlib.py
Lib/test/test_telnetlib.py
Misc/NEWS

index 4040f72ee8665deb679738430d8863ed696c9e38..ce406ca2e60323039e4eed085e59f8ba6f80c340 100644 (file)
@@ -43,6 +43,17 @@ Character), EL (Erase Line), GA (Go Ahead), SB (Subnegotiation Begin).
    :exc:`EOFError` when the end of the connection is read, because they can return
    an empty string for other reasons.  See the individual descriptions below.
 
+   A :class:`Telnet` object is a context manager and can be used in a
+   :keyword:`with` statement.  When the :keyword:`with` block ends, the
+   :meth:`close` method is called::
+
+       >>> from telnetlib import Telnet
+       >>> with Telnet('localhost', 23) as tn:
+       ...     tn.interact()
+       ...
+
+   .. versionchanged:: 3.6 Context manager support added
+
 
 .. seealso::
 
index 172b253e43bf57d58bd5189970c7763fc932d976..fc32fb5926b207c548d04bf8750b75ebc0831048 100644 (file)
@@ -125,6 +125,13 @@ Previously, names of properties and slots which were not yet created on
 an instance were excluded.  (Contributed by Martin Panter in :issue:`25590`.)
 
 
+telnetlib
+---------
+
+:class:`~telnetlib.Telnet` is now a context manager (contributed by
+Stéphane Wirtel in :issue:`25485`).
+
+
 urllib.robotparser
 ------------------
 
index 72dabc76e0cd4895e083e80cfac604d547c833f2..b0863b1cbd60344254992ba11aad25eb973e63b1 100644 (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()
index 524bba37b363a5f51e7d7097bef1e8589358a519..23029e0c6f73e28f1a7825f0bf2270041d62f9ab 100644 (file)
@@ -42,6 +42,11 @@ class GeneralTests(TestCase):
         telnet = telnetlib.Telnet(HOST, self.port)
         telnet.sock.close()
 
+    def testContextManager(self):
+        with telnetlib.Telnet(HOST, self.port) as tn:
+            self.assertIsNotNone(tn.get_socket())
+        self.assertIsNone(tn.get_socket())
+
     def testTimeoutDefault(self):
         self.assertTrue(socket.getdefaulttimeout() is None)
         socket.setdefaulttimeout(30)
index c3cce96dc9da732c8cca92612d5be3c9b937e28a..2cb06e9e8ea2afcdceedf15c1babc4516738454b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ Release date: XXXX-XX-XX
 Core and Builtins
 -----------------
 
+- Issue #25485: telnetlib.Telnet is now a context manager.
+
 - Issue #24097: Fixed crash in object.__reduce__() if slot name is freed inside
   __getattr__.