summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-02-23 12:10:23 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-02-23 12:10:23 (GMT)
commit02bfdb3f791a007801f2d14ed11b21bd6498ff9c (patch)
tree86835246c57c673166e4fe00911101195326428c /Python
parent9f6cbe09cc88be914600306b34ac3d0025738465 (diff)
downloadcpython-02bfdb3f791a007801f2d14ed11b21bd6498ff9c.zip
cpython-02bfdb3f791a007801f2d14ed11b21bd6498ff9c.tar.gz
cpython-02bfdb3f791a007801f2d14ed11b21bd6498ff9c.tar.bz2
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'). ........
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c13
-rw-r--r--Python/pythonrun.c11
2 files changed, 19 insertions, 5 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index f9b3202..2aea9f7 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1621,6 +1621,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)
@@ -1685,19 +1686,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;