]> granicus.if.org Git - python/commitdiff
Close #13119: use "\r\n" newline for sys.stdout/err on Windows
authorVictor Stinner <victor.stinner@gmail.com>
Fri, 3 Aug 2012 23:28:00 +0000 (01:28 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Fri, 3 Aug 2012 23:28:00 +0000 (01:28 +0200)
sys.stdout and sys.stderr are now using "\r\n" newline on Windows, as Python 2.

Lib/test/test_cmd_line.py
Misc/NEWS
Python/pythonrun.c

index 8c960b14ec913f76307cb0591f4b25a60ae8d925..7644db21eea791b886087e3ae2c8a632822042c4 100644 (file)
@@ -259,6 +259,23 @@ class CmdLineTest(unittest.TestCase):
             "print(repr(input()))",
             b"'abc'")
 
+    def test_output_newline(self):
+        # Issue 13119 Newline for print() should be \r\n on Windows.
+        code = """if 1:
+            import sys
+            print(1)
+            print(2)
+            print(3, file=sys.stderr)
+            print(4, file=sys.stderr)"""
+        rc, out, err = assert_python_ok('-c', code)
+
+        if sys.platform == 'win32':
+            self.assertEqual(b'1\r\n2\r\n', out)
+            self.assertEqual(b'3\r\n4', err)
+        else:
+            self.assertEqual(b'1\n2\n', out)
+            self.assertEqual(b'3\n4', err)
+
     def test_unmached_quote(self):
         # Issue #10206: python program starting with unmatched quote
         # spewed spaces to stdout
index 302c62c15707a1e7096de7d855c1340adec687cb..9923ff34c790d2823e81ea29e131ce55b9a62e6d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Beta 2?
 Core and Builtins
 -----------------
 
+- Issue #13119: sys.stdout and sys.stderr are now using "\r\n" newline on
+  Windows, as Python 2.
+
 - Issue #15534: Fix the fast-search function for non-ASCII Unicode strings.
 
 - Issue #15508: Fix the docstring for __import__ to have the proper default
index cafc09ac3f5f1532d95cc8247426fecf86c5af95..05dfb8e1d0a983432d8071db7c7b3ef444e2ceb6 100644 (file)
@@ -971,12 +971,15 @@ create_stdio(PyObject* io,
     Py_CLEAR(raw);
     Py_CLEAR(text);
 
-    newline = "\n";
 #ifdef MS_WINDOWS
-    if (!write_mode) {
-        /* translate \r\n to \n for sys.stdin on Windows */
-        newline = NULL;
-    }
+    /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
+       newlines to "\n".
+       sys.stdout and sys.stderr: translate "\n" to "\r\n". */
+    newline = NULL;
+#else
+    /* sys.stdin: split lines at "\n".
+       sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
+    newline = "\n";
 #endif
 
     stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",