From bd6ca15444b9bdb8330b45433a848838fa3f2ddb Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 20 Apr 2020 23:57:13 +1000 Subject: [PATCH] py/modio: Allow uio.IOBase streams to return errno for read/write error. This allows user code that inherits from uio.IOBase to return an errno error code from the user readinto/write function, by returning a negative value. Eg returning -123 means an errno of 123. This is already how the custom ioctl works. --- py/modio.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/py/modio.c b/py/modio.c index 14f5f21d6d..8a18357e24 100644 --- a/py/modio.c +++ b/py/modio.c @@ -59,12 +59,17 @@ STATIC mp_uint_t iobase_read_write(mp_obj_t obj, void *buf, mp_uint_t size, int mp_load_method(obj, qst, dest); mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, size, buf}; dest[2] = MP_OBJ_FROM_PTR(&ar); - mp_obj_t ret = mp_call_method_n_kw(1, 0, dest); - if (ret == mp_const_none) { + mp_obj_t ret_obj = mp_call_method_n_kw(1, 0, dest); + if (ret_obj == mp_const_none) { *errcode = MP_EAGAIN; return MP_STREAM_ERROR; + } + mp_int_t ret = mp_obj_get_int(ret_obj); + if (ret >= 0) { + return ret; } else { - return mp_obj_get_int(ret); + *errcode = -ret; + return MP_STREAM_ERROR; } } STATIC mp_uint_t iobase_read(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode) {