[Python-checkins] r87824 - in python/branches/py3k: Misc/NEWS Modules/_io/fileio.c Modules/main.c Parser/tokenizer.c

victor.stinner python-checkins at python.org
Fri Jan 7 19:47:22 CET 2011


Author: victor.stinner
Date: Fri Jan  7 19:47:22 2011
New Revision: 87824

Log:
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).

Modified:
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Modules/_io/fileio.c
   python/branches/py3k/Modules/main.c
   python/branches/py3k/Parser/tokenizer.c

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Fri Jan  7 19:47:22 2011
@@ -8,6 +8,10 @@
 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

Modified: python/branches/py3k/Modules/_io/fileio.c
==============================================================================
--- python/branches/py3k/Modules/_io/fileio.c	(original)
+++ python/branches/py3k/Modules/_io/fileio.c	Fri Jan  7 19:47:22 2011
@@ -388,6 +388,11 @@
             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;
 

Modified: python/branches/py3k/Modules/main.c
==============================================================================
--- python/branches/py3k/Modules/main.c	(original)
+++ python/branches/py3k/Modules/main.c	Fri Jan  7 19:47:22 2011
@@ -527,11 +527,14 @@
 
     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);

Modified: python/branches/py3k/Parser/tokenizer.c
==============================================================================
--- python/branches/py3k/Parser/tokenizer.c	(original)
+++ python/branches/py3k/Parser/tokenizer.c	Fri Jan  7 19:47:22 2011
@@ -892,6 +892,13 @@
         }
         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 */


More information about the Python-checkins mailing list