]> granicus.if.org Git - python/commitdiff
Issue #10841: set binary mode on files; the parser translates newlines
authorVictor Stinner <victor.stinner@haypocalc.com>
Fri, 7 Jan 2011 18:47:22 +0000 (18:47 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Fri, 7 Jan 2011 18:47:22 +0000 (18:47 +0000)
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).

Misc/NEWS
Modules/_io/fileio.c
Modules/main.c
Parser/tokenizer.c

index 8702e714e447bc8981910a17197ab211b04792a3..33aac6e3db06e1bf4da039e81c1846eeebfe86e5 100644 (file)
--- 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
index 23616361be1ef18aae7936e5214a409dbd6b064d..2e5d7acbf9614182caca7e884a1ebd7dabc846a2 100644 (file)
@@ -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;
 
index 93d093dbb8b334e1819184388b3c2d29682966c2..0b656e5163e7f1d30f88cbe3e5752754dd5e1974 100644 (file)
@@ -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);
index 556be46b674bc10af657735577da20f553d85a90..1eb62aa2a093a13f21492e7894323cefeb0a2fb2 100644 (file)
@@ -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 */