diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-02-23 12:07:37 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-02-23 12:07:37 (GMT) |
commit | c0f1a1afae3843986eb0bef54b165424361f2510 (patch) | |
tree | c650a798108af5024bcee2c01ae8f67586c952c8 | |
parent | dd071045e776e1c3e8cf6750a2fd1d0958bf19b3 (diff) | |
download | cpython-c0f1a1afae3843986eb0bef54b165424361f2510.zip cpython-c0f1a1afae3843986eb0bef54b165424361f2510.tar.gz cpython-c0f1a1afae3843986eb0bef54b165424361f2510.tar.bz2 |
Issue #11272: Fix input() and sys.stdin for Windows newline
On Windows, input() strips '\r' (and not only '\n'), and sys.stdin uses
universal newline (replace '\r\n' by '\n').
-rw-r--r-- | Lib/test/test_cmd_line.py | 26 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Python/bltinmodule.c | 13 | ||||
-rw-r--r-- | Python/pythonrun.c | 11 |
4 files changed, 48 insertions, 5 deletions
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index b21b61e..c4e3adf 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -6,6 +6,7 @@ import test.support, unittest import os import sys import subprocess +import tempfile from test.script_helper import spawn_python, kill_python, assert_python_ok, assert_python_failure @@ -239,6 +240,31 @@ class CmdLineTest(unittest.TestCase): escaped = repr(text).encode(encoding, 'backslashreplace') self.assertIn(escaped, data) + def check_input(self, code, expected): + with tempfile.NamedTemporaryFile("wb+") as stdin: + sep = os.linesep.encode('ASCII') + stdin.write(sep.join((b'abc', b'def'))) + stdin.flush() + stdin.seek(0) + with subprocess.Popen( + (sys.executable, "-c", code), + stdin=stdin, stdout=subprocess.PIPE) as proc: + stdout, stderr = proc.communicate() + self.assertEqual(stdout.rstrip(), expected) + + def test_stdin_readline(self): + # Issue #11272: check that sys.stdin.readline() replaces '\r\n' by '\n' + # on Windows (sys.stdin is opened in binary mode) + self.check_input( + "import sys; print(repr(sys.stdin.readline()))", + b"'abc\\n'") + + def test_builtin_input(self): + # Issue #11272: check that input() strips newlines ('\n' or '\r\n') + self.check_input( + "print(repr(input()))", + b"'abc'") + def test_main(): test.support.run_unittest(CmdLineTest) @@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1? Core and Builtins ----------------- +- Issue #11272: On Windows, input() strips '\r' (and not only '\n'), and + sys.stdin uses universal newline (replace '\r\n' by '\n'). + - Issue #10830: Fix PyUnicode_FromFormatV("%c") for non-BMP characters on narrow build. diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 42bc809..c6bb16c 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1618,6 +1618,7 @@ builtin_input(PyObject *self, PyObject *args) PyObject *stdin_encoding; char *stdin_encoding_str; PyObject *result; + size_t len; stdin_encoding = PyObject_GetAttrString(fin, "encoding"); if (!stdin_encoding) @@ -1682,19 +1683,23 @@ builtin_input(PyObject *self, PyObject *args) Py_DECREF(stdin_encoding); return NULL; } - if (*s == '\0') { + + len = strlen(s); + if (len == 0) { PyErr_SetNone(PyExc_EOFError); result = NULL; } - else { /* strip trailing '\n' */ - size_t len = strlen(s); + else { if (len > PY_SSIZE_T_MAX) { PyErr_SetString(PyExc_OverflowError, "input: input too long"); result = NULL; } else { - result = PyUnicode_Decode(s, len-1, stdin_encoding_str, NULL); + len--; /* strip trailing '\n' */ + if (len != 0 && s[len-1] == '\r') + len--; /* strip trailing '\r' */ + result = PyUnicode_Decode(s, len, stdin_encoding_str, NULL); } } Py_DECREF(stdin_encoding); diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 33f1873..8e97d7f 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -778,6 +778,7 @@ create_stdio(PyObject* io, { PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; const char* mode; + const char* newline; PyObject *line_buffering; int buffering, isatty; @@ -828,9 +829,17 @@ 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; + } +#endif + stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO", buf, encoding, errors, - "\n", line_buffering); + newline, line_buffering); Py_CLEAR(buf); if (stream == NULL) goto error; |