[Python-checkins] cpython: #25485: Add context manager support to Telnet class.

r.david.murray python-checkins at python.org
Sat Nov 28 12:25:11 EST 2015


https://hg.python.org/cpython/rev/277824f2d133
changeset: 99380:277824f2d133
parent: 99377:9fcfdb53e8af
user: R David Murray <rdmurray at bitdance.com>
date: Sat Nov 28 12:24:52 2015 -0500
summary:
 #25485: Add context manager support to Telnet class.
Patch by Stéphane Wirtel.
files:
 Doc/library/telnetlib.rst | 11 +++++++++++
 Doc/whatsnew/3.6.rst | 7 +++++++
 Lib/telnetlib.py | 15 ++++++++++-----
 Lib/test/test_telnetlib.py | 5 +++++
 Misc/NEWS | 2 ++
 5 files changed, 35 insertions(+), 5 deletions(-)
diff --git a/Doc/library/telnetlib.rst b/Doc/library/telnetlib.rst
--- a/Doc/library/telnetlib.rst
+++ b/Doc/library/telnetlib.rst
@@ -43,6 +43,17 @@
 :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::
 
diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst
--- a/Doc/whatsnew/3.6.rst
+++ b/Doc/whatsnew/3.6.rst
@@ -125,6 +125,13 @@
 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
 ------------------
 
diff --git a/Lib/telnetlib.py b/Lib/telnetlib.py
--- a/Lib/telnetlib.py
+++ b/Lib/telnetlib.py
@@ -637,6 +637,12 @@
 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 @@
 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()
diff --git a/Lib/test/test_telnetlib.py b/Lib/test/test_telnetlib.py
--- a/Lib/test/test_telnetlib.py
+++ b/Lib/test/test_telnetlib.py
@@ -42,6 +42,11 @@
 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)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@
 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__.
 
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /