pep 3116 behaviour on non-blocking reads

A

Antoon Pardon

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.
 
G

greg

Antoon said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top