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.

classification
Title: Telnetlib dosn't accept u'only ascii'
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: jackdied Nosy List: christian.heimes, ezio.melotti, jackdied, laukpe, sebek
Priority: low Keywords:

Created on 2007-08-12 23:17 by laukpe, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg32633 - (view) Author: Pekka Laukkanen (laukpe) * Date: 2007-08-12 23:17
It is not possible to use unicode strings with telnetlib even if these strings used only ascii characters. Example below demonstrates this.

Type "help", "copyright", "credits" or "license" for more information.
>>> import telnetlib
>>> telnetlib.Telnet().write(u'hi')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/telnetlib.py", line 289, in write
    if IAC in buffer:
TypeError: 'in <string>' requires string as left operand


This problem is caused by bug #1772788 "chr(128) in u'only ascii' -> TypeError with misleading msg". The relevant code is following and IAC is chr(255).

  def write(self, buffer):
      if IAC in buffer:
         buffer = buffer.replace(IAC, IAC+IAC)
      self.msg("send %r", buffer)
      self.sock.sendall(buffer)


There are many pretty obvious ways to have a workaround for the issue. I suggest something like follows assuming that accepting unicode data is ok in general. If unicode is not ok then "pass" can be replaced with something like "raise TypeError('Unicode data no accepted')" to at least have a better error message.

  def write(self, buffer):
      try:
          buffer = buffer.replace(IAC, IAC+IAC)
      except UnicodeError:
          pass
      self.msg("send %r", buffer)
      self.sock.sendall(buffer)

msg59359 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-01-06 12:32
The code should raise a proper unicode error. In general network code
accepts only bytes, not unicode.
msg76464 - (view) Author: sebek (sebek) Date: 2008-11-26 13:26
I wonder if it is the same problem as bug #3725.
In this case it has been fixed in r66904 where telnetlib knows only
understand bytes instead of characters.

see http://bugs.python.org/issue3725
msg85042 - (view) Author: Jack Diederich (jackdied) * (Python committer) Date: 2009-04-01 16:22
assigning all open telnetlib items to myself
msg90966 - (view) Author: Jack Diederich (jackdied) * (Python committer) Date: 2009-07-26 23:07
Marking closed/won't fix.  ASCII strings are the byte-ish type in 2.x so
we should expect the caller to convert down from unicode when sending
bytes over the wire.
History
Date User Action Args
2022-04-11 14:56:25adminsetgithub: 45304
2009-07-26 23:07:39jackdiedsetstatus: open -> closed
resolution: wont fix
messages: + msg90966
2009-07-12 19:12:38ezio.melottisetnosy: + ezio.melotti
2009-04-01 16:22:45jackdiedsetassignee: jackdied

messages: + msg85042
nosy: + jackdied
2008-11-26 13:26:58sebeksetnosy: + sebek
messages: + msg76464
2008-01-06 12:32:05christian.heimessetversions: + Python 2.6
nosy: + christian.heimes
messages: + msg59359
priority: normal -> low
components: + Library (Lib), - None
type: enhancement
2007-08-12 23:17:18laukpecreate