469,592 Members | 1,757 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

pep 3116 behaviour on non-blocking reads

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
Aug 9 '07 #1
1 994
Antoon Pardon wrote:
I would like the developers to reconsider and return 0 bytes when no
bytes are available and let None indicate end of file.
That would be a major departure from the way IO has
always been handled before in Python, which follows
the Unix model.

Also, only code that deals with non-blocking streams
will ever get None, and such code is relatively rare,
so most code won't have to worry about the None case.

Even when dealing with a non-blocking stream, usually
there will be some other way (such as select) used to
determine when there is something to be read from a
stream, and it won't be read otherwise. In that case,
the code *still* won't ever see a None.

So I think the PEP has it right.

--
Greg
Aug 10 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

48 posts views Thread by marbac | last post: by
1 post views Thread by JKop | last post: by
31 posts views Thread by grid | last post: by
3 posts views Thread by Carlo Capelli | last post: by
24 posts views Thread by temper3243 | last post: by
1 post views Thread by sachingoel82 | last post: by
285 posts views Thread by Sheth Raxit | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.