Skip to content

Newline for print() is \n on Windows, and not \r\n as expected #57328

Closed

Description

@MZ
mannequin
BPO 13119
Nosy @loewis, @birkenfeld, @atsuoishimoto, @amauryfa, @pitrou, @vstinner, @florentx, @serhiy-storchaka
Files
  • newline.py: Small trivial script
  • newline_3.1.txt: Py 3.1.4 output on Windows
  • newline_3.2.txt: Py 3.2.2 output on Windows
  • windows_stdout_newline.patch
  • issue13119_test.patch
  • issue13119_httpserver.patch
  • issue13119_unbuffered.patch
  • 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 = None
    closed_at = <Date 2012-08-16.06:58:03.911>
    created_at = <Date 2011-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 = <Date 2012-08-16.06:58:03.909>
    user = 'https://bugs.python.org/MZ'

    bugs.python.org fields:

    activity = <Date 2012-08-16.06:58:03.909>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-08-16.06:58:03.911>
    closer = 'serhiy.storchaka'
    components = ['Windows']
    creation = <Date 2011-10-06.20:59:47.493>
    creator = 'M..Z.'
    dependencies = []
    files = ['23330', '23331', '23332', '26644', '26660', '26694', '26695']
    hgrepos = []
    issue_num = 13119
    keywords = ['patch']
    message_count = 36.0
    messages = ['145039', '145050', '145054', '145064', '145066', '145068', '145310', '167067', '167092', '167193', '167250', '167257', '167269', '167288', '167379', '167382', '167449', '167452', '167454', '167457', '167460', '167465', '167468', '167479', '167481', '167482', '167485', '167486', '167488', '167499', '167500', '167502', '168348', '168349', '168350', '168351']
    nosy_count = 10.0
    nosy_names = ['loewis', 'georg.brandl', 'ishimoto', 'amaury.forgeotdarc', 'pitrou', 'vstinner', 'flox', 'M..Z.', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'release blocker'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue13119'
    versions = ['Python 3.2', 'Python 3.3']

    Activity

    1. MZ commented on Oct 6, 2011

      MZmannequin
      MannequinAuthor

      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.

    2. added
      type-bug
      on Oct 6, 2011
    3. amauryfa commented on Oct 6, 2011

      @amauryfa
      Contributor

      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?

    4. MZ commented on Oct 7, 2011

      MZmannequin
      MannequinAuthor

      Hi Amaury,

      The two text files were obtained through redirection in Windows, so I simply ran the newline.py file with:

      ...> C:\Python31\python.exe newline.py > newline_3.1.txt
      
      ...> C:\Python32\python.exe newline.py > newline_3.2.txt
      

      Best regards,
      Morten Zilmer

    5. vstinner commented on Oct 7, 2011

      @vstinner
      Member

      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).

    6. vstinner commented on Oct 7, 2011

      @vstinner
      Member

      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.

    7. amauryfa commented on Oct 7, 2011

      @amauryfa
      Contributor

      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?

    8. MZ commented on Oct 10, 2011

      MZmannequin
      MannequinAuthor

      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.

    9. atsuoishimoto commented on Aug 1, 2012

      atsuoishimotomannequin
      Mannequin

      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
      ?????

    10. vstinner commented on Aug 1, 2012

      @vstinner
      Member

      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.

    11. atsuoishimoto commented on Aug 2, 2012

      atsuoishimotomannequin
      Mannequin

      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.

    12. vstinner commented on Aug 2, 2012

      @vstinner
      Member

      I wonder why "print(1, file=sys.stderr)" returns '1' instead of '1\n'.

      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.

    13. vstinner commented on Aug 2, 2012

      @vstinner
      Member

      It would be nice to fix this before Python 3.3 final.

      @georg: So, what do you think?

    14. 15 remaining items

    15. pitrou commented on Aug 5, 2012

      @pitrou
      Member

      I wonder why this is a release blocker. It's a bug in Python 3.2, so
      why should it block the release of 3.3 (it's not a regression).

      It's a blocker because the fix broke a couple of tests.
      And it's also a regression from 3.1 and 2.7.

    16. loewis commented on Aug 5, 2012

      loewismannequin
      Mannequin

      It's a blocker because the fix broke a couple of tests.

      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.

      And it's also a regression from 3.1 and 2.7.

      Since 3.2 was released with this behavior, this bug cannot manage
      to block 3.3.

    17. atsuoishimoto commented on Aug 5, 2012

      atsuoishimotomannequin
      Mannequin

      Antoine Pitrou added the comment:

      > We should not convert \n with -u command line option or PYTHONUNBUFFERED was set.

      Why that? What do universal newlines have to do with buffering?

      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.

    18. pitrou commented on Aug 5, 2012

      @pitrou
      Member

      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.

      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.”

      test_httpservers depends on this behavior, but was implemented as
      documented in Python 3.

      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.

    19. python-dev commented on Aug 5, 2012

      python-devmannequin
      Mannequin

      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

    20. vstinner commented on Aug 5, 2012

      @vstinner
      Member

      Buildbots are happy, the issue can be closed again. Thanks Antoine.

    21. atsuoishimoto commented on Aug 5, 2012

      atsuoishimotomannequin
      Mannequin

      I don't know which version it is, but current 3.3 says:

      Ah, sorry, I thought I was reading latest Man page.

    22. serhiy-storchaka commented on Aug 16, 2012

      @serhiy-storchaka
      Member
      • " newline is '' or '\n', no translation takes place. If newline is any\n"

      Non-escaped "\n".

    23. birkenfeld commented on Aug 16, 2012

      @birkenfeld
      Member

      Would be nice to be a bit more specific *where* that line comes from.

    24. serhiy-storchaka commented on Aug 16, 2012

      @serhiy-storchaka
      Member

      Would be nice to be a bit more specific *where* that line comes from.

      Modules/_io/textio.c, changesets 243ad1a6f638 and 083776adcacc.

    25. serhiy-storchaka commented on Aug 16, 2012

      @serhiy-storchaka
      Member

      Oops, I got the wrong issue, sorry.

    26. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      OS-windowsrelease-blockertype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions