Joey Geralnik <jgeral...@gmail.com> added the comment:

First off, I'm a complete noob looking at the python source code for the first 
time so forgive me if I've done something wrong.

What if the length of the chunk is checked as well? The following code works 
fine:

import sys
while True:
    chunk = sys.stdin.read(1000)
    if not chunk:
        break
    # process
    if len(chunk) < 1000:
        break

Something similar could be done in the fileinput class. The patch I've attached 
checks if the number of bytes read from the file is less than the size of the 
buffer (which means that the file has ended). If so, the next time the file is 
to be read it skips to the next file instead.

joey@j-Laptop:~/cpython$ ./python 
Python 3.3.0a3+ (default:befd56673c80+, Jun 15 2012, 17:14:12) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import fileinput
[73732 refs]
>>> lines = list(fileinput.input())
foo
bar
^D
[73774 refs]
>>> lines
['foo\n', 'bar\n']
[73780 refs]

----------
keywords: +patch
nosy: +jgeralnik
Added file: http://bugs.python.org/file26018/fileinput.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue15068>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to