[Python-Dev] [python] Re: New lines, carriage returns, and Windows (original) (raw)

Michael Foord fuzzyman at voidspace.org.uk
Thu Sep 27 00:22:52 CEST 2007


Dino Viehland wrote:

And if this is fine for you, given that you may have the largest WinForms / IronPython code base, I tend to think the replace may be reasonable. But we have had someone get surprised by this behavior.

It is a slight impedance mismatch between Python and Windows - but isn't restricted to IronPython, so changing Python semantics doesn't seem like the right answer.

Alternatively a more intelligent text mode (that writes '\n' as '\r\n' and '\r\n' as '\r\n' on Windows) doesn't sound like such a bad idea - but you will still get caught out by this. A string read in text mode
will read '\r\n' as '\n'. Setting this on a winforms component will still do the wrong thing. Better to be aware of the difference and use binary mode.

Michael

-----Original Message----- From: Michael Foord [mailto:fuzzyman at voidspace.org.uk] Sent: Wednesday, September 26, 2007 3:15 PM To: Dino Viehland Cc: python-dev at python.org Subject: Re: [python] Re: [Python-Dev] New lines, carriage returns, and Windows

Dino Viehland wrote:

My understanding is that users can write code that uses only \n and Python will write the end-of-line character(s) that are appropriate for the platform when writing to a file. That's what I meant by uses \n for everything internally.

But if you write \r\n to a file Python completely ignores the presence of the \r and transforms the \n into a \r\n anyway, hence the \r\r in the resulting stream. My last question is simply does anyone find writing \r\r\n when the original string contained \r\n a useful behavior - personally I don't see how it is. But Guido's response makes this sound like it's a problem w/ VC++ stdio implementation and not something that Python is explicitly doing. Anyway, it'd might be useful to have a text-mode file that you can write \r\n to and only get \r\n in the resulting file. But if the general sentiment is s.replace('\r', '') is the way to go we can advice our users of the behavior when interoperating w/ APIs that return \r\n in strings.

We always do replace('\r\n','\n') but same difference... Michael -----Original Message----- From: "Martin v. Löwis" [mailto:martin at v.loewis.de] Sent: Wednesday, September 26, 2007 3:01 PM To: Dino Viehland Cc: python-dev at python.org Subject: Re: [Python-Dev] New lines, carriage returns, and Windows

This works great as long as you stay within an entirely Python world. Because Python uses \n for everything internally I think you misunderstand fairly significantly how this all works together. Python does not use \n "for everything internally". Python is well capable of representing \r separately, and does so if you ask it to. So I'm curious: Is there a reason this behavior is useful that I'm missing? I think you are missing how it works in the first place (or else you failed to communicate to me what precise behavior you find puzzling). Regards, Martin


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk



More information about the Python-Dev mailing list