[issue10041] socket.makefile(mode = 'r').readline() silently removes carriage return

kai zhu report at bugs.python.org
Fri Oct 8 05:24:47 CEST 2010


kai zhu <kaizhu256 at gmail.com> added the comment:

my bad for not rtfm, but it seems the newline argument has no effect in socket.makefile.

the TextIOWrapper signatures don't seem to match.  a hack to put newline parameter in 4th position or making it a keyword arg doesn't work either (scratch my head...)

socket.py source <line 162>
        text = io.TextIOWrapper(buffer, encoding, newline)

textio.c <line 807>
static int
textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
{
    char *kwlist[] = {"buffer", "encoding", "errors",
                      "newline", "line_buffering",
                      NULL};




$ python3 echo.py ## from previous example

$ python3 client.py 
b'hello\r\n' recv()
b'hello\r\n' makefile(mode = "rb")
'hello\n' makefile(mode = "r", newline = "")



# echo client program
data = b'hello\r\n'
import socket
clie = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
  clie.connect(('localhost', 12345))

  clie.send(data)
  data = clie.recv(4096)
  print(repr(data), 'recv()')

  clie.send(data)
  file = clie.makefile('rb')
  data = file.readline()
  print(repr(data), 'makefile(mode = "rb")')

  clie.send(data)
  file = clie.makefile('r', newline = '')
  data = file.readline()
  print(repr(data), 'makefile(mode = "r", newline = "")') ## '\r' is still silently dropped
finally:
  clie.close()

----------
resolution: invalid -> 
status: closed -> open

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10041>
_______________________________________


More information about the Python-bugs-list mailing list