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.
This commit is contained in:
Damien George 2020-04-20 23:57:13 +10:00
parent e08ca78f40
commit bd6ca15444
1 changed files with 8 additions and 3 deletions

View File

@ -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) {