Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 89e3436

Browse files
author
Victor Stinner
committed
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).
1 parent bdde506 commit 89e3436

4 files changed

Lines changed: 22 additions & 3 deletions

File tree

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ What's New in Python 3.2 Release Candidate 1
88
Core and Builtins
99
-----------------
1010

11+
- Issue #10841: On Windows, set the binary mode on stdin, stdout, stderr and
12+
all io.FileIO objects (to not translate newlines, \r\n <=> \n). The Python
13+
parser translates newlines (\r\n => \n).
14+
1115
- Remove buffer API from stable ABI for now, see #10181.
1216

1317
- Issue #8651: PyArg_Parse*() functions raise an OverflowError if the file

Modules/_io/fileio.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
388388
goto error;
389389
}
390390

391+
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
392+
/* don't translate newlines (\r\n <=> \n) */
393+
_setmode(self->fd, O_BINARY);
394+
#endif
395+
391396
if (PyObject_SetAttrString((PyObject *)self, "name", nameobj) < 0)
392397
goto error;
393398

Modules/main.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,14 @@ Py_Main(int argc, wchar_t **argv)
527527

528528
stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
529529

530-
if (Py_UnbufferedStdioFlag) {
531530
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
532-
_setmode(fileno(stdin), O_BINARY);
533-
_setmode(fileno(stdout), O_BINARY);
531+
/* don't translate newlines (\r\n <=> \n) */
532+
_setmode(fileno(stdin), O_BINARY);
533+
_setmode(fileno(stdout), O_BINARY);
534+
_setmode(fileno(stderr), O_BINARY);
534535
#endif
536+
537+
if (Py_UnbufferedStdioFlag) {
535538
#ifdef HAVE_SETVBUF
536539
setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
537540
setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);

Parser/tokenizer.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,13 @@ tok_nextc(register struct tok_state *tok)
892892
}
893893
if (tok->prompt != NULL) {
894894
char *newtok = PyOS_Readline(stdin, stdout, tok->prompt);
895+
if (newtok != NULL) {
896+
char *translated = translate_newlines(newtok, 0, tok);
897+
PyMem_FREE(newtok);
898+
if (translated == NULL)
899+
return EOF;
900+
newtok = translated;
901+
}
895902
#ifndef PGEN
896903
if (tok->encoding && newtok && *newtok) {
897904
/* Recode to UTF-8 */

0 commit comments

Comments
 (0)