Skip to content

Commit a592104

Browse files
committed
stream: Add compliant handling of non-blocking read()/write().
In case of empty non-blocking read()/write(), both return None. read() cannot return 0, as that means EOF, so returns another value, and then write() just follows. This is still pretty unexpected, and typical "if not len:" check would treat this as EOF. Well, non-blocking files require special handling! This also kind of makes it depending on POSIX, but well, anything else should emulate POSIX anyway ;-).
1 parent 93afa23 commit a592104

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

py/stream.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include <string.h>
28+
#include <errno.h>
2829

2930
#include "mpconfig.h"
3031
#include "nlr.h"
@@ -38,6 +39,10 @@
3839

3940
STATIC mp_obj_t stream_readall(mp_obj_t self_in);
4041

42+
// TODO: This is POSIX-specific (but then POSIX is the only real thing,
43+
// and anything else just emulates it, right?)
44+
#define is_nonblocking_error(errno) ((errno) == EAGAIN || (errno) == EWOULDBLOCK)
45+
4146
STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
4247
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
4348
if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
@@ -53,6 +58,14 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
5358
int error;
5459
machine_int_t out_sz = o->type->stream_p->read(o, buf, sz, &error);
5560
if (out_sz == -1) {
61+
if (is_nonblocking_error(error)) {
62+
// https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
63+
// "If the object is in non-blocking mode and no bytes are available,
64+
// None is returned."
65+
// This is actually very weird, as naive truth check will treat
66+
// this as EOF.
67+
return mp_const_none;
68+
}
5669
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
5770
} else {
5871
mp_obj_t s = mp_obj_new_str(buf, out_sz, false); // will reallocate to use exact size
@@ -74,11 +87,16 @@ STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
7487
int error;
7588
machine_int_t out_sz = o->type->stream_p->write(self_in, bufinfo.buf, bufinfo.len, &error);
7689
if (out_sz == -1) {
90+
if (is_nonblocking_error(error)) {
91+
// http://docs.python.org/3/library/io.html#io.RawIOBase.write
92+
// "None is returned if the raw stream is set not to block and
93+
// no single byte could be readily written to it."
94+
// This is for consistency with read() behavior, still weird,
95+
// see abobe.
96+
return mp_const_none;
97+
}
7798
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
7899
} else {
79-
// http://docs.python.org/3/library/io.html#io.RawIOBase.write
80-
// "None is returned if the raw stream is set not to block and no single byte could be readily written to it."
81-
// Do they mean that instead of 0 they return None?
82100
return MP_OBJ_NEW_SMALL_INT(out_sz);
83101
}
84102
}

0 commit comments

Comments
 (0)