New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'python -u' yields trailing carriage return '\r' (Python2 for Windows) #66145
Comments
raw_input yields trailing carriage return ('\r') when C:\Python27\python.exe is called using the -u option How to reproduce the error: save the attached file 'input.py' which contains the following lines -- begin input.py -- i = raw_input("enter y or n: ")
print(repr(i))
print([ord(c) for c in i]) -- end input.py -- then run the two following commands from a windows command shell The same bug affects also the interactive mode: start 'python -u' and enter an arbitrary command. You will get a syntax error at the end of line. (see transcript 2 below) -- begin transcript 1 of cmd session -- C:\Temp>where python C:\Temp>python --version C:\Temp>type input.py i = raw_input("enter y or n: ")
print(repr(i))
print([ord(c) for c in i]) C:\Temp>python input.py -- begin transcript 2 of cmd session -- C:\Temp>python -u input.py C:\Temp>python -u
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello world"
File "<stdin>", line 1
print "hello world"
^
SyntaxError: invalid syntax
>>> -- end transcript 2 of cmd session -- |
correction: the markers for transcripts 1 and 2 were inserted at the wrong location; here's the correction: -- begin transcript 1 of cmd session -- C:\Temp>where python C:\Temp>python --version C:\Temp>type input.py i = raw_input("enter y or n: ")
print(repr(i))
print([ord(c) for c in i]) C:\Temp>python input.py C:\Temp>python -u input.py -- end transcript 1 of cmd session -- -- begin transcript 2 of cmd session -- C:\Temp>python -u
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello world"
File "<stdin>", line 1
print "hello world"
^
SyntaxError: invalid syntax
>>> -- end transcript 2 of cmd session -- |
This is the documented behavior of the -u option. It puts the streams in binary mode "on systems where it matters", which would be windows. That is, universal newline processing is disabled when you use -u. Note that this is no longer an issue in python3: there, -u only affects buffering, and not the binary/text mode (because in python3 there is always a distinction between binary and text mode, regardless of platform). |
The bug is not on print, but raw_input(). In Python 3, I worked on the following issues to support fully binary standard streams:
It looks like this issue is the same than bpo-11272: input() removes '\n' but not '\r'. |
I backported and adapted most Python 3 fixes related to the Windows binary mode for stdout/stderr used when Python is started with the -u command line option. See attached binary.patch. My change to file.write() looks wrong because the function returns None and does not loop until all bytes are written. By the way, string_write() doesn't check if fwrite(n) wrote less than n bytes or failed with an error. See also the issue bpo-21090 which improves error handling in the file object. |
While testing binary.patch, test_subprocess failed because of the issue bpo-19612. I backported the fix: changeset: 91905:039ac3f01c4e |
It's not entirely clear from a quick read whether this was actually fixed or not (probably?), but as 2.7 is out of support it no longer matters :) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: