Tim Ludwinski
Tim Ludwinski

Reputation: 2863

Python Unbuffered Mode Causes Problems in Windows

Running the following, then trying to run interactive commands fails...

c:\> python -u -i test.py | tee output.txt
... test.py output ...   

  File "<stdin>", line 1

    ^
SyntaxError: invalid syntax
>>> print "Hi"
  File "<stdin>", line 1
   print "Hi"
              ^

SyntaxError: invalid syntax

A simpler test also fails:

c:\> python -u 
>>> print "Hi"
  File "<stdin>", line 1
   print "Hi"
              ^

SyntaxError: invalid syntax

I'm using Python 2.6.6 (r266:84297, Aug 24 2010, 18:13:38) [MSC v.1500 64 bit (AMD64)] on win32 on Windows 7.

Upvotes: 1

Views: 554

Answers (2)

Eryk Sun
Eryk Sun

Reputation: 34260

The parser is having trouble with the Windows "\r\n" line endings. Python's unbuffered mode is also binary mode, for which the C runtime doesn't translate "\r\n" to the "\n" newline character that Python expects. Try entering the following at the first prompt to switch back to text mode: import os, msvcrt; _ = msvcrt.setmode(0, os.O_TEXT) #. For example:

>>> import sys, os, msvcrt #
>>> line = sys.stdin.readline() #
print "Hi"
>>> line #
'print "Hi"\r\n'

>>> _ = msvcrt.setmode(sys.stdin.fileno(), os.O_TEXT) #
>>> line = sys.stdin.readline()
print "Hi"
>>> line
'print "Hi"\n'
>>> print "Hi"
Hi

Upvotes: 1

Tim Ludwinski
Tim Ludwinski

Reputation: 2863

The following is a workaround for the problem:

c:\> python -u 
>>> print "Hi" # This comment prevents the bug
Hi

Unfortunately, this is the best I can come up with for multi line statements:

c:\> python -u 
>>> exec ''' # Comments still needed inside string
... for i in range(4): # 
...     print i #
... ''' # comment on the end of line
0
1
2
3

Upvotes: 0

Related Questions