Skip to content

Commit 4f09806

Browse files
committed
#25485: Add context manager support to Telnet class.
Patch by Stéphane Wirtel.
1 parent 37f5421 commit 4f09806

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

Doc/library/telnetlib.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ Character), EL (Erase Line), GA (Go Ahead), SB (Subnegotiation Begin).
4343
:exc:`EOFError` when the end of the connection is read, because they can return
4444
an empty string for other reasons. See the individual descriptions below.
4545

46+
A :class:`Telnet` object is a context manager and can be used in a
47+
:keyword:`with` statement. When the :keyword:`with` block ends, the
48+
:meth:`close` method is called::
49+
50+
>>> from telnetlib import Telnet
51+
>>> with Telnet('localhost', 23) as tn:
52+
... tn.interact()
53+
...
54+
55+
.. versionchanged:: 3.6 Context manager support added
56+
4657

4758
.. seealso::
4859

Doc/whatsnew/3.6.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ Previously, names of properties and slots which were not yet created on
125125
an instance were excluded. (Contributed by Martin Panter in :issue:`25590`.)
126126

127127

128+
telnetlib
129+
---------
130+
131+
:class:`~telnetlib.Telnet` is now a context manager (contributed by
132+
Stéphane Wirtel in :issue:`25485`).
133+
134+
128135
urllib.robotparser
129136
------------------
130137

Lib/telnetlib.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,12 @@ def expect(self, list, timeout=None):
637637
raise EOFError
638638
return (-1, None, text)
639639

640+
def __enter__(self):
641+
return self
642+
643+
def __exit__(self, type, value, traceback):
644+
self.close()
645+
640646

641647
def test():
642648
"""Test program for telnetlib.
@@ -660,11 +666,10 @@ def test():
660666
port = int(portstr)
661667
except ValueError:
662668
port = socket.getservbyname(portstr, 'tcp')
663-
tn = Telnet()
664-
tn.set_debuglevel(debuglevel)
665-
tn.open(host, port, timeout=0.5)
666-
tn.interact()
667-
tn.close()
669+
with Telnet() as tn:
670+
tn.set_debuglevel(debuglevel)
671+
tn.open(host, port, timeout=0.5)
672+
tn.interact()
668673

669674
if __name__ == '__main__':
670675
test()

Lib/test/test_telnetlib.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ def testBasic(self):
4242
telnet = telnetlib.Telnet(HOST, self.port)
4343
telnet.sock.close()
4444

45+
def testContextManager(self):
46+
with telnetlib.Telnet(HOST, self.port) as tn:
47+
self.assertIsNotNone(tn.get_socket())
48+
self.assertIsNone(tn.get_socket())
49+
4550
def testTimeoutDefault(self):
4651
self.assertTrue(socket.getdefaulttimeout() is None)
4752
socket.setdefaulttimeout(30)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Release date: XXXX-XX-XX
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #25485: telnetlib.Telnet is now a context manager.
14+
1315
- Issue #24097: Fixed crash in object.__reduce__() if slot name is freed inside
1416
__getattr__.
1517

0 commit comments

Comments
 (0)