[Python-checkins] python/dist/src/Lib codecs.py,,

doerwalter at users.sourceforge.net doerwalter at users.sourceforge.net
Mon Apr 4 23:56:30 CEST 2005

Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15351/Lib

Modified Files:
      Tag: release24-maint
Log Message:
Backport checkin:
Fix for SF bug #1175396: readline() will now read one more character, if
the last character read is "\r" (and size is None, i.e. we're allowed to
call read() multiple times), so that we can return the correct line ending
(this additional character might be a "\n").

If the stream is temporarily exhausted, we might return the wrong line ending
(if the last character read is "\r" and the next one (after the byte stream
provides more data) is "\n", but at least the atcr member ensures that we
get the correct number of lines (i.e. this "\n" will not be treated as
another line ending).

Index: codecs.py
RCS file: /cvsroot/python/python/dist/src/Lib/codecs.py,v
retrieving revision
retrieving revision
diff -u -d -r1.35.2.5 -r1.35.2.6
--- codecs.py	14 Mar 2005 19:26:47 -0000
+++ codecs.py	4 Apr 2005 21:56:27 -0000
@@ -310,6 +310,15 @@
                 data = data[1:]
             if data:
                 self.atcr = data.endswith(u"\r")
+                # If we're at a "\r" (and are allowed to read more), read one
+                # extra character (which might be a "\n") to get a proper
+                # line ending. (If the stream is temporarily exhausted we return
+                # the wrong line ending, but at least we won't generate a bogus
+                # second line.)
+                if self.atcr and size is None:
+                    data += self.read(size=1, chars=1)
+                    self.atcr = data.endswith(u"\r")
             line += data
             lines = line.splitlines(True)
             if lines:

More information about the Python-checkins mailing list