Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90ef747

Browse files
committed
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.
1 parent 401e17d commit 90ef747

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

Lib/test/test_cmd_line.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,23 @@ def test_builtin_input(self):
265265
"print(repr(input()))",
266266
b"'abc'")
267267

268+
def test_output_newline(self):
269+
# Issue 13119 Newline for print() should be \r\n on Windows.
270+
code = """if 1:
271+
import sys
272+
print(1)
273+
print(2)
274+
print(3, file=sys.stderr)
275+
print(4, file=sys.stderr)"""
276+
rc, out, err = assert_python_ok('-c', code)
277+
278+
if sys.platform == 'win32':
279+
self.assertEqual(b'1\r\n2\r\n', out)
280+
self.assertEqual(b'3\r\n4', err)
281+
else:
282+
self.assertEqual(b'1\n2\n', out)
283+
self.assertEqual(b'3\n4', err)
284+
268285
def test_unmached_quote(self):
269286
# Issue #10206: python program starting with unmatched quote
270287
# spewed spaces to stdout

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #13119: sys.stdout and sys.stderr are now using "\r\n" newline on
14+
Windows, as Python 2.
15+
1316
- Issue #14579: Fix CVE-2012-2135: vulnerability in the utf-16 decoder after
1417
error handling. Patch by Serhiy Storchaka.
1518

Python/pythonrun.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -870,12 +870,15 @@ create_stdio(PyObject* io,
870870
Py_CLEAR(raw);
871871
Py_CLEAR(text);
872872

873-
newline = "\n";
874873
#ifdef MS_WINDOWS
875-
if (!write_mode) {
876-
/* translate \r\n to \n for sys.stdin on Windows */
877-
newline = NULL;
878-
}
874+
/* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
875+
newlines to "\n".
876+
sys.stdout and sys.stderr: translate "\n" to "\r\n". */
877+
newline = NULL;
878+
#else
879+
/* sys.stdin: split lines at "\n".
880+
sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
881+
newline = "\n";
879882
#endif
880883

881884
stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO",

0 commit comments

Comments
 (0)