Skip to content

Commit e2f8d98

Browse files
committed
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().
1 parent 185cb0d commit e2f8d98

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

py/stream.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,28 @@ STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) {
215215
return mp_stream_write(self_in, bufinfo.buf, bufinfo.len);
216216
}
217217

218-
STATIC mp_obj_t stream_readinto(mp_obj_t self_in, mp_obj_t arg) {
219-
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
218+
STATIC mp_obj_t stream_readinto(uint n_args, const mp_obj_t *args) {
219+
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
220220
if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
221221
// CPython: io.UnsupportedOperation, OSError subclass
222222
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
223223
}
224224
mp_buffer_info_t bufinfo;
225-
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_WRITE);
225+
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
226+
227+
// CPython extension: if 2nd arg is provided, that's max len to read,
228+
// instead of full buffer. Similar to
229+
// https://docs.python.org/3/library/socket.html#socket.socket.recv_into
230+
mp_uint_t len = bufinfo.len;
231+
if (n_args > 2) {
232+
len = mp_obj_int_get(args[2]);
233+
if (len > bufinfo.len) {
234+
len = bufinfo.len;
235+
}
236+
}
226237

227238
int error;
228-
mp_uint_t out_sz = o->type->stream_p->read(o, bufinfo.buf, bufinfo.len, &error);
239+
mp_uint_t out_sz = o->type->stream_p->read(o, bufinfo.buf, len, &error);
229240
if (out_sz == MP_STREAM_ERROR) {
230241
if (is_nonblocking_error(error)) {
231242
return mp_const_none;
@@ -370,7 +381,7 @@ mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
370381
}
371382

372383
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
373-
MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_readinto_obj, stream_readinto);
384+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
374385
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
375386
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
376387
MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write_method);

tests/io/file_readinto_len.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
b = bytearray(30)
2+
f = open("io/data/file1", "rb")
3+
# 2nd arg (length to read) is extension to CPython
4+
print(f.readinto(b, 8))
5+
print(b)
6+
7+
b = bytearray(4)
8+
f = open("io/data/file1", "rb")
9+
print(f.readinto(b, 8))
10+
print(b)

tests/io/file_readinto_len.py.exp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
8
2+
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')
3+
4
4+
bytearray(b'long')

0 commit comments

Comments
 (0)