mirror of
https://github.com/python/cpython.git
synced 2025-09-04 07:51:13 +00:00
Merged revisions 88530 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r88530 | victor.stinner | 2011-02-23 13:07:37 +0100 (mer., 23 févr. 2011) | 4 lines 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'). ........
This commit is contained in:
parent
9f6cbe09cc
commit
02bfdb3f79
4 changed files with 49 additions and 6 deletions
|
@ -6,6 +6,7 @@ import test.support, unittest
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import tempfile
|
||||||
from test.script_helper import spawn_python, kill_python, assert_python_ok, assert_python_failure
|
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')
|
escaped = repr(text).encode(encoding, 'backslashreplace')
|
||||||
self.assertIn(escaped, data)
|
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():
|
def test_main():
|
||||||
test.support.run_unittest(CmdLineTest)
|
test.support.run_unittest(CmdLineTest)
|
||||||
|
|
|
@ -10,6 +10,9 @@ What's New in Python 3.2.1?
|
||||||
Core and Builtins
|
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').
|
||||||
|
|
||||||
- Check for NULL result in PyType_FromSpec.
|
- Check for NULL result in PyType_FromSpec.
|
||||||
|
|
||||||
Library
|
Library
|
||||||
|
@ -26,7 +29,7 @@ Library
|
||||||
32-bit Windows.
|
32-bit Windows.
|
||||||
|
|
||||||
- Issue #11089: Fix performance issue limiting the use of ConfigParser()
|
- Issue #11089: Fix performance issue limiting the use of ConfigParser()
|
||||||
with large config files.
|
with large config files.
|
||||||
|
|
||||||
- Issue #10276: Fix the results of zlib.crc32() and zlib.adler32() on buffers
|
- Issue #10276: Fix the results of zlib.crc32() and zlib.adler32() on buffers
|
||||||
larger than 4GB. Patch by Nadeem Vawda.
|
larger than 4GB. Patch by Nadeem Vawda.
|
||||||
|
|
|
@ -1621,6 +1621,7 @@ builtin_input(PyObject *self, PyObject *args)
|
||||||
PyObject *stdin_encoding;
|
PyObject *stdin_encoding;
|
||||||
char *stdin_encoding_str;
|
char *stdin_encoding_str;
|
||||||
PyObject *result;
|
PyObject *result;
|
||||||
|
size_t len;
|
||||||
|
|
||||||
stdin_encoding = PyObject_GetAttrString(fin, "encoding");
|
stdin_encoding = PyObject_GetAttrString(fin, "encoding");
|
||||||
if (!stdin_encoding)
|
if (!stdin_encoding)
|
||||||
|
@ -1685,19 +1686,23 @@ builtin_input(PyObject *self, PyObject *args)
|
||||||
Py_DECREF(stdin_encoding);
|
Py_DECREF(stdin_encoding);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (*s == '\0') {
|
|
||||||
|
len = strlen(s);
|
||||||
|
if (len == 0) {
|
||||||
PyErr_SetNone(PyExc_EOFError);
|
PyErr_SetNone(PyExc_EOFError);
|
||||||
result = NULL;
|
result = NULL;
|
||||||
}
|
}
|
||||||
else { /* strip trailing '\n' */
|
else {
|
||||||
size_t len = strlen(s);
|
|
||||||
if (len > PY_SSIZE_T_MAX) {
|
if (len > PY_SSIZE_T_MAX) {
|
||||||
PyErr_SetString(PyExc_OverflowError,
|
PyErr_SetString(PyExc_OverflowError,
|
||||||
"input: input too long");
|
"input: input too long");
|
||||||
result = NULL;
|
result = NULL;
|
||||||
}
|
}
|
||||||
else {
|
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);
|
Py_DECREF(stdin_encoding);
|
||||||
|
|
|
@ -778,6 +778,7 @@ create_stdio(PyObject* io,
|
||||||
{
|
{
|
||||||
PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
|
PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
|
||||||
const char* mode;
|
const char* mode;
|
||||||
|
const char* newline;
|
||||||
PyObject *line_buffering;
|
PyObject *line_buffering;
|
||||||
int buffering, isatty;
|
int buffering, isatty;
|
||||||
|
|
||||||
|
@ -828,9 +829,17 @@ create_stdio(PyObject* io,
|
||||||
Py_CLEAR(raw);
|
Py_CLEAR(raw);
|
||||||
Py_CLEAR(text);
|
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",
|
stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO",
|
||||||
buf, encoding, errors,
|
buf, encoding, errors,
|
||||||
"\n", line_buffering);
|
newline, line_buffering);
|
||||||
Py_CLEAR(buf);
|
Py_CLEAR(buf);
|
||||||
if (stream == NULL)
|
if (stream == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue