From e2f8d98525ddc9ced9bbda3614de0b9b9d6bc291 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 23 Oct 2014 21:22:08 +0300 Subject: [PATCH] stream: Add optional 2nd "length" arg to .readinto() - extension to CPython. While extension to file.readinto() definition of CPython, the additional arg is similar to what in CPython available in socket.recv_into(). --- py/stream.c | 21 ++++++++++++++++----- tests/io/file_readinto_len.py | 10 ++++++++++ tests/io/file_readinto_len.py.exp | 4 ++++ 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 tests/io/file_readinto_len.py create mode 100644 tests/io/file_readinto_len.py.exp diff --git a/py/stream.c b/py/stream.c index 7d080331dd..beb667668a 100644 --- a/py/stream.c +++ b/py/stream.c @@ -215,17 +215,28 @@ 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; +STATIC mp_obj_t stream_readinto(uint n_args, const mp_obj_t *args) { + struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0]; 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); + mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE); + + // CPython extension: if 2nd arg is provided, that's max len to read, + // instead of full buffer. Similar to + // https://docs.python.org/3/library/socket.html#socket.socket.recv_into + mp_uint_t len = bufinfo.len; + if (n_args > 2) { + len = mp_obj_int_get(args[2]); + if (len > bufinfo.len) { + len = bufinfo.len; + } + } int error; - mp_uint_t out_sz = o->type->stream_p->read(o, bufinfo.buf, bufinfo.len, &error); + mp_uint_t out_sz = o->type->stream_p->read(o, bufinfo.buf, len, &error); if (out_sz == MP_STREAM_ERROR) { if (is_nonblocking_error(error)) { return mp_const_none; @@ -370,7 +381,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_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, 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/tests/io/file_readinto_len.py b/tests/io/file_readinto_len.py new file mode 100644 index 0000000000..84cc8cf5e1 --- /dev/null +++ b/tests/io/file_readinto_len.py @@ -0,0 +1,10 @@ +b = bytearray(30) +f = open("io/data/file1", "rb") +# 2nd arg (length to read) is extension to CPython +print(f.readinto(b, 8)) +print(b) + +b = bytearray(4) +f = open("io/data/file1", "rb") +print(f.readinto(b, 8)) +print(b) diff --git a/tests/io/file_readinto_len.py.exp b/tests/io/file_readinto_len.py.exp new file mode 100644 index 0000000000..a7877115eb --- /dev/null +++ b/tests/io/file_readinto_len.py.exp @@ -0,0 +1,4 @@ +8 +bytearray(b'longer l\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +4 +bytearray(b'long')