Search code examples
pythonwindowspython-2.6unbuffered

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.


Solution

  • 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