blob: 9b1d5fd2dec5549a5ce26b533097d2213a27c119 [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"
34
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
53// before error condition occured. If *errcode == 0, returns total bytes written (which will
54// 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;
60 if (flags & MP_STREAM_RW_WRITE) {
61 io_func = (io_func_t)s->type->stream_p->write;
62 } else {
63 io_func = s->type->stream_p->read;
64 }
65
66 *errcode = 0;
67 mp_uint_t done = 0;
68 while (size > 0) {
69 mp_uint_t out_sz = io_func(stream, buf, size, errcode);
70 // For read, out_sz == 0 means EOF. For write, it's unspecified
71 // what it means, but we don't make any progress, so returning
72 // is still the best option.
73 if (out_sz == 0) {
74 return done;
75 }
76 if (out_sz == MP_STREAM_ERROR) {
77 // If we read something before getting EAGAIN, don't leak it
78 if (mp_is_nonblocking_error(*errcode) && done != 0) {
79 *errcode = 0;
80 }
81 return done;
82 }
83 if (flags & MP_STREAM_RW_ONCE) {
84 return out_sz;
85 }
86
87 buf += out_sz;
88 size -= out_sz;
89 done += out_sz;
90 }
91 return done;
92}
93
Damien Georgee84325b2015-12-08 23:34:59 +000094const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) {
95 mp_obj_base_t *o = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
96 const mp_stream_p_t *stream_p = o->type->stream_p;
97 if (stream_p == NULL
98 || ((flags & MP_STREAM_OP_READ) && stream_p->read == NULL)
99 || ((flags & MP_STREAM_OP_WRITE) && stream_p->write == NULL)
100 || ((flags & MP_STREAM_OP_IOCTL) && stream_p->ioctl == NULL)) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200101 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgee84325b2015-12-08 23:34:59 +0000102 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "stream operation not supported"));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200103 }
Damien Georgee84325b2015-12-08 23:34:59 +0000104 return stream_p;
105}
106
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300107STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte flags) {
Damien Georgee84325b2015-12-08 23:34:59 +0000108 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200109
Damien George1694bc72014-07-16 11:45:10 +0100110 // What to do if sz < -1? Python docs don't specify this case.
111 // CPython does a readall, but here we silently let negatives through,
112 // and they will cause a MemoryError.
Damien George40f3c022014-07-03 13:25:24 +0100113 mp_int_t sz;
Paul Sokolovskya671f892014-01-16 12:53:46 +0200114 if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) {
115 return stream_readall(args[0]);
116 }
Paul Sokolovskyf5f6c3b2014-06-15 23:23:36 +0300117
118 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien Georgee84325b2015-12-08 23:34:59 +0000119 if (stream_p->is_text) {
Damien George1694bc72014-07-16 11:45:10 +0100120 // We need to read sz number of unicode characters. Because we don't have any
121 // buffering, and because the stream API can only read bytes, we must read here
122 // in units of bytes and must never over read. If we want sz chars, then reading
123 // sz bytes will never over-read, so we follow this approach, in a loop to keep
124 // reading until we have exactly enough chars. This will be 1 read for text
125 // with ASCII-only chars, and about 2 reads for text with a couple of non-ASCII
126 // chars. For text with lots of non-ASCII chars, it'll be pretty inefficient
127 // in time and memory.
128
129 vstr_t vstr;
130 vstr_init(&vstr, sz);
131 mp_uint_t more_bytes = sz;
132 mp_uint_t last_buf_offset = 0;
133 while (more_bytes > 0) {
134 char *p = vstr_add_len(&vstr, more_bytes);
135 if (p == NULL) {
136 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
137 }
138 int error;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300139 mp_uint_t out_sz = mp_stream_read_exactly(args[0], p, more_bytes, &error);
140 if (error != 0) {
Damien George1694bc72014-07-16 11:45:10 +0100141 vstr_cut_tail_bytes(&vstr, more_bytes);
Paul Sokolovsky77994102015-10-18 15:37:19 +0300142 if (mp_is_nonblocking_error(error)) {
Damien George1694bc72014-07-16 11:45:10 +0100143 // With non-blocking streams, we read as much as we can.
144 // If we read nothing, return None, just like read().
145 // Otherwise, return data read so far.
146 // TODO what if we have read only half a non-ASCII char?
147 if (vstr.len == 0) {
148 vstr_clear(&vstr);
149 return mp_const_none;
150 }
151 break;
152 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300153 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Damien George1694bc72014-07-16 11:45:10 +0100154 }
155
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100156 if (out_sz < more_bytes) {
Damien George1694bc72014-07-16 11:45:10 +0100157 // Finish reading.
158 // TODO what if we have read only half a non-ASCII char?
Dave Hylands1d8816c2014-07-21 16:51:07 -0700159 vstr_cut_tail_bytes(&vstr, more_bytes - out_sz);
160 if (out_sz == 0) {
161 break;
162 }
Damien George1694bc72014-07-16 11:45:10 +0100163 }
164
165 // count chars from bytes just read
166 for (mp_uint_t off = last_buf_offset;;) {
167 byte b = vstr.buf[off];
168 int n;
169 if (!UTF8_IS_NONASCII(b)) {
170 // 1-byte ASCII char
171 n = 1;
172 } else if ((b & 0xe0) == 0xc0) {
173 // 2-byte char
174 n = 2;
175 } else if ((b & 0xf0) == 0xe0) {
176 // 3-byte char
177 n = 3;
178 } else if ((b & 0xf8) == 0xf0) {
179 // 4-byte char
180 n = 4;
181 } else {
182 // TODO
183 n = 5;
184 }
185 if (off + n <= vstr.len) {
186 // got a whole char in n bytes
187 off += n;
188 sz -= 1;
189 last_buf_offset = off;
190 if (off >= vstr.len) {
191 more_bytes = sz;
192 break;
193 }
194 } else {
195 // didn't get a whole char, so work out how many extra bytes are needed for
196 // this partial char, plus bytes for additional chars that we want
197 more_bytes = (off + n - vstr.len) + (sz - 1);
198 break;
199 }
200 }
201 }
202
Damien George0b9ee862015-01-21 19:14:25 +0000203 return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
Paul Sokolovskyf5f6c3b2014-06-15 23:23:36 +0300204 }
205 #endif
206
Damien George05005f62015-01-21 22:48:37 +0000207 vstr_t vstr;
208 vstr_init_len(&vstr, sz);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200209 int error;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300210 mp_uint_t out_sz = mp_stream_rw(args[0], vstr.buf, sz, &error, flags);
211 if (error != 0) {
Damien George05005f62015-01-21 22:48:37 +0000212 vstr_clear(&vstr);
Paul Sokolovsky77994102015-10-18 15:37:19 +0300213 if (mp_is_nonblocking_error(error)) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300214 // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
215 // "If the object is in non-blocking mode and no bytes are available,
216 // None is returned."
217 // This is actually very weird, as naive truth check will treat
218 // this as EOF.
219 return mp_const_none;
220 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300221 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200222 } else {
Damien George05005f62015-01-21 22:48:37 +0000223 vstr.len = out_sz;
Damien Georgee84325b2015-12-08 23:34:59 +0000224 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200225 }
226}
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300227
228STATIC mp_obj_t stream_read(size_t n_args, const mp_obj_t *args) {
229 return stream_read_generic(n_args, args, MP_STREAM_RW_READ);
230}
Damien George358e5d82016-04-10 12:41:28 +0100231MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200232
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300233STATIC mp_obj_t stream_read1(size_t n_args, const mp_obj_t *args) {
234 return stream_read_generic(n_args, args, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);
235}
236MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read1_obj, 1, 2, stream_read1);
237
238mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, size_t len, byte flags) {
239 mp_get_stream_raise(self_in, MP_STREAM_OP_WRITE);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200240
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200241 int error;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300242 mp_uint_t out_sz = mp_stream_rw(self_in, (void*)buf, len, &error, flags);
243 if (error != 0) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300244 if (mp_is_nonblocking_error(error)) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300245 // http://docs.python.org/3/library/io.html#io.RawIOBase.write
246 // "None is returned if the raw stream is set not to block and
247 // no single byte could be readily written to it."
Paul Sokolovskya5921042014-05-07 01:39:38 +0300248 return mp_const_none;
249 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300250 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200251 } else {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200252 return MP_OBJ_NEW_SMALL_INT(out_sz);
253 }
254}
255
Damien George999cedb2015-11-27 17:01:44 +0000256// XXX hack
257void mp_stream_write_adaptor(void *self, const char *buf, size_t len) {
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300258 mp_stream_write(MP_OBJ_FROM_PTR(self), buf, len, MP_STREAM_RW_WRITE);
Paul Sokolovskyd4c8e622016-03-24 19:09:00 +0200259}
260
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300261STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) {
262 mp_buffer_info_t bufinfo;
263 mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300264 return mp_stream_write(self_in, bufinfo.buf, bufinfo.len, MP_STREAM_RW_WRITE);
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300265}
Damien George358e5d82016-04-10 12:41:28 +0100266MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write_method);
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300267
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300268STATIC mp_obj_t stream_write1_method(mp_obj_t self_in, mp_obj_t arg) {
269 mp_buffer_info_t bufinfo;
270 mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
271 return mp_stream_write(self_in, bufinfo.buf, bufinfo.len, MP_STREAM_RW_WRITE | MP_STREAM_RW_ONCE);
272}
273MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write1_obj, stream_write1_method);
274
Damien George4b72b3a2016-01-03 14:21:40 +0000275STATIC mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300276 mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300277 mp_buffer_info_t bufinfo;
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300278 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
279
280 // CPython extension: if 2nd arg is provided, that's max len to read,
281 // instead of full buffer. Similar to
282 // https://docs.python.org/3/library/socket.html#socket.socket.recv_into
283 mp_uint_t len = bufinfo.len;
284 if (n_args > 2) {
Damien Georgec50772d2015-05-12 23:05:53 +0100285 len = mp_obj_get_int(args[2]);
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300286 if (len > bufinfo.len) {
287 len = bufinfo.len;
288 }
289 }
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300290
291 int error;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300292 mp_uint_t out_sz = mp_stream_read_exactly(args[0], bufinfo.buf, len, &error);
293 if (error != 0) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300294 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300295 return mp_const_none;
296 }
297 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
298 } else {
299 return MP_OBJ_NEW_SMALL_INT(out_sz);
300 }
301}
Damien George358e5d82016-04-10 12:41:28 +0100302MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300303
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200304STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
Damien Georgee84325b2015-12-08 23:34:59 +0000305 const mp_stream_p_t *stream_p = mp_get_stream_raise(self_in, MP_STREAM_OP_READ);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200306
Damien Georged00d8ac2014-10-24 11:26:12 +0000307 mp_uint_t total_size = 0;
Damien George0b9ee862015-01-21 19:14:25 +0000308 vstr_t vstr;
309 vstr_init(&vstr, DEFAULT_BUFFER_SIZE);
310 char *p = vstr.buf;
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200311 mp_uint_t current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200312 while (true) {
Damien Georged00d8ac2014-10-24 11:26:12 +0000313 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000314 mp_uint_t out_sz = stream_p->read(self_in, p, current_read, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100315 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300316 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky6e731432014-05-07 01:48:12 +0300317 // With non-blocking streams, we read as much as we can.
318 // If we read nothing, return None, just like read().
319 // Otherwise, return data read so far.
320 if (total_size == 0) {
321 return mp_const_none;
322 }
323 break;
324 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300325 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200326 }
327 if (out_sz == 0) {
328 break;
329 }
330 total_size += out_sz;
331 if (out_sz < current_read) {
332 current_read -= out_sz;
333 p += out_sz;
334 } else {
Damien George05005f62015-01-21 22:48:37 +0000335 p = vstr_extend(&vstr, DEFAULT_BUFFER_SIZE);
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200336 current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200337 if (p == NULL) {
338 // TODO
Damien Georgeea13f402014-04-05 18:32:08 +0100339 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200340 }
341 }
342 }
Damien George5fa93b62014-01-22 14:35:10 +0000343
Damien George0b9ee862015-01-21 19:14:25 +0000344 vstr.len = total_size;
Damien Georgee84325b2015-12-08 23:34:59 +0000345 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200346}
Damien George358e5d82016-04-10 12:41:28 +0100347MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200348
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200349// Unbuffered, inefficient implementation of readline() for raw I/O files.
Damien George4b72b3a2016-01-03 14:21:40 +0000350STATIC mp_obj_t stream_unbuffered_readline(size_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000351 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200352
Damien George40f3c022014-07-03 13:25:24 +0100353 mp_int_t max_size = -1;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200354 if (n_args > 1) {
355 max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
356 }
357
Damien George0d3cb672015-01-28 23:43:01 +0000358 vstr_t vstr;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200359 if (max_size != -1) {
Damien George0d3cb672015-01-28 23:43:01 +0000360 vstr_init(&vstr, max_size);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200361 } else {
Damien George0d3cb672015-01-28 23:43:01 +0000362 vstr_init(&vstr, 16);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200363 }
364
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200365 while (max_size == -1 || max_size-- != 0) {
Damien George0d3cb672015-01-28 23:43:01 +0000366 char *p = vstr_add_len(&vstr, 1);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200367 if (p == NULL) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100368 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200369 }
370
Damien Georged00d8ac2014-10-24 11:26:12 +0000371 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000372 mp_uint_t out_sz = stream_p->read(args[0], p, 1, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100373 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300374 if (mp_is_nonblocking_error(error)) {
Damien George0d3cb672015-01-28 23:43:01 +0000375 if (vstr.len == 1) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300376 // We just incremented it, but otherwise we read nothing
377 // and immediately got EAGAIN. This is case is not well
378 // specified in
379 // https://docs.python.org/3/library/io.html#io.IOBase.readline
380 // unlike similar case for read(). But we follow the latter's
381 // behavior - return None.
Damien George0d3cb672015-01-28 23:43:01 +0000382 vstr_clear(&vstr);
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300383 return mp_const_none;
384 } else {
385 goto done;
386 }
387 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300388 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200389 }
Paul Sokolovsky09143712014-01-22 10:34:45 +0200390 if (out_sz == 0) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300391done:
Paul Sokolovsky09143712014-01-22 10:34:45 +0200392 // Back out previously added byte
Paul Sokolovsky09143712014-01-22 10:34:45 +0200393 // Consider, what's better - read a char and get OutOfMemory (so read
394 // char is lost), or allocate first as we do.
Damien George0d3cb672015-01-28 23:43:01 +0000395 vstr_cut_tail_bytes(&vstr, 1);
Paul Sokolovsky09143712014-01-22 10:34:45 +0200396 break;
397 }
398 if (*p == '\n') {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200399 break;
400 }
401 }
Damien George0d3cb672015-01-28 23:43:01 +0000402
Damien Georgee84325b2015-12-08 23:34:59 +0000403 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200404}
Damien George358e5d82016-04-10 12:41:28 +0100405MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200406
Damien Georged5f5b2f2014-05-03 22:01:32 +0100407// TODO take an optional extra argument (what does it do exactly?)
408STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) {
409 mp_obj_t lines = mp_obj_new_list(0, NULL);
410 for (;;) {
411 mp_obj_t line = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300412 if (!mp_obj_is_true(line)) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100413 break;
414 }
415 mp_obj_list_append(lines, line);
416 }
417 return lines;
418}
419MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines);
420
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200421mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
422 mp_obj_t l_in = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300423 if (mp_obj_is_true(l_in)) {
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200424 return l_in;
425 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100426 return MP_OBJ_STOP_ITERATION;
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200427}
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200428
Damien George4b72b3a2016-01-03 14:21:40 +0000429STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000430 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200431
432 struct mp_stream_seek_t seek_s;
433 // TODO: Could be uint64
434 seek_s.offset = mp_obj_get_int(args[1]);
435 seek_s.whence = 0;
436 if (n_args == 3) {
437 seek_s.whence = mp_obj_get_int(args[2]);
438 }
439
440 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000441 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 +0200442 if (res == MP_STREAM_ERROR) {
443 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
444 }
445
446 // TODO: Could be uint64
447 return mp_obj_new_int_from_uint(seek_s.offset);
448}
449MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj, 2, 3, stream_seek);
450
blmorrisbdd78c32015-08-04 19:45:30 -0400451STATIC mp_obj_t stream_tell(mp_obj_t self) {
452 mp_obj_t offset = MP_OBJ_NEW_SMALL_INT(0);
453 mp_obj_t whence = MP_OBJ_NEW_SMALL_INT(SEEK_CUR);
454 const mp_obj_t args[3] = {self, offset, whence};
455 return stream_seek(3, args);
456}
457MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
458
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300459STATIC mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
460 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
461
462 mp_buffer_info_t bufinfo;
Paul Sokolovsky558fd5d2016-04-10 13:36:44 +0300463 uintptr_t val = 0;
464 if (n_args > 2) {
Damien George657aef62016-04-10 12:37:59 +0100465 if (mp_get_buffer(args[2], &bufinfo, MP_BUFFER_WRITE)) {
Paul Sokolovsky558fd5d2016-04-10 13:36:44 +0300466 val = (uintptr_t)bufinfo.buf;
Damien George657aef62016-04-10 12:37:59 +0100467 } else {
468 val = mp_obj_get_int_truncated(args[2]);
Paul Sokolovsky558fd5d2016-04-10 13:36:44 +0300469 }
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300470 }
471
472 int error;
Paul Sokolovsky6c3db262016-04-10 13:31:52 +0300473 mp_uint_t res = stream_p->ioctl(args[0], mp_obj_get_int(args[1]), val, &error);
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300474 if (res == MP_STREAM_ERROR) {
475 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
476 }
477
478 return mp_obj_new_int(res);
479}
480MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj, 2, 3, stream_ioctl);