Skip to content

Commit 7799410

Browse files
committed
py/stream: Allow to reuse is_nonblocking_error().
1 parent 2ca7b05 commit 7799410

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

py/stream.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@
4747

4848
STATIC mp_obj_t stream_readall(mp_obj_t self_in);
4949

50-
#if MICROPY_STREAMS_NON_BLOCK
51-
// TODO: This is POSIX-specific (but then POSIX is the only real thing,
52-
// and anything else just emulates it, right?)
53-
#define is_nonblocking_error(errno) ((errno) == EAGAIN || (errno) == EWOULDBLOCK)
54-
#else
55-
#define is_nonblocking_error(errno) (0)
56-
#endif
57-
5850
#define STREAM_CONTENT_TYPE(stream) (((stream)->is_text) ? &mp_type_str : &mp_type_bytes)
5951

6052
STATIC mp_obj_t stream_read(mp_uint_t n_args, const mp_obj_t *args) {
@@ -96,7 +88,7 @@ STATIC mp_obj_t stream_read(mp_uint_t n_args, const mp_obj_t *args) {
9688
mp_uint_t out_sz = o->type->stream_p->read(o, p, more_bytes, &error);
9789
if (out_sz == MP_STREAM_ERROR) {
9890
vstr_cut_tail_bytes(&vstr, more_bytes);
99-
if (is_nonblocking_error(error)) {
91+
if (mp_is_nonblocking_error(error)) {
10092
// With non-blocking streams, we read as much as we can.
10193
// If we read nothing, return None, just like read().
10294
// Otherwise, return data read so far.
@@ -167,7 +159,7 @@ STATIC mp_obj_t stream_read(mp_uint_t n_args, const mp_obj_t *args) {
167159
mp_uint_t out_sz = o->type->stream_p->read(o, vstr.buf, sz, &error);
168160
if (out_sz == MP_STREAM_ERROR) {
169161
vstr_clear(&vstr);
170-
if (is_nonblocking_error(error)) {
162+
if (mp_is_nonblocking_error(error)) {
171163
// https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
172164
// "If the object is in non-blocking mode and no bytes are available,
173165
// None is returned."
@@ -192,7 +184,7 @@ mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, mp_uint_t len) {
192184
int error;
193185
mp_uint_t out_sz = o->type->stream_p->write(self_in, buf, len, &error);
194186
if (out_sz == MP_STREAM_ERROR) {
195-
if (is_nonblocking_error(error)) {
187+
if (mp_is_nonblocking_error(error)) {
196188
// http://docs.python.org/3/library/io.html#io.RawIOBase.write
197189
// "None is returned if the raw stream is set not to block and
198190
// no single byte could be readily written to it."
@@ -235,7 +227,7 @@ STATIC mp_obj_t stream_readinto(mp_uint_t n_args, const mp_obj_t *args) {
235227
int error;
236228
mp_uint_t out_sz = o->type->stream_p->read(o, bufinfo.buf, len, &error);
237229
if (out_sz == MP_STREAM_ERROR) {
238-
if (is_nonblocking_error(error)) {
230+
if (mp_is_nonblocking_error(error)) {
239231
return mp_const_none;
240232
}
241233
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
@@ -260,7 +252,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
260252
int error;
261253
mp_uint_t out_sz = o->type->stream_p->read(self_in, p, current_read, &error);
262254
if (out_sz == MP_STREAM_ERROR) {
263-
if (is_nonblocking_error(error)) {
255+
if (mp_is_nonblocking_error(error)) {
264256
// With non-blocking streams, we read as much as we can.
265257
// If we read nothing, return None, just like read().
266258
// Otherwise, return data read so far.
@@ -321,7 +313,7 @@ STATIC mp_obj_t stream_unbuffered_readline(mp_uint_t n_args, const mp_obj_t *arg
321313
int error;
322314
mp_uint_t out_sz = o->type->stream_p->read(o, p, 1, &error);
323315
if (out_sz == MP_STREAM_ERROR) {
324-
if (is_nonblocking_error(error)) {
316+
if (mp_is_nonblocking_error(error)) {
325317
if (vstr.len == 1) {
326318
// We just incremented it, but otherwise we read nothing
327319
// and immediately got EAGAIN. This is case is not well

py/stream.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self);
4242

4343
mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, mp_uint_t len);
4444

45+
#if MICROPY_STREAMS_NON_BLOCK
46+
// TODO: This is POSIX-specific (but then POSIX is the only real thing,
47+
// and anything else just emulates it, right?)
48+
#define mp_is_nonblocking_error(errno) ((errno) == EAGAIN || (errno) == EWOULDBLOCK)
49+
#else
50+
#define mp_is_nonblocking_error(errno) (0)
51+
#endif
52+
4553
#endif // __MICROPY_INCLUDED_PY_STREAM_H__

0 commit comments

Comments
 (0)