Message381496
| Author |
josh.r |
| Recipients |
Don Hatch, josh.r, martin.panter, serhiy.storchaka |
| Date |
2020年11月20日.18:48:44 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1605898124.5.0.162185282894.issue26290@roundup.psfhosted.org> |
| In-reply-to |
| Content |
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. |
|