Skip to content

Commit a8031c2

Browse files
committed
bpo-29621: telnetlib.write() raise TypeError if recv non-bytes type.
If telnetlib.write() receive non-byte-like bytes then raise TypeError.
1 parent 5a4e3d8 commit a8031c2

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

Lib/telnetlib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ def write(self, buffer):
284284
OSError if the connection is closed.
285285
286286
"""
287+
if not isinstance(buffer, (bytes, bytearray)):
288+
raise TypeError('%s must be a bytes-like type.' % buffer)
287289
if IAC in buffer:
288290
buffer = buffer.replace(IAC, IAC+IAC)
289291
self.msg("send %r", buffer)

Lib/test/test_telnetlib.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,17 @@ def test_write(self):
304304
written = b''.join(telnet.sock.writes)
305305
self.assertEqual(data.replace(tl.IAC,tl.IAC+tl.IAC), written)
306306

307+
308+
def test_write_type_error(self):
309+
non_bytes_data_sample = ['data sample with string',
310+
[b'data sample without IAC'],
311+
[b'data sample with', tl.IAC, b' one IAC'],
312+
(b'data sample with', tl.IAC ,b' one IAC'),
313+
'']
314+
for data in non_bytes_data_sample:
315+
telnet = test_telnet()
316+
self.assertRaises(TypeError, telnet.write, data)
317+
307318
class OptionTests(unittest.TestCase):
308319
# RFC 854 commands
309320
cmds = [tl.AO, tl.AYT, tl.BRK, tl.EC, tl.EL, tl.GA, tl.IP, tl.NOP]

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ Extension Modules
317317
Library
318318
-------
319319

320+
- bpo-29621: If telnetlib.write() receive non-byte-like type
321+
then raise TypeError. Patched by Dong-hee Na.
322+
320323
- bpo-30101: Add support for curses.A_ITALIC.
321324

322325
- bpo-29822: inspect.isabstract() now works during __init_subclass__. Patch

0 commit comments

Comments
 (0)