This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: telnetlib.Telnet.write gives confusing error message when a string is passed in
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: John Hagen, berker.peksag, corona10, jackdied, r.david.murray
Priority: normal Keywords:

Created on 2017-02-22 16:35 by John Hagen, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg288371 - (view) Author: John Hagen (John Hagen) * Date: 2017-02-22 16:35
I was recently helping someone learn Python 3 from Python 2 and they were confused by error resulting from the following easy-to-make mistake:

import telnetlib

tn = telnetlib.Telnet('localhost')

Traceback (most recent call last):
  File "/Users/user/PycharmProjects/python-test/", line 4, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/", line 287, in write
    if IAC in buffer:
TypeError: 'in <string>' requires string as left operand, not bytes

What is confusing is that the solution is to pass in `bytes`, not a `str`, but the place where this manifests is in an `in` operator with the first argument as a `bytes` (telnetlib.IAC).

Perhaps a simple isinstance(buffer, bytes) could be used to throw a `TypeError` or something a bit more helpful.

It also doesn't help that for some reason Googling "python telnet" only seems to bring up the Python 2 docs link, rather than Python 3 or both.
msg292500 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2017-04-28 01:48
I submitted PR to handle this issue like this way.

tn.write([b'data sample without IAC'])
File "/Users/corona10/cpython/Lib/", line 288, in write
    raise TypeError('%s must be a bytes-like type.' % buffer)
TypeError: [b'data sample without IAC'] must be a bytes-like type.
msg292501 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-04-28 02:03
We generally don't do this kind of type checking in the standard library.  The message may not be clear, but it is accurate and decipherable, especially given the fact that the write parameter name is 'buffer'.  I don't think anything should be done here.  If you want type checking use the types module :)

Thanks for the PR, @corona10, but I think we should just close this issue.  Other devs may disagree, though, so I'm not closing it myself.

On the other hand, enhancing telnetlib to also work with strings might be a nice project for someone.  It would require some design discussion, though, so I don't think anyone should hop on it without discussing it somewhere first (python-ideas, maybe?).
msg292502 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2017-04-28 02:05
Thanks for the response. I agree with you. :)
msg293046 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-05-05 06:35
I agree with David. The documentation of the method starts with "Write a byte string to the socket [...]" and the example at is pretty clear.
Date User Action Args
2022-04-11 14:58:43adminsetgithub: 73807
2017-05-05 06:35:59berker.peksagsetstatus: open -> closed

nosy: + berker.peksag
messages: + msg293046

resolution: rejected
stage: resolved
2017-04-28 02:06:42corona10setpull_requests: - pull_request1429
2017-04-28 02:05:47corona10setmessages: + msg292502
2017-04-28 02:03:20r.david.murraysetnosy: + jackdied, r.david.murray
messages: + msg292501
2017-04-28 01:48:15corona10setnosy: + corona10
messages: + msg292500
2017-04-27 14:57:18corona10setpull_requests: + pull_request1429
2017-02-22 16:35:21John Hagencreate