You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Newline for print() is \n on Windows, and not \r\n as expected #57328
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2012-08-16.06:58:03.911>created_at=<Date2011-10-06.20:59:47.493>labels= ['type-bug', 'OS-windows', 'release-blocker']
title='Newline for print() is \\n on Windows, and not \\r\\n as expected'updated_at=<Date2012-08-16.06:58:03.909>user='https://bugs.python.org/MZ'
To people who open the file in their browser: text files are very similar, but newline_3.1.txt has CRLF line endings and newline_3.2.txt has LF line endings.
M.Z, how did you obtain them? did you start a subprocess?
MZ commented on Oct 7, 2011
MZmannequin
MannequinAuthor
More actions
Hi Amaury,
The two text files were obtained through redirection in Windows, so I simply ran the newline.py file with:
On Windows, set the binary mode on stdin, stdout, stderr and all
io.FileIO objects (to not translate newlines, \r\n <=> \n). The Python parser
translates newlines (\r\n => \n).
print() uses PyFile_WriteString("\n", file) by default (if the end argument is not set) to write the newline. TextIOWrapper.write("\n") replaces "\n" by TextIOWrapper._writenl.
On Windows, stdin, stdout and stderr are creates using TextIOWrapper(..., newline=None). In this case, TextIOWrapper._writenl is os.linesep and so '\r\n'.
To sum up, print() writes '\n' into sys.stdout, but sys.stdout write b'\r\n' into the file descriptor 1 which is a binary file (ie. the underlying OS file doesn't translate newlines).
If the output is redirected (e.g. into a file), TextIOWrapper is created with line_buffering=False.
You may try to force line_buffering=True when the output is redirected.
If the output is redirected (e.g. into a file),
TextIOWrapper is created with line_buffering=False.
How does this affect the \r\n translation?
MZ commented on Oct 10, 2011
MZmannequin
MannequinAuthor
More actions
Just to make it clear: I have not observed any problems on the Windows terminal (cmd) with \n newline, but at least Notepad does not break lines correctly if only \n is used.
atsuoishimoto commented on Aug 1, 2012
atsuoishimotomannequin
Mannequin
More actions
I found 'more' command in Windows7 requires \r\n.
Python 2.7.3:
C:\>python -c "for i in range(5):print(i)"|more
0
1
2
3
4
Python 3.3(trunk):
c:\src\cpython\PCbuild>python -c "for i in range(5):print(i)"|more
?????
On Windows, stdin, stdout and stderr are creates using TextIOWrapper(..., newline=None).
In this case, TextIOWrapper._writenl is os.linesep and so '\r\n'.
Oh, I was wrong: stdin is created with newline=None, but stdout and stderr are created with newline="\n" and so "\n" is not translated to "\r\n".
I checked in Python 2.7: print("abc") and sys.stdout.write("abc\n") writes b"abc\r\n" into the output file (when the output is redirected), but sys.stdout.write("abc\r\n") writes b"abc\r\r\n". Python 3.3 should do the same: \r\n is preferred on Windows (ex: notepad doesn't support UNIX line ending, \n).
Attached patch changes line ending for stdout and stderr on Windows: translate "\n" to "\r\n".
It would be nice to fix this before Python 3.3 final.
Activity
MZ commentedon Oct 6, 2011
In 3.2.2 the newline for print() is \n on Windows, and not \r\n as expected.
In 3.1.4 the newline is \r\n.
OS is Win 7, and tried on both 32 and 64 bit.
Small example with output is attached.
amauryfa commentedon Oct 6, 2011
To people who open the file in their browser: text files are very similar, but newline_3.1.txt has CRLF line endings and newline_3.2.txt has LF line endings.
M.Z, how did you obtain them? did you start a subprocess?
MZ commentedon Oct 7, 2011
Hi Amaury,
The two text files were obtained through redirection in Windows, so I simply ran the newline.py file with:
Best regards,
Morten Zilmer
vstinner commentedon Oct 7, 2011
I changed how newlines are handled on Windows to fix an issue with CGI: see the issue bpo-10841.
changeset: 67431:0933c3753a71
user: Victor Stinner <victor.stinner@haypocalc.com>
date: Fri Jan 07 18:47:22 2011 +0000
files: Misc/NEWS Modules/_io/fileio.c Modules/main.c Parser/tokenizer.c
description:
Issue bpo-10841: set binary mode on files; the parser translates newlines
On Windows, set the binary mode on stdin, stdout, stderr and all
io.FileIO objects (to not translate newlines, \r\n <=> \n). The Python parser
translates newlines (\r\n => \n).
vstinner commentedon Oct 7, 2011
print() uses PyFile_WriteString("\n", file) by default (if the end argument is not set) to write the newline. TextIOWrapper.write("\n") replaces "\n" by TextIOWrapper._writenl.
On Windows, stdin, stdout and stderr are creates using TextIOWrapper(..., newline=None). In this case, TextIOWrapper._writenl is os.linesep and so '\r\n'.
To sum up, print() writes '\n' into sys.stdout, but sys.stdout write b'\r\n' into the file descriptor 1 which is a binary file (ie. the underlying OS file doesn't translate newlines).
If the output is redirected (e.g. into a file), TextIOWrapper is created with line_buffering=False.
You may try to force line_buffering=True when the output is redirected.
amauryfa commentedon Oct 7, 2011
MZ commentedon Oct 10, 2011
Just to make it clear: I have not observed any problems on the Windows terminal (cmd) with \n newline, but at least Notepad does not break lines correctly if only \n is used.
atsuoishimoto commentedon Aug 1, 2012
I found 'more' command in Windows7 requires \r\n.
Python 2.7.3:
C:\>python -c "for i in range(5):print(i)"|more
0
1
2
3
4
Python 3.3(trunk):
c:\src\cpython\PCbuild>python -c "for i in range(5):print(i)"|more
?????
vstinner commentedon Aug 1, 2012
Oh, I was wrong: stdin is created with newline=None, but stdout and stderr are created with newline="\n" and so "\n" is not translated to "\r\n".
I checked in Python 2.7: print("abc") and sys.stdout.write("abc\n") writes b"abc\r\n" into the output file (when the output is redirected), but sys.stdout.write("abc\r\n") writes b"abc\r\r\n". Python 3.3 should do the same: \r\n is preferred on Windows (ex: notepad doesn't support UNIX line ending, \n).
Attached patch changes line ending for stdout and stderr on Windows: translate "\n" to "\r\n".
It would be nice to fix this before Python 3.3 final.
atsuoishimoto commentedon Aug 2, 2012
Test for this issue. Tested on Windows7, Ubuntu linux 12.04.
I wonder why "print(1, file=sys.stderr)" returns '1' instead of '1\n'.
But in Python2.7, "print >>sys.stderr, 1" also returns '1',
so this might not be a problem.
vstinner commentedon Aug 2, 2012
I suppose that you mean "returns '1\n' instead of '1'". This is a
major change between Python 2 and Python 3. Use print(1, end=' ') if
you want the same behaviour. See:
http://docs.python.org/dev/whatsnew/3.0.html#print-is-a-function
You can also use the print() as a function in Python 2 using "from
__future__ import print_function":
http://docs.python.org/dev/whatsnew/2.6.html#pep-3105-print-as-a-function
I never liked "print expr," because it looks like a typo or an ugly
hack. It's easy to add a comma by mistake.
vstinner commentedon Aug 2, 2012
@georg: So, what do you think?
15 remaining items
pitrou commentedon Aug 5, 2012
It's a blocker because the fix broke a couple of tests.
And it's also a regression from 3.1 and 2.7.
loewis commentedon Aug 5, 2012
That cannot possibly be the explanation why haypo declared
it a blocker on 2012-08-01; the fix was only applied on
2012-08-04.
But I agree that it should block the release now; hence I
propose to roll back the entire set of changes and revert
to the 3.2 state.
Since 3.2 was released with this behavior, this bug cannot manage
to block 3.3.
atsuoishimoto commentedon Aug 5, 2012
Antoine Pitrou added the comment:
Man page of Python says
-u Force stdin, stdout and stderr to be totally unbuffered. On
systems where it matters, also put stdin, stdout and stderr in
binary mode.
test_httpservers depends on this behavior, but was implemented as documented in Python 3.
pitrou commentedon Aug 5, 2012
I don't know which version it is, but current 3.3 says:
“Force the binary I/O layers of stdin, stdout and stderr to be
unbuffered. The text I/O layer will still be line-buffered.”
I would argue that test_httpservers is wrong, since it uses print() in a
CGI script where sys.stdout.buffer.write() should really be used.
python-dev commentedon Aug 5, 2012
New changeset bc4fdb758b8c by Antoine Pitrou in branch '3.2':
Fix CGI tests to take into account the platform's line ending (issue bpo-13119)
http://hg.python.org/cpython/rev/bc4fdb758b8c
New changeset ee185c6b2880 by Antoine Pitrou in branch 'default':
Fix CGI tests to take into account the platform's line ending (issue bpo-13119)
http://hg.python.org/cpython/rev/ee185c6b2880
vstinner commentedon Aug 5, 2012
Buildbots are happy, the issue can be closed again. Thanks Antoine.
atsuoishimoto commentedon Aug 5, 2012
Ah, sorry, I thought I was reading latest Man page.
serhiy-storchaka commentedon Aug 16, 2012
Non-escaped "\n".
birkenfeld commentedon Aug 16, 2012
Would be nice to be a bit more specific *where* that line comes from.
serhiy-storchaka commentedon Aug 16, 2012
Modules/_io/textio.c, changesets 243ad1a6f638 and 083776adcacc.
serhiy-storchaka commentedon Aug 16, 2012
Oops, I got the wrong issue, sorry.