I have the following very simple-looking code:
from __future__ import print_function
import sys
for line in sys.stdin:
print("hello!")
sys.stdout.flush()
When I run it, I expect to type in a line and then immediately see hello!
printed out after it, after which I can type in another line.
When I run it with py -3
I get expected results:
py -3 test.py
asdf
hello!
asdf
hello!
asdf
hello!
^C
When I run it with py -2
, it won't print hello!
unless I send ctrl-Z:
py -2 test.py
asdf
asdf
asdf
^Z
hello!
hello!
hello!
asdf
^Z
hello!
^Z
I ran this test on Windows with Python 2.7.12 and on Linux (using ctrl-D instead of ctrl-Z) with Python 2.6.6.
How can I get the Python 2 behavior to match that of Python3?
EDIT:
Working Python 3 example on repl.it: https://repl.it/Cs6n/0
Broken Python 2 example on repl.it: https://repl.it/Cs7C/0