classification
Title: syntax error at end of line in interactive python -u
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: luch, pitrou, r.david.murray, vstinner
Priority: normal Keywords:

Created on 2011-02-02 11:18 by luch, last changed 2011-02-03 14:32 by r.david.murray. This issue is now closed.

Messages (5)
msg127732 - (view) Author: Alexey Luchko (luch) Date: 2011-02-02 11:18
Interactive python -u produces syntax error in win2k:
> python -u
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1+1
  File "<stdin>", line 1
    1+1
       ^
SyntaxError: invalid syntax
>>> import sys
  File "<stdin>", line 1
    import sys
              ^
SyntaxError: invalid syntax
>>>

Tried Python 2.5.4, 2.7.1 and 3.1.2 on the same win2k machine.

The problem does not exist under mac os x 10.5 and freebsd 8.1.
msg127736 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-02-02 15:42
This is fixed in py3k rc2, and thus will be fixed in 3.2.  The trick will be figuring out which update fixed it, to see if we can backport to 3.1 and 2.7.  Since issue 10841 touched stdio handling in Windows, I suspect that may be the source, so I'm adding Victor and Antoine to nosy since they worked that issue.

On the other hand, given how long this issue has existed without anybody running in to it, it may not even be worth backporting....what's your use case for starting the interactive interpreter with -u?
msg127739 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-02-02 16:05
Since r7409 (14 years ago), Python does set the binary binary mode on stdin and stdout (not stderr) if the -u flag is used:
----
    if (unbuffered) {
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
        _setmode(fileno(stdin), O_BINARY);
        _setmode(fileno(stdout), O_BINARY);
#endif
#ifdef HAVE_SETVBUF
        setvbuf(stdin,  (char *)NULL, _IONBF, BUFSIZ);
        setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
        setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
#else /* !HAVE_SETVBUF */
        setbuf(stdin,  (char *)NULL);
        setbuf(stdout, (char *)NULL);
        setbuf(stderr, (char *)NULL);
#endif /* !HAVE_SETVBUF */
    }
----

If you would like to check that the problem is related to the binary mode, you can set the binary mode manually without the -u flag. Add the following code at the end of your site.py file:
----
import msvcrt
msvcrt.setmode (0, os.O_BINARY) # stdin  = 0
msvcrt.setmode (1, os.O_BINARY) # stdout = 1
msvcrt.setmode (2, os.O_BINARY) # stderr = 2
----

Python 3.2 does always set the binary mode for all files (opened files but also stdin, stdout and stderr), which requires a fix in the parser because the parser didn't support Windows newlines (\r\n). See issue 10841 and the commit r87824 for more information.
msg127788 - (view) Author: Alexey Luchko (luch) Date: 2011-02-03 11:57
I reported the issue just because I didn't find it is already known.
I don't think it is worth backporting.
msg127793 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-02-03 14:32
Great.  Thanks for reporting it, and I'm glad we managed to already have it fixed :)
History
Date User Action Args
2011-03-04 19:11:38r.david.murraylinkissue11399 superseder
2011-02-03 14:32:09r.david.murraysetstatus: open -> closed
nosy: pitrou, vstinner, r.david.murray, luch
messages: + msg127793

resolution: fixed
stage: resolved
2011-02-03 11:57:37luchsetnosy: pitrou, vstinner, r.david.murray, luch
messages: + msg127788
2011-02-02 16:05:45vstinnersetnosy: pitrou, vstinner, r.david.murray, luch
messages: + msg127739
2011-02-02 15:42:01r.david.murraysetversions: - Python 2.5
nosy: + r.david.murray, vstinner, pitrou

messages: + msg127736

type: behavior
2011-02-02 11:18:54luchcreate