diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 3252e35ad4..2c2c45b5be 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -437,6 +437,7 @@ Q(BF_LEN) #if MICROPY_PY_IO Q(_io) Q(readall) +Q(readinto) Q(readline) Q(readlines) Q(FileIO) diff --git a/py/stream.c b/py/stream.c index a0940599cd..7d080331dd 100644 --- a/py/stream.c +++ b/py/stream.c @@ -215,6 +215,27 @@ STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) { return mp_stream_write(self_in, bufinfo.buf, bufinfo.len); } +STATIC mp_obj_t stream_readinto(mp_obj_t self_in, mp_obj_t arg) { + struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in; + if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) { + // CPython: io.UnsupportedOperation, OSError subclass + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported")); + } + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_WRITE); + + int error; + mp_uint_t out_sz = o->type->stream_p->read(o, bufinfo.buf, bufinfo.len, &error); + if (out_sz == MP_STREAM_ERROR) { + if (is_nonblocking_error(error)) { + return mp_const_none; + } + nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error))); + } else { + return MP_OBJ_NEW_SMALL_INT(out_sz); + } +} + STATIC mp_obj_t stream_readall(mp_obj_t self_in) { struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in; if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) { @@ -349,6 +370,7 @@ mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) { } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read); +MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_readinto_obj, stream_readinto); MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline); MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write_method); diff --git a/py/stream.h b/py/stream.h index 4cdc1b4dc5..733c48e33b 100644 --- a/py/stream.h +++ b/py/stream.h @@ -25,6 +25,7 @@ */ MP_DECLARE_CONST_FUN_OBJ(mp_stream_read_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_stream_readinto_obj); MP_DECLARE_CONST_FUN_OBJ(mp_stream_readall_obj); MP_DECLARE_CONST_FUN_OBJ(mp_stream_unbuffered_readline_obj); MP_DECLARE_CONST_FUN_OBJ(mp_stream_unbuffered_readlines_obj); diff --git a/stmhal/file.c b/stmhal/file.c index 99e3cefd03..d49a8a259b 100644 --- a/stmhal/file.c +++ b/stmhal/file.c @@ -222,6 +222,7 @@ STATIC mp_obj_t file_obj_make_new(mp_obj_t type, mp_uint_t n_args, mp_uint_t n_k STATIC const mp_map_elem_t rawfile_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj}, { MP_OBJ_NEW_QSTR(MP_QSTR_readlines), (mp_obj_t)&mp_stream_unbuffered_readlines_obj}, { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, diff --git a/tests/io/data/file2 b/tests/io/data/file2 new file mode 100644 index 0000000000..274c0052dd --- /dev/null +++ b/tests/io/data/file2 @@ -0,0 +1 @@ +1234 \ No newline at end of file diff --git a/tests/io/file_readinto.py b/tests/io/file_readinto.py new file mode 100644 index 0000000000..7a0603377a --- /dev/null +++ b/tests/io/file_readinto.py @@ -0,0 +1,7 @@ +b = bytearray(30) +f = open("io/data/file1", "rb") +print(f.readinto(b)) +print(b) +f = open("io/data/file2", "rb") +print(f.readinto(b)) +print(b) diff --git a/unix/file.c b/unix/file.c index 26c5be9c1b..bb7ed247f0 100644 --- a/unix/file.c +++ b/unix/file.c @@ -177,6 +177,7 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_fileno), (mp_obj_t)&fdfile_fileno_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj}, { MP_OBJ_NEW_QSTR(MP_QSTR_readlines), (mp_obj_t)&mp_stream_unbuffered_readlines_obj}, { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, diff --git a/unix/modsocket.c b/unix/modsocket.c index 483b6c8004..7f9184db64 100644 --- a/unix/modsocket.c +++ b/unix/modsocket.c @@ -289,6 +289,7 @@ STATIC const mp_map_elem_t usocket_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_makefile), (mp_obj_t)&socket_makefile_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj}, { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&socket_connect_obj },