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().
This commit is contained in:
parent
185cb0d943
commit
e2f8d98525
21
py/stream.c
21
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);
|
||||
|
10
tests/io/file_readinto_len.py
Normal file
10
tests/io/file_readinto_len.py
Normal file
@ -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)
|
4
tests/io/file_readinto_len.py.exp
Normal file
4
tests/io/file_readinto_len.py.exp
Normal file
@ -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')
|
Loading…
Reference in New Issue
Block a user