|
10 | 10 | // This file defines generic Python stream read/write methods which |
11 | 11 | // dispatch to the underlying stream interface of an object. |
12 | 12 |
|
13 | | -static mp_obj_t stream_read(mp_obj_t self_in, mp_obj_t arg) { |
14 | | - struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in; |
| 13 | +static mp_obj_t stream_readall(mp_obj_t self_in); |
| 14 | + |
| 15 | +static mp_obj_t stream_read(int n_args, const mp_obj_t *args) { |
| 16 | + struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0]; |
15 | 17 | if (o->type->stream_p.read == NULL) { |
16 | 18 | // CPython: io.UnsupportedOperation, OSError subclass |
17 | 19 | nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported")); |
18 | 20 | } |
19 | 21 |
|
20 | | - machine_int_t sz = mp_obj_get_int(arg); |
| 22 | + machine_int_t sz; |
| 23 | + if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) { |
| 24 | + return stream_readall(args[0]); |
| 25 | + } |
21 | 26 | // +1 because so far we mark end of string with \0 |
22 | 27 | char *buf = m_new(char, sz + 1); |
23 | 28 | int error; |
24 | | - machine_int_t out_sz = o->type->stream_p.read(self_in, buf, sz, &error); |
| 29 | + machine_int_t out_sz = o->type->stream_p.read(o, buf, sz, &error); |
25 | 30 | if (out_sz == -1) { |
26 | 31 | nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error)); |
27 | 32 | } else { |
@@ -136,7 +141,7 @@ static mp_obj_t stream_unbuffered_readline(int n_args, const mp_obj_t *args) { |
136 | 141 | } |
137 | 142 |
|
138 | 143 |
|
139 | | -MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_read_obj, stream_read); |
| 144 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read); |
140 | 145 | MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall); |
141 | 146 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline); |
142 | 147 | MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write); |
0 commit comments