[Python-Dev] fileobj.read(float): warning or error?

Cameron Simpson cs at zip.com.au
Wed Jul 23 00:28:44 CEST 2008


On 22Jul2008 20:56, John J Lee <jjl at pobox.com> wrote:
> On Tue, 22 Jul 2008, Cameron Simpson wrote:
> [...]
>> Leaving aside the 0.2 => 0 converstion, shouldn't read() raise an
>> exception if asked for < 1 bytes? Or is there a legitimate use for read(0)
>> with which I was not previously aware?
>
> http://docs.python.org/lib/bltin-file-objects.html
>
> read([size])
>
> ... If the size argument is negative or omitted, read all data until EOF  
> is reached. ...

Hmm, yeah, but 0 is not negative and not omitted so this does not apply.

Personally I'm not very fond of that spec; I'm good with the omitted size
provoking a "read everything" mode but I'd rather a non-numeric value like
None rather than a negative one (eg the conventional "def read(size=None)")
if an explicit size should do so. That way bad arithmetic in the caller
could have a chance of triggering an exception from read instead of a
silent (and to my taste, nasty) "slurp the file" mode.
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/


More information about the Python-Dev mailing list