blob: 09c39bdf4a45d19ff7c0dfe38ea4f513704c4382 [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 Sokolovskyf5f6c3b2014-06-15 23:23:36 +030036#include "runtime.h"
Paul Sokolovskye98cf402014-01-08 02:43:48 +020037#include "stream.h"
Paul Sokolovsky0ef015b2014-05-07 02:23:46 +030038#if MICROPY_STREAMS_NON_BLOCK
39#include <errno.h>
stijnec6fa872014-06-28 21:04:20 +020040#if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
41#define EWOULDBLOCK 140
42#endif
Paul Sokolovsky0ef015b2014-05-07 02:23:46 +030043#endif
Paul Sokolovskye98cf402014-01-08 02:43:48 +020044
45// This file defines generic Python stream read/write methods which
46// dispatch to the underlying stream interface of an object.
47
Paul Sokolovskyb9be45e2014-05-07 01:51:07 +030048// TODO: should be in mpconfig.h
49#define DEFAULT_BUFFER_SIZE 256
50
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020051STATIC mp_obj_t stream_readall(mp_obj_t self_in);
Paul Sokolovskya671f892014-01-16 12:53:46 +020052
Paul Sokolovsky0ef015b2014-05-07 02:23:46 +030053#if MICROPY_STREAMS_NON_BLOCK
Paul Sokolovskya5921042014-05-07 01:39:38 +030054// TODO: This is POSIX-specific (but then POSIX is the only real thing,
55// and anything else just emulates it, right?)
56#define is_nonblocking_error(errno) ((errno) == EAGAIN || (errno) == EWOULDBLOCK)
Paul Sokolovsky0ef015b2014-05-07 02:23:46 +030057#else
58#define is_nonblocking_error(errno) (0)
59#endif
Paul Sokolovskya5921042014-05-07 01:39:38 +030060
Damien Georgeadf0f2a2014-07-27 22:38:58 +010061#define STREAM_CONTENT_TYPE(stream) (((stream)->is_text) ? &mp_type_str : &mp_type_bytes)
Paul Sokolovskya47b64a2014-05-15 07:28:19 +030062
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020063STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
Paul Sokolovskya671f892014-01-16 12:53:46 +020064 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
Damien George27e735f2014-04-05 23:02:23 +010065 if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +020066 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +010067 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020068 }
69
Damien George1694bc72014-07-16 11:45:10 +010070 // What to do if sz < -1? Python docs don't specify this case.
71 // CPython does a readall, but here we silently let negatives through,
72 // and they will cause a MemoryError.
Damien George40f3c022014-07-03 13:25:24 +010073 mp_int_t sz;
Paul Sokolovskya671f892014-01-16 12:53:46 +020074 if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) {
75 return stream_readall(args[0]);
76 }
Paul Sokolovskyf5f6c3b2014-06-15 23:23:36 +030077
78 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien Georgeadf0f2a2014-07-27 22:38:58 +010079 if (o->type->stream_p->is_text) {
Damien George1694bc72014-07-16 11:45:10 +010080 // We need to read sz number of unicode characters. Because we don't have any
81 // buffering, and because the stream API can only read bytes, we must read here
82 // in units of bytes and must never over read. If we want sz chars, then reading
83 // sz bytes will never over-read, so we follow this approach, in a loop to keep
84 // reading until we have exactly enough chars. This will be 1 read for text
85 // with ASCII-only chars, and about 2 reads for text with a couple of non-ASCII
86 // chars. For text with lots of non-ASCII chars, it'll be pretty inefficient
87 // in time and memory.
88
89 vstr_t vstr;
90 vstr_init(&vstr, sz);
91 mp_uint_t more_bytes = sz;
92 mp_uint_t last_buf_offset = 0;
93 while (more_bytes > 0) {
94 char *p = vstr_add_len(&vstr, more_bytes);
95 if (p == NULL) {
96 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
97 }
98 int error;
Damien Georgeadf0f2a2014-07-27 22:38:58 +010099 mp_uint_t out_sz = o->type->stream_p->read(o, p, more_bytes, &error);
100 if (out_sz == MP_STREAM_ERROR) {
Damien George1694bc72014-07-16 11:45:10 +0100101 vstr_cut_tail_bytes(&vstr, more_bytes);
102 if (is_nonblocking_error(error)) {
103 // With non-blocking streams, we read as much as we can.
104 // If we read nothing, return None, just like read().
105 // Otherwise, return data read so far.
106 // TODO what if we have read only half a non-ASCII char?
107 if (vstr.len == 0) {
108 vstr_clear(&vstr);
109 return mp_const_none;
110 }
111 break;
112 }
113 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
114 }
115
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100116 if (out_sz < more_bytes) {
Damien George1694bc72014-07-16 11:45:10 +0100117 // Finish reading.
118 // TODO what if we have read only half a non-ASCII char?
Dave Hylands1d8816c2014-07-21 16:51:07 -0700119 vstr_cut_tail_bytes(&vstr, more_bytes - out_sz);
120 if (out_sz == 0) {
121 break;
122 }
Damien George1694bc72014-07-16 11:45:10 +0100123 }
124
125 // count chars from bytes just read
126 for (mp_uint_t off = last_buf_offset;;) {
127 byte b = vstr.buf[off];
128 int n;
129 if (!UTF8_IS_NONASCII(b)) {
130 // 1-byte ASCII char
131 n = 1;
132 } else if ((b & 0xe0) == 0xc0) {
133 // 2-byte char
134 n = 2;
135 } else if ((b & 0xf0) == 0xe0) {
136 // 3-byte char
137 n = 3;
138 } else if ((b & 0xf8) == 0xf0) {
139 // 4-byte char
140 n = 4;
141 } else {
142 // TODO
143 n = 5;
144 }
145 if (off + n <= vstr.len) {
146 // got a whole char in n bytes
147 off += n;
148 sz -= 1;
149 last_buf_offset = off;
150 if (off >= vstr.len) {
151 more_bytes = sz;
152 break;
153 }
154 } else {
155 // didn't get a whole char, so work out how many extra bytes are needed for
156 // this partial char, plus bytes for additional chars that we want
157 more_bytes = (off + n - vstr.len) + (sz - 1);
158 break;
159 }
160 }
161 }
162
163 mp_obj_t ret = mp_obj_new_str_of_type(&mp_type_str, (byte*)vstr.buf, vstr.len);
164 vstr_clear(&vstr);
165 return ret;
Paul Sokolovskyf5f6c3b2014-06-15 23:23:36 +0300166 }
167 #endif
168
Damien George5fa93b62014-01-22 14:35:10 +0000169 byte *buf = m_new(byte, sz);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200170 int error;
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100171 mp_uint_t out_sz = o->type->stream_p->read(o, buf, sz, &error);
172 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300173 if (is_nonblocking_error(error)) {
174 // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
175 // "If the object is in non-blocking mode and no bytes are available,
176 // None is returned."
177 // This is actually very weird, as naive truth check will treat
178 // this as EOF.
179 return mp_const_none;
180 }
Damien Georgeea13f402014-04-05 18:32:08 +0100181 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200182 } else {
Damien Georgef600a6a2014-05-25 22:34:34 +0100183 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 +0000184 m_free(buf, sz);
185 return s;
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200186 }
187}
188
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300189mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, mp_uint_t len) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200190 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
Damien George27e735f2014-04-05 23:02:23 +0100191 if (o->type->stream_p == NULL || o->type->stream_p->write == NULL) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200192 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +0100193 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200194 }
195
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200196 int error;
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100197 mp_uint_t out_sz = o->type->stream_p->write(self_in, buf, len, &error);
198 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300199 if (is_nonblocking_error(error)) {
200 // http://docs.python.org/3/library/io.html#io.RawIOBase.write
201 // "None is returned if the raw stream is set not to block and
202 // no single byte could be readily written to it."
203 // This is for consistency with read() behavior, still weird,
204 // see abobe.
205 return mp_const_none;
206 }
Damien Georgeea13f402014-04-05 18:32:08 +0100207 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200208 } else {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200209 return MP_OBJ_NEW_SMALL_INT(out_sz);
210 }
211}
212
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300213STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) {
214 mp_buffer_info_t bufinfo;
215 mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
216 return mp_stream_write(self_in, bufinfo.buf, bufinfo.len);
217}
218
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200219STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
Paul Sokolovsky52254502014-01-13 23:25:33 +0200220 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
Damien George27e735f2014-04-05 23:02:23 +0100221 if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
Paul Sokolovsky52254502014-01-13 23:25:33 +0200222 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +0100223 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200224 }
225
226 int total_size = 0;
Paul Sokolovskyb9be45e2014-05-07 01:51:07 +0300227 vstr_t *vstr = vstr_new_size(DEFAULT_BUFFER_SIZE);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200228 char *buf = vstr_str(vstr);
229 char *p = buf;
230 int error;
Paul Sokolovskyb9be45e2014-05-07 01:51:07 +0300231 int current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200232 while (true) {
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100233 mp_uint_t out_sz = o->type->stream_p->read(self_in, p, current_read, &error);
234 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky6e731432014-05-07 01:48:12 +0300235 if (is_nonblocking_error(error)) {
236 // With non-blocking streams, we read as much as we can.
237 // If we read nothing, return None, just like read().
238 // Otherwise, return data read so far.
239 if (total_size == 0) {
240 return mp_const_none;
241 }
242 break;
243 }
Damien Georgeea13f402014-04-05 18:32:08 +0100244 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200245 }
246 if (out_sz == 0) {
247 break;
248 }
249 total_size += out_sz;
250 if (out_sz < current_read) {
251 current_read -= out_sz;
252 p += out_sz;
253 } else {
Paul Sokolovskyb9be45e2014-05-07 01:51:07 +0300254 current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200255 p = vstr_extend(vstr, current_read);
256 if (p == NULL) {
257 // TODO
Damien Georgeea13f402014-04-05 18:32:08 +0100258 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200259 }
260 }
261 }
Damien George5fa93b62014-01-22 14:35:10 +0000262
Damien Georgef600a6a2014-05-25 22:34:34 +0100263 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 +0000264 vstr_free(vstr);
265 return s;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200266}
267
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200268// Unbuffered, inefficient implementation of readline() for raw I/O files.
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200269STATIC mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200270 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
Damien George27e735f2014-04-05 23:02:23 +0100271 if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200272 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +0100273 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200274 }
275
Damien George40f3c022014-07-03 13:25:24 +0100276 mp_int_t max_size = -1;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200277 if (n_args > 1) {
278 max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
279 }
280
281 vstr_t *vstr;
282 if (max_size != -1) {
Damien George55baff42014-01-21 21:40:13 +0000283 vstr = vstr_new_size(max_size);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200284 } else {
285 vstr = vstr_new();
286 }
287
288 int error;
289 while (max_size == -1 || max_size-- != 0) {
290 char *p = vstr_add_len(vstr, 1);
291 if (p == NULL) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100292 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200293 }
294
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100295 mp_uint_t out_sz = o->type->stream_p->read(o, p, 1, &error);
296 if (out_sz == MP_STREAM_ERROR) {
Damien Georgeea13f402014-04-05 18:32:08 +0100297 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200298 }
Paul Sokolovsky09143712014-01-22 10:34:45 +0200299 if (out_sz == 0) {
300 // Back out previously added byte
Paul Sokolovsky09143712014-01-22 10:34:45 +0200301 // Consider, what's better - read a char and get OutOfMemory (so read
302 // char is lost), or allocate first as we do.
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100303 vstr_cut_tail_bytes(vstr, 1);
Paul Sokolovsky09143712014-01-22 10:34:45 +0200304 break;
305 }
306 if (*p == '\n') {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200307 break;
308 }
309 }
Damien Georged5f5b2f2014-05-03 22:01:32 +0100310 // TODO need a string creation API that doesn't copy the given data
Damien Georgef600a6a2014-05-25 22:34:34 +0100311 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 +0100312 vstr_free(vstr);
313 return ret;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200314}
315
Damien Georged5f5b2f2014-05-03 22:01:32 +0100316// TODO take an optional extra argument (what does it do exactly?)
317STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) {
318 mp_obj_t lines = mp_obj_new_list(0, NULL);
319 for (;;) {
320 mp_obj_t line = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300321 if (!mp_obj_is_true(line)) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100322 break;
323 }
324 mp_obj_list_append(lines, line);
325 }
326 return lines;
327}
328MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines);
329
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200330mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
331 mp_obj_t l_in = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300332 if (mp_obj_is_true(l_in)) {
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200333 return l_in;
334 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100335 return MP_OBJ_STOP_ITERATION;
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200336}
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200337
Paul Sokolovskya671f892014-01-16 12:53:46 +0200338MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200339MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200340MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300341MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write_method);