pep 3116 behaviour on non-blocking reads

Antoon Pardon apardon at forel.vub.ac.be
Thu Aug 9 05:09:26 EDT 2007


In the RawIOBase class I read the following:

.read(n: int) -> bytes

    Read up to n bytes from the object and return them. Fewer than n
    bytes may be returned if the operating system call returns fewer
    than n bytes. If 0 bytes are returned, this indicates end of file.
    If the object is in non-blocking mode and no bytes are available,
    the call returns None.


I would like the developers to reconsider and return 0 bytes when no
bytes are available and let None indicate end of file.

The reason is that this can lead to clearer code that will work
independant of the blocking mode of the stream.

Consider a consumer that just has to treat each byte. Then with
the current choice the code will look something like the following
(assuming pep 315 is implemented)


|    do:
|        buf = stream.read(nr)
|    while buf != "":
|        if buf is not None:
|            for b in buf:
|                treat(b)


If what the method returns follows my proposal, the code to do the same
would look something like the following:


|    do:
|        buf = stream.read(nr)
|    while buf is not None:
|        for b in buff:
|            treat(b)


The advantage with my propsal is that in a lot of cases an empty buffer
can be treated just the same as a non-empty one and that is reflected
in the return values of my proposal.


-- 
Antoon Pardon



More information about the Python-list mailing list