blob: 07a79248ab0b98a2b6b169981dcc84a711e2f892 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovskyda9f0922014-05-13 08:44:45 +03007 * Copyright (c) 2014 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
Ilya Dmitrichenko5630b012014-04-12 16:44:32 +010028#include <string.h>
Paul Sokolovskye98cf402014-01-08 02:43:48 +020029
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030030#include "mpconfig.h"
Paul Sokolovskye98cf402014-01-08 02:43:48 +020031#include "nlr.h"
32#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000033#include "qstr.h"
Paul Sokolovskye98cf402014-01-08 02:43:48 +020034#include "obj.h"
Paul Sokolovskya47b64a2014-05-15 07:28:19 +030035#include "objstr.h"
Paul Sokolovskye98cf402014-01-08 02:43:48 +020036#include "stream.h"
Paul Sokolovsky0ef015b2014-05-07 02:23:46 +030037#if MICROPY_STREAMS_NON_BLOCK
38#include <errno.h>
39#endif
Paul Sokolovskye98cf402014-01-08 02:43:48 +020040
41// This file defines generic Python stream read/write methods which
42// dispatch to the underlying stream interface of an object.
43
Paul Sokolovskyb9be45e2014-05-07 01:51:07 +030044// TODO: should be in mpconfig.h
45#define DEFAULT_BUFFER_SIZE 256
46
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020047STATIC mp_obj_t stream_readall(mp_obj_t self_in);
Paul Sokolovskya671f892014-01-16 12:53:46 +020048
Paul Sokolovsky0ef015b2014-05-07 02:23:46 +030049#if MICROPY_STREAMS_NON_BLOCK
Paul Sokolovskya5921042014-05-07 01:39:38 +030050// TODO: This is POSIX-specific (but then POSIX is the only real thing,
51// and anything else just emulates it, right?)
52#define is_nonblocking_error(errno) ((errno) == EAGAIN || (errno) == EWOULDBLOCK)
Paul Sokolovsky0ef015b2014-05-07 02:23:46 +030053#else
54#define is_nonblocking_error(errno) (0)
55#endif
Paul Sokolovskya5921042014-05-07 01:39:38 +030056
Paul Sokolovskya47b64a2014-05-15 07:28:19 +030057#define STREAM_CONTENT_TYPE(stream) (((stream)->is_bytes) ? &mp_type_bytes : &mp_type_str)
58
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020059STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
Paul Sokolovskya671f892014-01-16 12:53:46 +020060 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
Damien George27e735f2014-04-05 23:02:23 +010061 if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +020062 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +010063 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020064 }
65
Paul Sokolovskya671f892014-01-16 12:53:46 +020066 machine_int_t sz;
67 if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) {
68 return stream_readall(args[0]);
69 }
Damien George5fa93b62014-01-22 14:35:10 +000070 byte *buf = m_new(byte, sz);
Paul Sokolovskye98cf402014-01-08 02:43:48 +020071 int error;
Damien George27e735f2014-04-05 23:02:23 +010072 machine_int_t out_sz = o->type->stream_p->read(o, buf, sz, &error);
Paul Sokolovskye98cf402014-01-08 02:43:48 +020073 if (out_sz == -1) {
Paul Sokolovskya5921042014-05-07 01:39:38 +030074 if (is_nonblocking_error(error)) {
75 // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
76 // "If the object is in non-blocking mode and no bytes are available,
77 // None is returned."
78 // This is actually very weird, as naive truth check will treat
79 // this as EOF.
80 return mp_const_none;
81 }
Damien Georgeea13f402014-04-05 18:32:08 +010082 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020083 } else {
Damien Georgef600a6a2014-05-25 22:34:34 +010084 mp_obj_t s = mp_obj_new_str_of_type(STREAM_CONTENT_TYPE(o->type->stream_p), buf, out_sz); // will reallocate to use exact size
Damien George5fa93b62014-01-22 14:35:10 +000085 m_free(buf, sz);
86 return s;
Paul Sokolovskye98cf402014-01-08 02:43:48 +020087 }
88}
89
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020090STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +020091 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
Damien George27e735f2014-04-05 23:02:23 +010092 if (o->type->stream_p == NULL || o->type->stream_p->write == NULL) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +020093 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +010094 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020095 }
96
Paul Sokolovsky45fb1432014-04-26 05:46:06 +030097 mp_buffer_info_t bufinfo;
98 mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
99
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200100 int error;
Paul Sokolovsky45fb1432014-04-26 05:46:06 +0300101 machine_int_t out_sz = o->type->stream_p->write(self_in, bufinfo.buf, bufinfo.len, &error);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200102 if (out_sz == -1) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300103 if (is_nonblocking_error(error)) {
104 // http://docs.python.org/3/library/io.html#io.RawIOBase.write
105 // "None is returned if the raw stream is set not to block and
106 // no single byte could be readily written to it."
107 // This is for consistency with read() behavior, still weird,
108 // see abobe.
109 return mp_const_none;
110 }
Damien Georgeea13f402014-04-05 18:32:08 +0100111 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200112 } else {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200113 return MP_OBJ_NEW_SMALL_INT(out_sz);
114 }
115}
116
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200117STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
Paul Sokolovsky52254502014-01-13 23:25:33 +0200118 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
Damien George27e735f2014-04-05 23:02:23 +0100119 if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
Paul Sokolovsky52254502014-01-13 23:25:33 +0200120 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +0100121 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200122 }
123
124 int total_size = 0;
Paul Sokolovskyb9be45e2014-05-07 01:51:07 +0300125 vstr_t *vstr = vstr_new_size(DEFAULT_BUFFER_SIZE);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200126 char *buf = vstr_str(vstr);
127 char *p = buf;
128 int error;
Paul Sokolovskyb9be45e2014-05-07 01:51:07 +0300129 int current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200130 while (true) {
Damien George27e735f2014-04-05 23:02:23 +0100131 machine_int_t out_sz = o->type->stream_p->read(self_in, p, current_read, &error);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200132 if (out_sz == -1) {
Paul Sokolovsky6e731432014-05-07 01:48:12 +0300133 if (is_nonblocking_error(error)) {
134 // With non-blocking streams, we read as much as we can.
135 // If we read nothing, return None, just like read().
136 // Otherwise, return data read so far.
137 if (total_size == 0) {
138 return mp_const_none;
139 }
140 break;
141 }
Damien Georgeea13f402014-04-05 18:32:08 +0100142 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200143 }
144 if (out_sz == 0) {
145 break;
146 }
147 total_size += out_sz;
148 if (out_sz < current_read) {
149 current_read -= out_sz;
150 p += out_sz;
151 } else {
Paul Sokolovskyb9be45e2014-05-07 01:51:07 +0300152 current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200153 p = vstr_extend(vstr, current_read);
154 if (p == NULL) {
155 // TODO
Damien Georgeea13f402014-04-05 18:32:08 +0100156 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200157 }
158 }
159 }
Damien George5fa93b62014-01-22 14:35:10 +0000160
Damien Georgef600a6a2014-05-25 22:34:34 +0100161 mp_obj_t s = mp_obj_new_str_of_type(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, total_size);
Damien George5fa93b62014-01-22 14:35:10 +0000162 vstr_free(vstr);
163 return s;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200164}
165
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200166// Unbuffered, inefficient implementation of readline() for raw I/O files.
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200167STATIC mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200168 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
Damien George27e735f2014-04-05 23:02:23 +0100169 if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200170 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +0100171 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200172 }
173
174 machine_int_t max_size = -1;
175 if (n_args > 1) {
176 max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
177 }
178
179 vstr_t *vstr;
180 if (max_size != -1) {
Damien George55baff42014-01-21 21:40:13 +0000181 vstr = vstr_new_size(max_size);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200182 } else {
183 vstr = vstr_new();
184 }
185
186 int error;
187 while (max_size == -1 || max_size-- != 0) {
188 char *p = vstr_add_len(vstr, 1);
189 if (p == NULL) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100190 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200191 }
192
Damien George27e735f2014-04-05 23:02:23 +0100193 machine_int_t out_sz = o->type->stream_p->read(o, p, 1, &error);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200194 if (out_sz == -1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100195 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200196 }
Paul Sokolovsky09143712014-01-22 10:34:45 +0200197 if (out_sz == 0) {
198 // Back out previously added byte
199 // TODO: This is a bit hacky, does it supported by vstr API contract?
200 // Consider, what's better - read a char and get OutOfMemory (so read
201 // char is lost), or allocate first as we do.
202 vstr_add_len(vstr, -1);
203 break;
204 }
205 if (*p == '\n') {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200206 break;
207 }
208 }
Damien Georged5f5b2f2014-05-03 22:01:32 +0100209 // TODO need a string creation API that doesn't copy the given data
Damien Georgef600a6a2014-05-25 22:34:34 +0100210 mp_obj_t ret = mp_obj_new_str_of_type(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, vstr->len);
Damien Georged5f5b2f2014-05-03 22:01:32 +0100211 vstr_free(vstr);
212 return ret;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200213}
214
Damien Georged5f5b2f2014-05-03 22:01:32 +0100215// TODO take an optional extra argument (what does it do exactly?)
216STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) {
217 mp_obj_t lines = mp_obj_new_list(0, NULL);
218 for (;;) {
219 mp_obj_t line = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300220 if (!mp_obj_is_true(line)) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100221 break;
222 }
223 mp_obj_list_append(lines, line);
224 }
225 return lines;
226}
227MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines);
228
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200229mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
230 mp_obj_t l_in = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300231 if (mp_obj_is_true(l_in)) {
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200232 return l_in;
233 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100234 return MP_OBJ_STOP_ITERATION;
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200235}
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200236
Paul Sokolovskya671f892014-01-16 12:53:46 +0200237MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200238MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200239MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200240MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write);