blob: 453dee769fe8a14761783aa0dbd777b82a0d6cea [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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/objstr.h"
32#include "py/stream.h"
Paul Sokolovsky497660f2016-05-20 21:16:58 +030033#include "py/runtime.h"
Damien George51dfcb42015-01-01 20:27:54 +000034
Paul Sokolovsky0ef015b2014-05-07 02:23:46 +030035#if MICROPY_STREAMS_NON_BLOCK
36#include <errno.h>
stijnec6fa872014-06-28 21:04:20 +020037#if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
38#define EWOULDBLOCK 140
39#endif
Paul Sokolovsky0ef015b2014-05-07 02:23:46 +030040#endif
Paul Sokolovskye98cf402014-01-08 02:43:48 +020041
42// This file defines generic Python stream read/write methods which
43// dispatch to the underlying stream interface of an object.
44
Paul Sokolovskyb9be45e2014-05-07 01:51:07 +030045// TODO: should be in mpconfig.h
46#define DEFAULT_BUFFER_SIZE 256
47
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020048STATIC mp_obj_t stream_readall(mp_obj_t self_in);
Paul Sokolovskya671f892014-01-16 12:53:46 +020049
Damien Georgeadf0f2a2014-07-27 22:38:58 +010050#define STREAM_CONTENT_TYPE(stream) (((stream)->is_text) ? &mp_type_str : &mp_type_bytes)
Paul Sokolovskya47b64a2014-05-15 07:28:19 +030051
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +030052// Returns error condition in *errcode, if non-zero, return value is number of bytes written
Ville Skyttäca16c382017-05-29 10:08:14 +030053// before error condition occurred. If *errcode == 0, returns total bytes written (which will
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +030054// be equal to input size).
55mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf_, mp_uint_t size, int *errcode, byte flags) {
56 byte *buf = buf_;
57 mp_obj_base_t* s = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
58 typedef mp_uint_t (*io_func_t)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
59 io_func_t io_func;
Paul Sokolovsky07209f82016-06-18 18:19:24 +030060 const mp_stream_p_t *stream_p = s->type->protocol;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +030061 if (flags & MP_STREAM_RW_WRITE) {
Paul Sokolovsky07209f82016-06-18 18:19:24 +030062 io_func = (io_func_t)stream_p->write;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +030063 } else {
Paul Sokolovsky07209f82016-06-18 18:19:24 +030064 io_func = stream_p->read;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +030065 }
66
67 *errcode = 0;
68 mp_uint_t done = 0;
69 while (size > 0) {
70 mp_uint_t out_sz = io_func(stream, buf, size, errcode);
71 // For read, out_sz == 0 means EOF. For write, it's unspecified
72 // what it means, but we don't make any progress, so returning
73 // is still the best option.
74 if (out_sz == 0) {
75 return done;
76 }
77 if (out_sz == MP_STREAM_ERROR) {
78 // If we read something before getting EAGAIN, don't leak it
79 if (mp_is_nonblocking_error(*errcode) && done != 0) {
80 *errcode = 0;
81 }
82 return done;
83 }
84 if (flags & MP_STREAM_RW_ONCE) {
85 return out_sz;
86 }
87
88 buf += out_sz;
89 size -= out_sz;
90 done += out_sz;
91 }
92 return done;
93}
94
Damien Georgee84325b2015-12-08 23:34:59 +000095const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) {
Krzysztof Blazewicz65620762016-08-20 13:45:25 +020096 mp_obj_type_t *type = mp_obj_get_type(self_in);
97 const mp_stream_p_t *stream_p = type->protocol;
Damien Georgee84325b2015-12-08 23:34:59 +000098 if (stream_p == NULL
99 || ((flags & MP_STREAM_OP_READ) && stream_p->read == NULL)
100 || ((flags & MP_STREAM_OP_WRITE) && stream_p->write == NULL)
101 || ((flags & MP_STREAM_OP_IOCTL) && stream_p->ioctl == NULL)) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200102 // CPython: io.UnsupportedOperation, OSError subclass
Damien George7d0d7212016-10-17 12:17:37 +1100103 mp_raise_msg(&mp_type_OSError, "stream operation not supported");
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200104 }
Damien Georgee84325b2015-12-08 23:34:59 +0000105 return stream_p;
106}
107
Paul Sokolovsky497660f2016-05-20 21:16:58 +0300108mp_obj_t mp_stream_close(mp_obj_t stream) {
109 // TODO: Still consider using ioctl for close
110 mp_obj_t dest[2];
111 mp_load_method(stream, MP_QSTR_close, dest);
112 return mp_call_method_n_kw(0, 0, dest);
113}
114
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300115STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte flags) {
Damien Georgee84325b2015-12-08 23:34:59 +0000116 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200117
Damien George1694bc72014-07-16 11:45:10 +0100118 // What to do if sz < -1? Python docs don't specify this case.
119 // CPython does a readall, but here we silently let negatives through,
120 // and they will cause a MemoryError.
Damien George40f3c022014-07-03 13:25:24 +0100121 mp_int_t sz;
Paul Sokolovskya671f892014-01-16 12:53:46 +0200122 if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) {
123 return stream_readall(args[0]);
124 }
Paul Sokolovskyf5f6c3b2014-06-15 23:23:36 +0300125
126 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien Georgee84325b2015-12-08 23:34:59 +0000127 if (stream_p->is_text) {
Damien George1694bc72014-07-16 11:45:10 +0100128 // We need to read sz number of unicode characters. Because we don't have any
129 // buffering, and because the stream API can only read bytes, we must read here
130 // in units of bytes and must never over read. If we want sz chars, then reading
131 // sz bytes will never over-read, so we follow this approach, in a loop to keep
132 // reading until we have exactly enough chars. This will be 1 read for text
133 // with ASCII-only chars, and about 2 reads for text with a couple of non-ASCII
134 // chars. For text with lots of non-ASCII chars, it'll be pretty inefficient
135 // in time and memory.
136
137 vstr_t vstr;
138 vstr_init(&vstr, sz);
139 mp_uint_t more_bytes = sz;
140 mp_uint_t last_buf_offset = 0;
141 while (more_bytes > 0) {
142 char *p = vstr_add_len(&vstr, more_bytes);
Damien George1694bc72014-07-16 11:45:10 +0100143 int error;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300144 mp_uint_t out_sz = mp_stream_read_exactly(args[0], p, more_bytes, &error);
145 if (error != 0) {
Damien George1694bc72014-07-16 11:45:10 +0100146 vstr_cut_tail_bytes(&vstr, more_bytes);
Paul Sokolovsky77994102015-10-18 15:37:19 +0300147 if (mp_is_nonblocking_error(error)) {
Damien George1694bc72014-07-16 11:45:10 +0100148 // With non-blocking streams, we read as much as we can.
149 // If we read nothing, return None, just like read().
150 // Otherwise, return data read so far.
151 // TODO what if we have read only half a non-ASCII char?
152 if (vstr.len == 0) {
153 vstr_clear(&vstr);
154 return mp_const_none;
155 }
156 break;
157 }
Damien George3a0a7712016-10-07 13:31:59 +1100158 mp_raise_OSError(error);
Damien George1694bc72014-07-16 11:45:10 +0100159 }
160
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100161 if (out_sz < more_bytes) {
Damien George1694bc72014-07-16 11:45:10 +0100162 // Finish reading.
163 // TODO what if we have read only half a non-ASCII char?
Dave Hylands1d8816c2014-07-21 16:51:07 -0700164 vstr_cut_tail_bytes(&vstr, more_bytes - out_sz);
165 if (out_sz == 0) {
Krzysztof Blazewicz65620762016-08-20 13:45:25 +0200166 break;
Dave Hylands1d8816c2014-07-21 16:51:07 -0700167 }
Damien George1694bc72014-07-16 11:45:10 +0100168 }
169
170 // count chars from bytes just read
171 for (mp_uint_t off = last_buf_offset;;) {
172 byte b = vstr.buf[off];
173 int n;
174 if (!UTF8_IS_NONASCII(b)) {
175 // 1-byte ASCII char
176 n = 1;
177 } else if ((b & 0xe0) == 0xc0) {
178 // 2-byte char
179 n = 2;
180 } else if ((b & 0xf0) == 0xe0) {
181 // 3-byte char
182 n = 3;
183 } else if ((b & 0xf8) == 0xf0) {
184 // 4-byte char
185 n = 4;
186 } else {
187 // TODO
188 n = 5;
189 }
190 if (off + n <= vstr.len) {
191 // got a whole char in n bytes
192 off += n;
193 sz -= 1;
194 last_buf_offset = off;
195 if (off >= vstr.len) {
196 more_bytes = sz;
197 break;
198 }
199 } else {
200 // didn't get a whole char, so work out how many extra bytes are needed for
201 // this partial char, plus bytes for additional chars that we want
202 more_bytes = (off + n - vstr.len) + (sz - 1);
203 break;
204 }
205 }
206 }
207
Damien George0b9ee862015-01-21 19:14:25 +0000208 return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
Paul Sokolovskyf5f6c3b2014-06-15 23:23:36 +0300209 }
210 #endif
211
Damien George05005f62015-01-21 22:48:37 +0000212 vstr_t vstr;
213 vstr_init_len(&vstr, sz);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200214 int error;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300215 mp_uint_t out_sz = mp_stream_rw(args[0], vstr.buf, sz, &error, flags);
216 if (error != 0) {
Damien George05005f62015-01-21 22:48:37 +0000217 vstr_clear(&vstr);
Paul Sokolovsky77994102015-10-18 15:37:19 +0300218 if (mp_is_nonblocking_error(error)) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300219 // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
220 // "If the object is in non-blocking mode and no bytes are available,
221 // None is returned."
222 // This is actually very weird, as naive truth check will treat
223 // this as EOF.
224 return mp_const_none;
225 }
Damien George3a0a7712016-10-07 13:31:59 +1100226 mp_raise_OSError(error);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200227 } else {
Damien George05005f62015-01-21 22:48:37 +0000228 vstr.len = out_sz;
Damien Georgee84325b2015-12-08 23:34:59 +0000229 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200230 }
231}
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300232
233STATIC mp_obj_t stream_read(size_t n_args, const mp_obj_t *args) {
234 return stream_read_generic(n_args, args, MP_STREAM_RW_READ);
235}
Damien George358e5d82016-04-10 12:41:28 +0100236MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200237
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300238STATIC mp_obj_t stream_read1(size_t n_args, const mp_obj_t *args) {
239 return stream_read_generic(n_args, args, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);
240}
241MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read1_obj, 1, 2, stream_read1);
242
243mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, size_t len, byte flags) {
244 mp_get_stream_raise(self_in, MP_STREAM_OP_WRITE);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200245
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200246 int error;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300247 mp_uint_t out_sz = mp_stream_rw(self_in, (void*)buf, len, &error, flags);
248 if (error != 0) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300249 if (mp_is_nonblocking_error(error)) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300250 // http://docs.python.org/3/library/io.html#io.RawIOBase.write
251 // "None is returned if the raw stream is set not to block and
252 // no single byte could be readily written to it."
Paul Sokolovskya5921042014-05-07 01:39:38 +0300253 return mp_const_none;
254 }
Damien George3a0a7712016-10-07 13:31:59 +1100255 mp_raise_OSError(error);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200256 } else {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200257 return MP_OBJ_NEW_SMALL_INT(out_sz);
258 }
259}
260
Damien George999cedb2015-11-27 17:01:44 +0000261// XXX hack
262void mp_stream_write_adaptor(void *self, const char *buf, size_t len) {
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300263 mp_stream_write(MP_OBJ_FROM_PTR(self), buf, len, MP_STREAM_RW_WRITE);
Paul Sokolovskyd4c8e622016-03-24 19:09:00 +0200264}
265
Paul Sokolovskyad9b9c72016-07-14 01:44:50 +0300266STATIC mp_obj_t stream_write_method(size_t n_args, const mp_obj_t *args) {
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300267 mp_buffer_info_t bufinfo;
Paul Sokolovskyad9b9c72016-07-14 01:44:50 +0300268 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
269 size_t max_len = (size_t)-1;
270 size_t off = 0;
271 if (n_args == 3) {
272 max_len = mp_obj_get_int_truncated(args[2]);
273 } else if (n_args == 4) {
274 off = mp_obj_get_int_truncated(args[2]);
275 max_len = mp_obj_get_int_truncated(args[3]);
276 if (off > bufinfo.len) {
277 off = bufinfo.len;
278 }
279 }
280 bufinfo.len -= off;
281 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 +0300282}
Paul Sokolovskyad9b9c72016-07-14 01:44:50 +0300283MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_write_obj, 2, 4, stream_write_method);
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300284
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300285STATIC mp_obj_t stream_write1_method(mp_obj_t self_in, mp_obj_t arg) {
286 mp_buffer_info_t bufinfo;
287 mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
288 return mp_stream_write(self_in, bufinfo.buf, bufinfo.len, MP_STREAM_RW_WRITE | MP_STREAM_RW_ONCE);
289}
290MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write1_obj, stream_write1_method);
291
Damien George4b72b3a2016-01-03 14:21:40 +0000292STATIC mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300293 mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300294 mp_buffer_info_t bufinfo;
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300295 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
296
297 // CPython extension: if 2nd arg is provided, that's max len to read,
298 // instead of full buffer. Similar to
299 // https://docs.python.org/3/library/socket.html#socket.socket.recv_into
300 mp_uint_t len = bufinfo.len;
301 if (n_args > 2) {
Damien Georgec50772d2015-05-12 23:05:53 +0100302 len = mp_obj_get_int(args[2]);
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300303 if (len > bufinfo.len) {
304 len = bufinfo.len;
305 }
306 }
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300307
308 int error;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300309 mp_uint_t out_sz = mp_stream_read_exactly(args[0], bufinfo.buf, len, &error);
310 if (error != 0) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300311 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300312 return mp_const_none;
313 }
Damien George3a0a7712016-10-07 13:31:59 +1100314 mp_raise_OSError(error);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300315 } else {
316 return MP_OBJ_NEW_SMALL_INT(out_sz);
317 }
318}
Damien George358e5d82016-04-10 12:41:28 +0100319MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300320
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200321STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
Damien Georgee84325b2015-12-08 23:34:59 +0000322 const mp_stream_p_t *stream_p = mp_get_stream_raise(self_in, MP_STREAM_OP_READ);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200323
Damien Georged00d8ac2014-10-24 11:26:12 +0000324 mp_uint_t total_size = 0;
Damien George0b9ee862015-01-21 19:14:25 +0000325 vstr_t vstr;
326 vstr_init(&vstr, DEFAULT_BUFFER_SIZE);
327 char *p = vstr.buf;
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200328 mp_uint_t current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200329 while (true) {
Damien Georged00d8ac2014-10-24 11:26:12 +0000330 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000331 mp_uint_t out_sz = stream_p->read(self_in, p, current_read, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100332 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300333 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky6e731432014-05-07 01:48:12 +0300334 // With non-blocking streams, we read as much as we can.
335 // If we read nothing, return None, just like read().
336 // Otherwise, return data read so far.
337 if (total_size == 0) {
338 return mp_const_none;
339 }
340 break;
341 }
Damien George3a0a7712016-10-07 13:31:59 +1100342 mp_raise_OSError(error);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200343 }
344 if (out_sz == 0) {
345 break;
346 }
347 total_size += out_sz;
348 if (out_sz < current_read) {
349 current_read -= out_sz;
350 p += out_sz;
351 } else {
Damien George05005f62015-01-21 22:48:37 +0000352 p = vstr_extend(&vstr, DEFAULT_BUFFER_SIZE);
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200353 current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200354 }
355 }
Damien George5fa93b62014-01-22 14:35:10 +0000356
Damien George0b9ee862015-01-21 19:14:25 +0000357 vstr.len = total_size;
Damien Georgee84325b2015-12-08 23:34:59 +0000358 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200359}
360
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200361// Unbuffered, inefficient implementation of readline() for raw I/O files.
Damien George4b72b3a2016-01-03 14:21:40 +0000362STATIC mp_obj_t stream_unbuffered_readline(size_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000363 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200364
Damien George40f3c022014-07-03 13:25:24 +0100365 mp_int_t max_size = -1;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200366 if (n_args > 1) {
367 max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
368 }
369
Damien George0d3cb672015-01-28 23:43:01 +0000370 vstr_t vstr;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200371 if (max_size != -1) {
Damien George0d3cb672015-01-28 23:43:01 +0000372 vstr_init(&vstr, max_size);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200373 } else {
Damien George0d3cb672015-01-28 23:43:01 +0000374 vstr_init(&vstr, 16);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200375 }
376
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200377 while (max_size == -1 || max_size-- != 0) {
Damien George0d3cb672015-01-28 23:43:01 +0000378 char *p = vstr_add_len(&vstr, 1);
Damien Georged00d8ac2014-10-24 11:26:12 +0000379 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000380 mp_uint_t out_sz = stream_p->read(args[0], p, 1, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100381 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300382 if (mp_is_nonblocking_error(error)) {
Damien George0d3cb672015-01-28 23:43:01 +0000383 if (vstr.len == 1) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300384 // We just incremented it, but otherwise we read nothing
Paul Sokolovsky8a499052016-10-27 22:12:59 +0300385 // and immediately got EAGAIN. This case is not well
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300386 // specified in
387 // https://docs.python.org/3/library/io.html#io.IOBase.readline
388 // unlike similar case for read(). But we follow the latter's
389 // behavior - return None.
Damien George0d3cb672015-01-28 23:43:01 +0000390 vstr_clear(&vstr);
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300391 return mp_const_none;
392 } else {
393 goto done;
394 }
395 }
Damien George3a0a7712016-10-07 13:31:59 +1100396 mp_raise_OSError(error);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200397 }
Paul Sokolovsky09143712014-01-22 10:34:45 +0200398 if (out_sz == 0) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300399done:
Paul Sokolovsky09143712014-01-22 10:34:45 +0200400 // Back out previously added byte
Paul Sokolovsky09143712014-01-22 10:34:45 +0200401 // Consider, what's better - read a char and get OutOfMemory (so read
402 // char is lost), or allocate first as we do.
Damien George0d3cb672015-01-28 23:43:01 +0000403 vstr_cut_tail_bytes(&vstr, 1);
Paul Sokolovsky09143712014-01-22 10:34:45 +0200404 break;
405 }
406 if (*p == '\n') {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200407 break;
408 }
409 }
Damien George0d3cb672015-01-28 23:43:01 +0000410
Damien Georgee84325b2015-12-08 23:34:59 +0000411 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200412}
Damien George358e5d82016-04-10 12:41:28 +0100413MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200414
Damien Georged5f5b2f2014-05-03 22:01:32 +0100415// TODO take an optional extra argument (what does it do exactly?)
416STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) {
417 mp_obj_t lines = mp_obj_new_list(0, NULL);
418 for (;;) {
419 mp_obj_t line = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300420 if (!mp_obj_is_true(line)) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100421 break;
422 }
423 mp_obj_list_append(lines, line);
424 }
425 return lines;
426}
427MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines);
428
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200429mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
430 mp_obj_t l_in = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300431 if (mp_obj_is_true(l_in)) {
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200432 return l_in;
433 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100434 return MP_OBJ_STOP_ITERATION;
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200435}
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200436
Damien George4b72b3a2016-01-03 14:21:40 +0000437STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000438 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200439
440 struct mp_stream_seek_t seek_s;
441 // TODO: Could be uint64
442 seek_s.offset = mp_obj_get_int(args[1]);
Paul Sokolovskye3383e92017-08-20 21:57:36 +0300443 seek_s.whence = SEEK_SET;
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200444 if (n_args == 3) {
445 seek_s.whence = mp_obj_get_int(args[2]);
446 }
447
Paul Sokolovskye3383e92017-08-20 21:57:36 +0300448 // In POSIX, it's error to seek before end of stream, we enforce it here.
449 if (seek_s.whence == SEEK_SET && seek_s.offset < 0) {
450 mp_raise_OSError(MP_EINVAL);
451 }
452
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200453 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000454 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 +0200455 if (res == MP_STREAM_ERROR) {
Damien George3a0a7712016-10-07 13:31:59 +1100456 mp_raise_OSError(error);
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200457 }
458
459 // TODO: Could be uint64
460 return mp_obj_new_int_from_uint(seek_s.offset);
461}
462MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj, 2, 3, stream_seek);
463
blmorrisbdd78c32015-08-04 19:45:30 -0400464STATIC mp_obj_t stream_tell(mp_obj_t self) {
465 mp_obj_t offset = MP_OBJ_NEW_SMALL_INT(0);
466 mp_obj_t whence = MP_OBJ_NEW_SMALL_INT(SEEK_CUR);
467 const mp_obj_t args[3] = {self, offset, whence};
468 return stream_seek(3, args);
469}
470MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
471
Paul Sokolovskya60b0262016-07-27 00:39:10 +0300472STATIC mp_obj_t stream_flush(mp_obj_t self) {
473 const mp_stream_p_t *stream_p = mp_get_stream_raise(self, MP_STREAM_OP_IOCTL);
474 int error;
475 mp_uint_t res = stream_p->ioctl(self, MP_STREAM_FLUSH, 0, &error);
476 if (res == MP_STREAM_ERROR) {
Damien George3a0a7712016-10-07 13:31:59 +1100477 mp_raise_OSError(error);
Paul Sokolovskya60b0262016-07-27 00:39:10 +0300478 }
479 return mp_const_none;
480}
481MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_flush_obj, stream_flush);
482
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300483STATIC mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
484 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
485
486 mp_buffer_info_t bufinfo;
Paul Sokolovsky558fd5d2016-04-10 13:36:44 +0300487 uintptr_t val = 0;
488 if (n_args > 2) {
Damien George657aef62016-04-10 12:37:59 +0100489 if (mp_get_buffer(args[2], &bufinfo, MP_BUFFER_WRITE)) {
Paul Sokolovsky558fd5d2016-04-10 13:36:44 +0300490 val = (uintptr_t)bufinfo.buf;
Damien George657aef62016-04-10 12:37:59 +0100491 } else {
492 val = mp_obj_get_int_truncated(args[2]);
Paul Sokolovsky558fd5d2016-04-10 13:36:44 +0300493 }
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300494 }
495
496 int error;
Paul Sokolovsky6c3db262016-04-10 13:31:52 +0300497 mp_uint_t res = stream_p->ioctl(args[0], mp_obj_get_int(args[1]), val, &error);
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300498 if (res == MP_STREAM_ERROR) {
Damien George3a0a7712016-10-07 13:31:59 +1100499 mp_raise_OSError(error);
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300500 }
501
502 return mp_obj_new_int(res);
503}
504MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj, 2, 3, stream_ioctl);
Paul Sokolovsky4f1b0292016-07-30 00:25:06 +0300505
Paul Sokolovsky61e77a42016-07-30 20:05:56 +0300506#if MICROPY_STREAMS_POSIX_API
Paul Sokolovsky4f1b0292016-07-30 00:25:06 +0300507/*
508 * POSIX-like functions
509 *
510 * These functions have POSIX-compatible signature (except for "void *stream"
511 * first argument instead of "int fd"). They are useful to port existing
512 * POSIX-compatible software to work with MicroPython streams.
513 */
514
Paul Sokolovsky4f1b0292016-07-30 00:25:06 +0300515// errno-like variable. If any of the functions below returned with error
516// status, this variable will contain error no.
517int mp_stream_errno;
518
519ssize_t mp_stream_posix_write(mp_obj_t stream, const void *buf, size_t len) {
520 mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
521 const mp_stream_p_t *stream_p = o->type->protocol;
522 mp_uint_t out_sz = stream_p->write(stream, buf, len, &mp_stream_errno);
523 if (out_sz == MP_STREAM_ERROR) {
524 return -1;
525 } else {
526 return out_sz;
527 }
528}
529
530ssize_t mp_stream_posix_read(mp_obj_t stream, void *buf, size_t len) {
531 mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
532 const mp_stream_p_t *stream_p = o->type->protocol;
533 mp_uint_t out_sz = stream_p->read(stream, buf, len, &mp_stream_errno);
534 if (out_sz == MP_STREAM_ERROR) {
535 return -1;
536 } else {
537 return out_sz;
538 }
539}
540
541off_t mp_stream_posix_lseek(mp_obj_t stream, off_t offset, int whence) {
542 const mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
543 const mp_stream_p_t *stream_p = o->type->protocol;
544 struct mp_stream_seek_t seek_s;
545 seek_s.offset = offset;
546 seek_s.whence = whence;
547 mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &mp_stream_errno);
548 if (res == MP_STREAM_ERROR) {
549 return -1;
550 }
551 return seek_s.offset;
552}
553
554int mp_stream_posix_fsync(mp_obj_t stream) {
555 mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
556 const mp_stream_p_t *stream_p = o->type->protocol;
557 mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_FLUSH, 0, &mp_stream_errno);
558 if (res == MP_STREAM_ERROR) {
559 return -1;
560 }
561 return res;
562}
Paul Sokolovsky61e77a42016-07-30 20:05:56 +0300563
564#endif