blob: 98f62518a5b08b05f43b8e26dfe3226617bae627 [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
Paul Sokolovskya47b64a2014-05-15 07:28:19 +030061#define STREAM_CONTENT_TYPE(stream) (((stream)->is_bytes) ? &mp_type_bytes : &mp_type_str)
62
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 George40f3c022014-07-03 13:25:24 +010070 mp_int_t sz;
Paul Sokolovskya671f892014-01-16 12:53:46 +020071 if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) {
72 return stream_readall(args[0]);
73 }
Paul Sokolovskyf5f6c3b2014-06-15 23:23:36 +030074
75 #if MICROPY_PY_BUILTINS_STR_UNICODE
76 if (!o->type->stream_p->is_bytes) {
77 mp_not_implemented("Reading from unicode text streams by character count");
78 }
79 #endif
80
Damien George5fa93b62014-01-22 14:35:10 +000081 byte *buf = m_new(byte, sz);
Paul Sokolovskye98cf402014-01-08 02:43:48 +020082 int error;
Damien George40f3c022014-07-03 13:25:24 +010083 mp_int_t out_sz = o->type->stream_p->read(o, buf, sz, &error);
Paul Sokolovskye98cf402014-01-08 02:43:48 +020084 if (out_sz == -1) {
Paul Sokolovskya5921042014-05-07 01:39:38 +030085 if (is_nonblocking_error(error)) {
86 // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
87 // "If the object is in non-blocking mode and no bytes are available,
88 // None is returned."
89 // This is actually very weird, as naive truth check will treat
90 // this as EOF.
91 return mp_const_none;
92 }
Damien Georgeea13f402014-04-05 18:32:08 +010093 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020094 } else {
Damien Georgef600a6a2014-05-25 22:34:34 +010095 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 +000096 m_free(buf, sz);
97 return s;
Paul Sokolovskye98cf402014-01-08 02:43:48 +020098 }
99}
100
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200101STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200102 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
Damien George27e735f2014-04-05 23:02:23 +0100103 if (o->type->stream_p == NULL || o->type->stream_p->write == NULL) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200104 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +0100105 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200106 }
107
Paul Sokolovsky45fb1432014-04-26 05:46:06 +0300108 mp_buffer_info_t bufinfo;
109 mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
110
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200111 int error;
Damien George40f3c022014-07-03 13:25:24 +0100112 mp_int_t out_sz = o->type->stream_p->write(self_in, bufinfo.buf, bufinfo.len, &error);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200113 if (out_sz == -1) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300114 if (is_nonblocking_error(error)) {
115 // http://docs.python.org/3/library/io.html#io.RawIOBase.write
116 // "None is returned if the raw stream is set not to block and
117 // no single byte could be readily written to it."
118 // This is for consistency with read() behavior, still weird,
119 // see abobe.
120 return mp_const_none;
121 }
Damien Georgeea13f402014-04-05 18:32:08 +0100122 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200123 } else {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200124 return MP_OBJ_NEW_SMALL_INT(out_sz);
125 }
126}
127
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200128STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
Paul Sokolovsky52254502014-01-13 23:25:33 +0200129 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
Damien George27e735f2014-04-05 23:02:23 +0100130 if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
Paul Sokolovsky52254502014-01-13 23:25:33 +0200131 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +0100132 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200133 }
134
135 int total_size = 0;
Paul Sokolovskyb9be45e2014-05-07 01:51:07 +0300136 vstr_t *vstr = vstr_new_size(DEFAULT_BUFFER_SIZE);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200137 char *buf = vstr_str(vstr);
138 char *p = buf;
139 int error;
Paul Sokolovskyb9be45e2014-05-07 01:51:07 +0300140 int current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200141 while (true) {
Damien George40f3c022014-07-03 13:25:24 +0100142 mp_int_t out_sz = o->type->stream_p->read(self_in, p, current_read, &error);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200143 if (out_sz == -1) {
Paul Sokolovsky6e731432014-05-07 01:48:12 +0300144 if (is_nonblocking_error(error)) {
145 // With non-blocking streams, we read as much as we can.
146 // If we read nothing, return None, just like read().
147 // Otherwise, return data read so far.
148 if (total_size == 0) {
149 return mp_const_none;
150 }
151 break;
152 }
Damien Georgeea13f402014-04-05 18:32:08 +0100153 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200154 }
155 if (out_sz == 0) {
156 break;
157 }
158 total_size += out_sz;
159 if (out_sz < current_read) {
160 current_read -= out_sz;
161 p += out_sz;
162 } else {
Paul Sokolovskyb9be45e2014-05-07 01:51:07 +0300163 current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200164 p = vstr_extend(vstr, current_read);
165 if (p == NULL) {
166 // TODO
Damien Georgeea13f402014-04-05 18:32:08 +0100167 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200168 }
169 }
170 }
Damien George5fa93b62014-01-22 14:35:10 +0000171
Damien Georgef600a6a2014-05-25 22:34:34 +0100172 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 +0000173 vstr_free(vstr);
174 return s;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200175}
176
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200177// Unbuffered, inefficient implementation of readline() for raw I/O files.
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200178STATIC mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200179 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
Damien George27e735f2014-04-05 23:02:23 +0100180 if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200181 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +0100182 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200183 }
184
Damien George40f3c022014-07-03 13:25:24 +0100185 mp_int_t max_size = -1;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200186 if (n_args > 1) {
187 max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
188 }
189
190 vstr_t *vstr;
191 if (max_size != -1) {
Damien George55baff42014-01-21 21:40:13 +0000192 vstr = vstr_new_size(max_size);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200193 } else {
194 vstr = vstr_new();
195 }
196
197 int error;
198 while (max_size == -1 || max_size-- != 0) {
199 char *p = vstr_add_len(vstr, 1);
200 if (p == NULL) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100201 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200202 }
203
Damien George40f3c022014-07-03 13:25:24 +0100204 mp_int_t out_sz = o->type->stream_p->read(o, p, 1, &error);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200205 if (out_sz == -1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100206 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200207 }
Paul Sokolovsky09143712014-01-22 10:34:45 +0200208 if (out_sz == 0) {
209 // Back out previously added byte
210 // TODO: This is a bit hacky, does it supported by vstr API contract?
211 // Consider, what's better - read a char and get OutOfMemory (so read
212 // char is lost), or allocate first as we do.
213 vstr_add_len(vstr, -1);
214 break;
215 }
216 if (*p == '\n') {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200217 break;
218 }
219 }
Damien Georged5f5b2f2014-05-03 22:01:32 +0100220 // TODO need a string creation API that doesn't copy the given data
Damien Georgef600a6a2014-05-25 22:34:34 +0100221 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 +0100222 vstr_free(vstr);
223 return ret;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200224}
225
Damien Georged5f5b2f2014-05-03 22:01:32 +0100226// TODO take an optional extra argument (what does it do exactly?)
227STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) {
228 mp_obj_t lines = mp_obj_new_list(0, NULL);
229 for (;;) {
230 mp_obj_t line = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300231 if (!mp_obj_is_true(line)) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100232 break;
233 }
234 mp_obj_list_append(lines, line);
235 }
236 return lines;
237}
238MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines);
239
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200240mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
241 mp_obj_t l_in = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300242 if (mp_obj_is_true(l_in)) {
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200243 return l_in;
244 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100245 return MP_OBJ_STOP_ITERATION;
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200246}
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200247
Paul Sokolovskya671f892014-01-16 12:53:46 +0200248MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200249MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200250MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200251MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write);