[issue26290] fileinput and 'for line in sys.stdin' do strange mockery of input buffering
Josh Rosenberg
report at bugs.python.org
Fri Nov 20 13:48:44 EST 2020
Josh Rosenberg <shadowranger+python at gmail.com> added the comment:
For those who find this in the future, the simplest workaround for the:
for line in sys.stdin:
issue on Python 2 is to replace it with:
for line in iter(sys.stdin.readline, ''):
The problem is caused by the way file.__next__'s buffering behaves, but file.readline doesn't use that code (it delegates to either fgets or a loop over getc/getc_unlocked that never overbuffers beyond the newline). Two-arg iter lets you make an iterator that calls readline each time you want a line, and considers a return of '' (which is what readline returns when you hit EOF) to terminate iteration.
----------
nosy: +josh.r
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue26290>
_______________________________________
More information about the Python-bugs-list
mailing list