when does newlines get set in universal newlines mode?

Chris Angelico rosuav at gmail.com
Mon May 4 11:26:45 EDT 2015


On Tue, May 5, 2015 at 1:17 AM, Peter Otten <__peter__ at web.de> wrote:
> OK, you convinced me. Then I tried:
>
>>>> with open("tmp.txt", "wb") as f: f.write("0\r\n3\r5\n7")
> ...
>>>> assert len(open("tmp.txt", "rb").read()) == 8
>>>> f = open("tmp.txt", "rU")
>>>> f.readline()
> '0\n'
>>>> f.newlines
>>>> f.tell()
> 3
>>>> f.newlines
> '\r\n'
>
> Hm, so tell() moves the file pointer? Is that sane?

... wow. Okay! That's a bit weird.

It's possible that something's being done with internal buffering
(after all, it's horribly inefficient to *actually* read text one byte
at a time, even if that's what's happening conceptually), and that
tell() causes some checks to be done. But that really is rather
strange. I'd be interested to know what happens if another process
writes to a pipe "0\r", then sleeps while the readline() and tell()
happen, and then writes a "\n" - what will that do to newlines?

By the way, it's as well to clarify, with all these examples, what
Python version you're using. There may be significant differences.

ChrisA



More information about the Python-list mailing list