summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-01-16 12:53:46 +0200
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-01-16 12:58:36 +0200
commita671f891ddb42496accb889e19a0eb14b50caba2 (patch)
treec291987863112b1f57e6f774df381d717faa56b4 /py
parent323c09e881558fe23d416dea1cda30cb0a2b8309 (diff)
downloadmicropython-a671f891ddb42496accb889e19a0eb14b50caba2.tar.gz
micropython-a671f891ddb42496accb889e19a0eb14b50caba2.zip
Make file.read() and file.read(-1) call out to file.readall().
Per Python3 io module semantics.
Diffstat (limited to 'py')
-rw-r--r--py/stream.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/py/stream.c b/py/stream.c
index 0e2cccad33..d5884ae819 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -10,18 +10,23 @@
// This file defines generic Python stream read/write methods which
// dispatch to the underlying stream interface of an object.
-static mp_obj_t stream_read(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_readall(mp_obj_t self_in);
+
+static mp_obj_t stream_read(int 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.read == NULL) {
// CPython: io.UnsupportedOperation, OSError subclass
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported"));
}
- machine_int_t sz = mp_obj_get_int(arg);
+ machine_int_t sz;
+ if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) {
+ return stream_readall(args[0]);
+ }
// +1 because so far we mark end of string with \0
char *buf = m_new(char, sz + 1);
int error;
- machine_int_t out_sz = o->type->stream_p.read(self_in, buf, sz, &error);
+ machine_int_t out_sz = o->type->stream_p.read(o, buf, sz, &error);
if (out_sz == -1) {
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error));
} else {
@@ -136,7 +141,7 @@ static mp_obj_t stream_unbuffered_readline(int n_args, const mp_obj_t *args) {
}
-MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_read_obj, stream_read);
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
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);