blob: c915110e0b95bca2d59632bdadd6c8eae541817c [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>
blmorrisbdd78c32015-08-04 19:45:30 -040029#include <unistd.h>
Paul Sokolovskye98cf402014-01-08 02:43:48 +020030
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/nlr.h"
32#include "py/objstr.h"
33#include "py/stream.h"
Paul Sokolovsky497660f2016-05-20 21:16:58 +030034#include "py/runtime.h"
Damien George51dfcb42015-01-01 20:27:54 +000035
Paul Sokolovsky0ef015b2014-05-07 02:23:46 +030036#if MICROPY_STREAMS_NON_BLOCK
37#include <errno.h>
stijnec6fa872014-06-28 21:04:20 +020038#if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
39#define EWOULDBLOCK 140
40#endif
Paul Sokolovsky0ef015b2014-05-07 02:23:46 +030041#endif
Paul Sokolovskye98cf402014-01-08 02:43:48 +020042
43// This file defines generic Python stream read/write methods which
44// dispatch to the underlying stream interface of an object.
45
Paul Sokolovskyb9be45e2014-05-07 01:51:07 +030046// TODO: should be in mpconfig.h
47#define DEFAULT_BUFFER_SIZE 256
48
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020049STATIC mp_obj_t stream_readall(mp_obj_t self_in);
Paul Sokolovskya671f892014-01-16 12:53:46 +020050
Damien Georgeadf0f2a2014-07-27 22:38:58 +010051#define STREAM_CONTENT_TYPE(stream) (((stream)->is_text) ? &mp_type_str : &mp_type_bytes)
Paul Sokolovskya47b64a2014-05-15 07:28:19 +030052
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +030053// Returns error condition in *errcode, if non-zero, return value is number of bytes written
54// before error condition occured. If *errcode == 0, returns total bytes written (which will
55// be equal to input size).
56mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf_, mp_uint_t size, int *errcode, byte flags) {
57 byte *buf = buf_;
58 mp_obj_base_t* s = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
59 typedef mp_uint_t (*io_func_t)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
60 io_func_t io_func;
Paul Sokolovsky07209f82016-06-18 18:19:24 +030061 const mp_stream_p_t *stream_p = s->type->protocol;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +030062 if (flags & MP_STREAM_RW_WRITE) {
Paul Sokolovsky07209f82016-06-18 18:19:24 +030063 io_func = (io_func_t)stream_p->write;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +030064 } else {
Paul Sokolovsky07209f82016-06-18 18:19:24 +030065 io_func = stream_p->read;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +030066 }
67
68 *errcode = 0;
69 mp_uint_t done = 0;
70 while (size > 0) {
71 mp_uint_t out_sz = io_func(stream, buf, size, errcode);
72 // For read, out_sz == 0 means EOF. For write, it's unspecified
73 // what it means, but we don't make any progress, so returning
74 // is still the best option.
75 if (out_sz == 0) {
76 return done;
77 }
78 if (out_sz == MP_STREAM_ERROR) {
79 // If we read something before getting EAGAIN, don't leak it
80 if (mp_is_nonblocking_error(*errcode) && done != 0) {
81 *errcode = 0;
82 }
83 return done;
84 }
85 if (flags & MP_STREAM_RW_ONCE) {
86 return out_sz;
87 }
88
89 buf += out_sz;
90 size -= out_sz;
91 done += out_sz;
92 }
93 return done;
94}
95
Damien Georgee84325b2015-12-08 23:34:59 +000096const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) {
Krzysztof Blazewicz65620762016-08-20 13:45:25 +020097 mp_obj_type_t *type = mp_obj_get_type(self_in);
98 const mp_stream_p_t *stream_p = type->protocol;
Damien Georgee84325b2015-12-08 23:34:59 +000099 if (stream_p == NULL
100 || ((flags & MP_STREAM_OP_READ) && stream_p->read == NULL)
101 || ((flags & MP_STREAM_OP_WRITE) && stream_p->write == NULL)
102 || ((flags & MP_STREAM_OP_IOCTL) && stream_p->ioctl == NULL)) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200103 // CPython: io.UnsupportedOperation, OSError subclass
Damien George7d0d7212016-10-17 12:17:37 +1100104 mp_raise_msg(&mp_type_OSError, "stream operation not supported");
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200105 }
Damien Georgee84325b2015-12-08 23:34:59 +0000106 return stream_p;
107}
108
Paul Sokolovsky497660f2016-05-20 21:16:58 +0300109mp_obj_t mp_stream_close(mp_obj_t stream) {
110 // TODO: Still consider using ioctl for close
111 mp_obj_t dest[2];
112 mp_load_method(stream, MP_QSTR_close, dest);
113 return mp_call_method_n_kw(0, 0, dest);
114}
115
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300116STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte flags) {
Damien Georgee84325b2015-12-08 23:34:59 +0000117 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200118
Damien George1694bc72014-07-16 11:45:10 +0100119 // What to do if sz < -1? Python docs don't specify this case.
120 // CPython does a readall, but here we silently let negatives through,
121 // and they will cause a MemoryError.
Damien George40f3c022014-07-03 13:25:24 +0100122 mp_int_t sz;
Paul Sokolovskya671f892014-01-16 12:53:46 +0200123 if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) {
124 return stream_readall(args[0]);
125 }
Paul Sokolovskyf5f6c3b2014-06-15 23:23:36 +0300126
127 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien Georgee84325b2015-12-08 23:34:59 +0000128 if (stream_p->is_text) {
Damien George1694bc72014-07-16 11:45:10 +0100129 // We need to read sz number of unicode characters. Because we don't have any
130 // buffering, and because the stream API can only read bytes, we must read here
131 // in units of bytes and must never over read. If we want sz chars, then reading
132 // sz bytes will never over-read, so we follow this approach, in a loop to keep
133 // reading until we have exactly enough chars. This will be 1 read for text
134 // with ASCII-only chars, and about 2 reads for text with a couple of non-ASCII
135 // chars. For text with lots of non-ASCII chars, it'll be pretty inefficient
136 // in time and memory.
137
138 vstr_t vstr;
139 vstr_init(&vstr, sz);
140 mp_uint_t more_bytes = sz;
141 mp_uint_t last_buf_offset = 0;
142 while (more_bytes > 0) {
143 char *p = vstr_add_len(&vstr, more_bytes);
144 if (p == NULL) {
145 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
146 }
147 int error;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300148 mp_uint_t out_sz = mp_stream_read_exactly(args[0], p, more_bytes, &error);
149 if (error != 0) {
Damien George1694bc72014-07-16 11:45:10 +0100150 vstr_cut_tail_bytes(&vstr, more_bytes);
Paul Sokolovsky77994102015-10-18 15:37:19 +0300151 if (mp_is_nonblocking_error(error)) {
Damien George1694bc72014-07-16 11:45:10 +0100152 // With non-blocking streams, we read as much as we can.
153 // If we read nothing, return None, just like read().
154 // Otherwise, return data read so far.
155 // TODO what if we have read only half a non-ASCII char?
156 if (vstr.len == 0) {
157 vstr_clear(&vstr);
158 return mp_const_none;
159 }
160 break;
161 }
Damien George3a0a7712016-10-07 13:31:59 +1100162 mp_raise_OSError(error);
Damien George1694bc72014-07-16 11:45:10 +0100163 }
164
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100165 if (out_sz < more_bytes) {
Damien George1694bc72014-07-16 11:45:10 +0100166 // Finish reading.
167 // TODO what if we have read only half a non-ASCII char?
Dave Hylands1d8816c2014-07-21 16:51:07 -0700168 vstr_cut_tail_bytes(&vstr, more_bytes - out_sz);
169 if (out_sz == 0) {
Krzysztof Blazewicz65620762016-08-20 13:45:25 +0200170 break;
Dave Hylands1d8816c2014-07-21 16:51:07 -0700171 }
Damien George1694bc72014-07-16 11:45:10 +0100172 }
173
174 // count chars from bytes just read
175 for (mp_uint_t off = last_buf_offset;;) {
176 byte b = vstr.buf[off];
177 int n;
178 if (!UTF8_IS_NONASCII(b)) {
179 // 1-byte ASCII char
180 n = 1;
181 } else if ((b & 0xe0) == 0xc0) {
182 // 2-byte char
183 n = 2;
184 } else if ((b & 0xf0) == 0xe0) {
185 // 3-byte char
186 n = 3;
187 } else if ((b & 0xf8) == 0xf0) {
188 // 4-byte char
189 n = 4;
190 } else {
191 // TODO
192 n = 5;
193 }
194 if (off + n <= vstr.len) {
195 // got a whole char in n bytes
196 off += n;
197 sz -= 1;
198 last_buf_offset = off;
199 if (off >= vstr.len) {
200 more_bytes = sz;
201 break;
202 }
203 } else {
204 // didn't get a whole char, so work out how many extra bytes are needed for
205 // this partial char, plus bytes for additional chars that we want
206 more_bytes = (off + n - vstr.len) + (sz - 1);
207 break;
208 }
209 }
210 }
211
Damien George0b9ee862015-01-21 19:14:25 +0000212 return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
Paul Sokolovskyf5f6c3b2014-06-15 23:23:36 +0300213 }
214 #endif
215
Damien George05005f62015-01-21 22:48:37 +0000216 vstr_t vstr;
217 vstr_init_len(&vstr, sz);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200218 int error;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300219 mp_uint_t out_sz = mp_stream_rw(args[0], vstr.buf, sz, &error, flags);
220 if (error != 0) {
Damien George05005f62015-01-21 22:48:37 +0000221 vstr_clear(&vstr);
Paul Sokolovsky77994102015-10-18 15:37:19 +0300222 if (mp_is_nonblocking_error(error)) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300223 // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
224 // "If the object is in non-blocking mode and no bytes are available,
225 // None is returned."
226 // This is actually very weird, as naive truth check will treat
227 // this as EOF.
228 return mp_const_none;
229 }
Damien George3a0a7712016-10-07 13:31:59 +1100230 mp_raise_OSError(error);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200231 } else {
Damien George05005f62015-01-21 22:48:37 +0000232 vstr.len = out_sz;
Damien Georgee84325b2015-12-08 23:34:59 +0000233 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200234 }
235}
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300236
237STATIC mp_obj_t stream_read(size_t n_args, const mp_obj_t *args) {
238 return stream_read_generic(n_args, args, MP_STREAM_RW_READ);
239}
Damien George358e5d82016-04-10 12:41:28 +0100240MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200241
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300242STATIC mp_obj_t stream_read1(size_t n_args, const mp_obj_t *args) {
243 return stream_read_generic(n_args, args, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);
244}
245MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read1_obj, 1, 2, stream_read1);
246
247mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, size_t len, byte flags) {
248 mp_get_stream_raise(self_in, MP_STREAM_OP_WRITE);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200249
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200250 int error;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300251 mp_uint_t out_sz = mp_stream_rw(self_in, (void*)buf, len, &error, flags);
252 if (error != 0) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300253 if (mp_is_nonblocking_error(error)) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300254 // http://docs.python.org/3/library/io.html#io.RawIOBase.write
255 // "None is returned if the raw stream is set not to block and
256 // no single byte could be readily written to it."
Paul Sokolovskya5921042014-05-07 01:39:38 +0300257 return mp_const_none;
258 }
Damien George3a0a7712016-10-07 13:31:59 +1100259 mp_raise_OSError(error);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200260 } else {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200261 return MP_OBJ_NEW_SMALL_INT(out_sz);
262 }
263}
264
Damien George999cedb2015-11-27 17:01:44 +0000265// XXX hack
266void mp_stream_write_adaptor(void *self, const char *buf, size_t len) {
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300267 mp_stream_write(MP_OBJ_FROM_PTR(self), buf, len, MP_STREAM_RW_WRITE);
Paul Sokolovskyd4c8e622016-03-24 19:09:00 +0200268}
269
Paul Sokolovskyad9b9c72016-07-14 01:44:50 +0300270STATIC mp_obj_t stream_write_method(size_t n_args, const mp_obj_t *args) {
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300271 mp_buffer_info_t bufinfo;
Paul Sokolovskyad9b9c72016-07-14 01:44:50 +0300272 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
273 size_t max_len = (size_t)-1;
274 size_t off = 0;
275 if (n_args == 3) {
276 max_len = mp_obj_get_int_truncated(args[2]);
277 } else if (n_args == 4) {
278 off = mp_obj_get_int_truncated(args[2]);
279 max_len = mp_obj_get_int_truncated(args[3]);
280 if (off > bufinfo.len) {
281 off = bufinfo.len;
282 }
283 }
284 bufinfo.len -= off;
285 return mp_stream_write(args[0], (byte*)bufinfo.buf + off, MIN(bufinfo.len, max_len), MP_STREAM_RW_WRITE);
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300286}
Paul Sokolovskyad9b9c72016-07-14 01:44:50 +0300287MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_write_obj, 2, 4, stream_write_method);
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300288
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300289STATIC mp_obj_t stream_write1_method(mp_obj_t self_in, mp_obj_t arg) {
290 mp_buffer_info_t bufinfo;
291 mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
292 return mp_stream_write(self_in, bufinfo.buf, bufinfo.len, MP_STREAM_RW_WRITE | MP_STREAM_RW_ONCE);
293}
294MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write1_obj, stream_write1_method);
295
Damien George4b72b3a2016-01-03 14:21:40 +0000296STATIC mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300297 mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300298 mp_buffer_info_t bufinfo;
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300299 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
300
301 // CPython extension: if 2nd arg is provided, that's max len to read,
302 // instead of full buffer. Similar to
303 // https://docs.python.org/3/library/socket.html#socket.socket.recv_into
304 mp_uint_t len = bufinfo.len;
305 if (n_args > 2) {
Damien Georgec50772d2015-05-12 23:05:53 +0100306 len = mp_obj_get_int(args[2]);
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300307 if (len > bufinfo.len) {
308 len = bufinfo.len;
309 }
310 }
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300311
312 int error;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300313 mp_uint_t out_sz = mp_stream_read_exactly(args[0], bufinfo.buf, len, &error);
314 if (error != 0) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300315 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300316 return mp_const_none;
317 }
Damien George3a0a7712016-10-07 13:31:59 +1100318 mp_raise_OSError(error);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300319 } else {
320 return MP_OBJ_NEW_SMALL_INT(out_sz);
321 }
322}
Damien George358e5d82016-04-10 12:41:28 +0100323MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300324
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200325STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
Damien Georgee84325b2015-12-08 23:34:59 +0000326 const mp_stream_p_t *stream_p = mp_get_stream_raise(self_in, MP_STREAM_OP_READ);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200327
Damien Georged00d8ac2014-10-24 11:26:12 +0000328 mp_uint_t total_size = 0;
Damien George0b9ee862015-01-21 19:14:25 +0000329 vstr_t vstr;
330 vstr_init(&vstr, DEFAULT_BUFFER_SIZE);
331 char *p = vstr.buf;
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200332 mp_uint_t current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200333 while (true) {
Damien Georged00d8ac2014-10-24 11:26:12 +0000334 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000335 mp_uint_t out_sz = stream_p->read(self_in, p, current_read, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100336 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300337 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky6e731432014-05-07 01:48:12 +0300338 // With non-blocking streams, we read as much as we can.
339 // If we read nothing, return None, just like read().
340 // Otherwise, return data read so far.
341 if (total_size == 0) {
342 return mp_const_none;
343 }
344 break;
345 }
Damien George3a0a7712016-10-07 13:31:59 +1100346 mp_raise_OSError(error);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200347 }
348 if (out_sz == 0) {
349 break;
350 }
351 total_size += out_sz;
352 if (out_sz < current_read) {
353 current_read -= out_sz;
354 p += out_sz;
355 } else {
Damien George05005f62015-01-21 22:48:37 +0000356 p = vstr_extend(&vstr, DEFAULT_BUFFER_SIZE);
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200357 current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200358 }
359 }
Damien George5fa93b62014-01-22 14:35:10 +0000360
Damien George0b9ee862015-01-21 19:14:25 +0000361 vstr.len = total_size;
Damien Georgee84325b2015-12-08 23:34:59 +0000362 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200363}
364
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200365// Unbuffered, inefficient implementation of readline() for raw I/O files.
Damien George4b72b3a2016-01-03 14:21:40 +0000366STATIC mp_obj_t stream_unbuffered_readline(size_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000367 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200368
Damien George40f3c022014-07-03 13:25:24 +0100369 mp_int_t max_size = -1;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200370 if (n_args > 1) {
371 max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
372 }
373
Damien George0d3cb672015-01-28 23:43:01 +0000374 vstr_t vstr;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200375 if (max_size != -1) {
Damien George0d3cb672015-01-28 23:43:01 +0000376 vstr_init(&vstr, max_size);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200377 } else {
Damien George0d3cb672015-01-28 23:43:01 +0000378 vstr_init(&vstr, 16);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200379 }
380
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200381 while (max_size == -1 || max_size-- != 0) {
Damien George0d3cb672015-01-28 23:43:01 +0000382 char *p = vstr_add_len(&vstr, 1);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200383 if (p == NULL) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100384 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200385 }
386
Damien Georged00d8ac2014-10-24 11:26:12 +0000387 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000388 mp_uint_t out_sz = stream_p->read(args[0], p, 1, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100389 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300390 if (mp_is_nonblocking_error(error)) {
Damien George0d3cb672015-01-28 23:43:01 +0000391 if (vstr.len == 1) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300392 // We just incremented it, but otherwise we read nothing
Paul Sokolovsky8a499052016-10-27 22:12:59 +0300393 // and immediately got EAGAIN. This case is not well
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300394 // specified in
395 // https://docs.python.org/3/library/io.html#io.IOBase.readline
396 // unlike similar case for read(). But we follow the latter's
397 // behavior - return None.
Damien George0d3cb672015-01-28 23:43:01 +0000398 vstr_clear(&vstr);
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300399 return mp_const_none;
400 } else {
401 goto done;
402 }
403 }
Damien George3a0a7712016-10-07 13:31:59 +1100404 mp_raise_OSError(error);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200405 }
Paul Sokolovsky09143712014-01-22 10:34:45 +0200406 if (out_sz == 0) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300407done:
Paul Sokolovsky09143712014-01-22 10:34:45 +0200408 // Back out previously added byte
Paul Sokolovsky09143712014-01-22 10:34:45 +0200409 // Consider, what's better - read a char and get OutOfMemory (so read
410 // char is lost), or allocate first as we do.
Damien George0d3cb672015-01-28 23:43:01 +0000411 vstr_cut_tail_bytes(&vstr, 1);
Paul Sokolovsky09143712014-01-22 10:34:45 +0200412 break;
413 }
414 if (*p == '\n') {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200415 break;
416 }
417 }
Damien George0d3cb672015-01-28 23:43:01 +0000418
Damien Georgee84325b2015-12-08 23:34:59 +0000419 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200420}
Damien George358e5d82016-04-10 12:41:28 +0100421MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200422
Damien Georged5f5b2f2014-05-03 22:01:32 +0100423// TODO take an optional extra argument (what does it do exactly?)
424STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) {
425 mp_obj_t lines = mp_obj_new_list(0, NULL);
426 for (;;) {
427 mp_obj_t line = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300428 if (!mp_obj_is_true(line)) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100429 break;
430 }
431 mp_obj_list_append(lines, line);
432 }
433 return lines;
434}
435MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines);
436
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200437mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
438 mp_obj_t l_in = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300439 if (mp_obj_is_true(l_in)) {
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200440 return l_in;
441 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100442 return MP_OBJ_STOP_ITERATION;
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200443}
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200444
Damien George4b72b3a2016-01-03 14:21:40 +0000445STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000446 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200447
448 struct mp_stream_seek_t seek_s;
449 // TODO: Could be uint64
450 seek_s.offset = mp_obj_get_int(args[1]);
451 seek_s.whence = 0;
452 if (n_args == 3) {
453 seek_s.whence = mp_obj_get_int(args[2]);
454 }
455
456 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000457 mp_uint_t res = stream_p->ioctl(args[0], MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &error);
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200458 if (res == MP_STREAM_ERROR) {
Damien George3a0a7712016-10-07 13:31:59 +1100459 mp_raise_OSError(error);
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200460 }
461
462 // TODO: Could be uint64
463 return mp_obj_new_int_from_uint(seek_s.offset);
464}
465MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj, 2, 3, stream_seek);
466
blmorrisbdd78c32015-08-04 19:45:30 -0400467STATIC mp_obj_t stream_tell(mp_obj_t self) {
468 mp_obj_t offset = MP_OBJ_NEW_SMALL_INT(0);
469 mp_obj_t whence = MP_OBJ_NEW_SMALL_INT(SEEK_CUR);
470 const mp_obj_t args[3] = {self, offset, whence};
471 return stream_seek(3, args);
472}
473MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
474
Paul Sokolovskya60b0262016-07-27 00:39:10 +0300475STATIC mp_obj_t stream_flush(mp_obj_t self) {
476 const mp_stream_p_t *stream_p = mp_get_stream_raise(self, MP_STREAM_OP_IOCTL);
477 int error;
478 mp_uint_t res = stream_p->ioctl(self, MP_STREAM_FLUSH, 0, &error);
479 if (res == MP_STREAM_ERROR) {
Damien George3a0a7712016-10-07 13:31:59 +1100480 mp_raise_OSError(error);
Paul Sokolovskya60b0262016-07-27 00:39:10 +0300481 }
482 return mp_const_none;
483}
484MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_flush_obj, stream_flush);
485
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300486STATIC mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
487 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
488
489 mp_buffer_info_t bufinfo;
Paul Sokolovsky558fd5d2016-04-10 13:36:44 +0300490 uintptr_t val = 0;
491 if (n_args > 2) {
Damien George657aef62016-04-10 12:37:59 +0100492 if (mp_get_buffer(args[2], &bufinfo, MP_BUFFER_WRITE)) {
Paul Sokolovsky558fd5d2016-04-10 13:36:44 +0300493 val = (uintptr_t)bufinfo.buf;
Damien George657aef62016-04-10 12:37:59 +0100494 } else {
495 val = mp_obj_get_int_truncated(args[2]);
Paul Sokolovsky558fd5d2016-04-10 13:36:44 +0300496 }
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300497 }
498
499 int error;
Paul Sokolovsky6c3db262016-04-10 13:31:52 +0300500 mp_uint_t res = stream_p->ioctl(args[0], mp_obj_get_int(args[1]), val, &error);
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300501 if (res == MP_STREAM_ERROR) {
Damien George3a0a7712016-10-07 13:31:59 +1100502 mp_raise_OSError(error);
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300503 }
504
505 return mp_obj_new_int(res);
506}
507MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj, 2, 3, stream_ioctl);
Paul Sokolovsky4f1b0292016-07-30 00:25:06 +0300508
Paul Sokolovsky61e77a42016-07-30 20:05:56 +0300509#if MICROPY_STREAMS_POSIX_API
Paul Sokolovsky4f1b0292016-07-30 00:25:06 +0300510/*
511 * POSIX-like functions
512 *
513 * These functions have POSIX-compatible signature (except for "void *stream"
514 * first argument instead of "int fd"). They are useful to port existing
515 * POSIX-compatible software to work with MicroPython streams.
516 */
517
Paul Sokolovsky4f1b0292016-07-30 00:25:06 +0300518// errno-like variable. If any of the functions below returned with error
519// status, this variable will contain error no.
520int mp_stream_errno;
521
522ssize_t mp_stream_posix_write(mp_obj_t stream, const void *buf, size_t len) {
523 mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
524 const mp_stream_p_t *stream_p = o->type->protocol;
525 mp_uint_t out_sz = stream_p->write(stream, buf, len, &mp_stream_errno);
526 if (out_sz == MP_STREAM_ERROR) {
527 return -1;
528 } else {
529 return out_sz;
530 }
531}
532
533ssize_t mp_stream_posix_read(mp_obj_t stream, void *buf, size_t len) {
534 mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
535 const mp_stream_p_t *stream_p = o->type->protocol;
536 mp_uint_t out_sz = stream_p->read(stream, buf, len, &mp_stream_errno);
537 if (out_sz == MP_STREAM_ERROR) {
538 return -1;
539 } else {
540 return out_sz;
541 }
542}
543
544off_t mp_stream_posix_lseek(mp_obj_t stream, off_t offset, int whence) {
545 const mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
546 const mp_stream_p_t *stream_p = o->type->protocol;
547 struct mp_stream_seek_t seek_s;
548 seek_s.offset = offset;
549 seek_s.whence = whence;
550 mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &mp_stream_errno);
551 if (res == MP_STREAM_ERROR) {
552 return -1;
553 }
554 return seek_s.offset;
555}
556
557int mp_stream_posix_fsync(mp_obj_t stream) {
558 mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
559 const mp_stream_p_t *stream_p = o->type->protocol;
560 mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_FLUSH, 0, &mp_stream_errno);
561 if (res == MP_STREAM_ERROR) {
562 return -1;
563 }
564 return res;
565}
Paul Sokolovsky61e77a42016-07-30 20:05:56 +0300566
567#endif