[Python-checkins] cpython (3.2): Close #13119: use "\r\n" newline for sys.stdout/err on Windows

victor.stinner python-checkins at python.org
Sat Aug 4 01:42:20 CEST 2012


http://hg.python.org/cpython/rev/09408b990ca5
changeset: 78406:09408b990ca5
branch: 3.2
parent: 78403:243ad1a6f638
user: Victor Stinner <victor.stinner at gmail.com>
date: Sat Aug 04 01:37:32 2012 +0200
summary:
 Close #13119: use "\r\n" newline for sys.stdout/err on Windows
sys.stdout and sys.stderr are now using "\r\n" newline on Windows, as Python 2.
files:
 Lib/test/test_cmd_line.py | 17 +++++++++++++++++
 Misc/NEWS | 3 +++
 Python/pythonrun.c | 13 ++++++++-----
 3 files changed, 28 insertions(+), 5 deletions(-)
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -265,6 +265,23 @@
 "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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #13119: sys.stdout and sys.stderr are now using "\r\n" newline on
+ Windows, as Python 2.
+
 - Issue #14579: Fix CVE-2012-2135: vulnerability in the utf-16 decoder after
 error handling. Patch by Serhiy Storchaka.
 
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -870,12 +870,15 @@
 Py_CLEAR(raw);
 Py_CLEAR(text);
 
+#ifdef MS_WINDOWS
+ /* 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";
-#ifdef MS_WINDOWS
- if (!write_mode) {
- /* translate \r\n to \n for sys.stdin on Windows */
- newline = NULL;
- }
 #endif
 
 stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO",
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /