Skip to content

Commit a671f89

Browse files
committed
Make file.read() and file.read(-1) call out to file.readall().
Per Python3 io module semantics.
1 parent 323c09e commit a671f89

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

py/stream.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@
1010
// This file defines generic Python stream read/write methods which
1111
// dispatch to the underlying stream interface of an object.
1212

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];
1517
if (o->type->stream_p.read == NULL) {
1618
// CPython: io.UnsupportedOperation, OSError subclass
1719
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported"));
1820
}
1921

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+
}
2126
// +1 because so far we mark end of string with \0
2227
char *buf = m_new(char, sz + 1);
2328
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);
2530
if (out_sz == -1) {
2631
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error));
2732
} else {
@@ -136,7 +141,7 @@ static mp_obj_t stream_unbuffered_readline(int n_args, const mp_obj_t *args) {
136141
}
137142

138143

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);
140145
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
141146
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
142147
MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write);

0 commit comments

Comments
 (0)