summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-01-07 18:47:22 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-01-07 18:47:22 (GMT)
commit89e343660623862acbd40cc617b4af262d6c799f (patch)
tree6ffb758ccc88b03797670e983e9e2917a33fc77c
parentbdde5061163bd773f56e3774f38fa80344267f6f (diff)
downloadcpython-89e343660623862acbd40cc617b4af262d6c799f.zip
cpython-89e343660623862acbd40cc617b4af262d6c799f.tar.gz
cpython-89e343660623862acbd40cc617b4af262d6c799f.tar.bz2
Issue #10841: set binary mode on files; the parser translates newlines
On Windows, set the binary mode on stdin, stdout, stderr and all io.FileIO objects (to not translate newlines, \r\n <=> \n). The Python parser translates newlines (\r\n => \n).
-rw-r--r--Misc/NEWS4
-rw-r--r--Modules/_io/fileio.c5
-rw-r--r--Modules/main.c9
-rw-r--r--Parser/tokenizer.c7
4 files changed, 22 insertions, 3 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index 8702e71..33aac6e 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -8,6 +8,10 @@ What's New in Python 3.2 Release Candidate 1
Core and Builtins
-----------------
+- Issue #10841: On Windows, set the binary mode on stdin, stdout, stderr and
+ all io.FileIO objects (to not translate newlines, \r\n <=> \n). The Python
+ parser translates newlines (\r\n => \n).
+
- Remove buffer API from stable ABI for now, see #10181.
- Issue #8651: PyArg_Parse*() functions raise an OverflowError if the file
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 2361636..2e5d7ac 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -388,6 +388,11 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
goto error;
}
+#if defined(MS_WINDOWS) || defined(__CYGWIN__)
+ /* don't translate newlines (\r\n <=> \n) */
+ _setmode(self->fd, O_BINARY);
+#endif
+
if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
goto error;
diff --git a/Modules/main.c b/Modules/main.c
index 93d093d..0b656e5 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -527,11 +527,14 @@ Py_Main(int argc, wchar_t **argv)
stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
- if (Py_UnbufferedStdioFlag) {
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
- _setmode(fileno(stdin), O_BINARY);
- _setmode(fileno(stdout), O_BINARY);
+ /* don't translate newlines (\r\n <=> \n) */
+ _setmode(fileno(stdin), O_BINARY);
+ _setmode(fileno(stdout), O_BINARY);
+ _setmode(fileno(stderr), O_BINARY);
#endif
+
+ if (Py_UnbufferedStdioFlag) {
#ifdef HAVE_SETVBUF
setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index 556be46..1eb62aa 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -892,6 +892,13 @@ tok_nextc(register struct tok_state *tok)
}
if (tok->prompt != NULL) {
char *newtok = PyOS_Readline(stdin, stdout, tok->prompt);
+ if (newtok != NULL) {
+ char *translated = translate_newlines(newtok, 0, tok);
+ PyMem_FREE(newtok);
+ if (translated == NULL)
+ return EOF;
+ newtok = translated;
+ }
#ifndef PGEN
if (tok->encoding && newtok && *newtok) {
/* Recode to UTF-8 */