blob: a3df1b8fdd01bea753cee520c7018895b75bf491 [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
Damien Georgee84325b2015-12-08 23:34:59 +000052const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) {
53 mp_obj_base_t *o = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
54 const mp_stream_p_t *stream_p = o->type->stream_p;
55 if (stream_p == NULL
56 || ((flags & MP_STREAM_OP_READ) && stream_p->read == NULL)
57 || ((flags & MP_STREAM_OP_WRITE) && stream_p->write == NULL)
58 || ((flags & MP_STREAM_OP_IOCTL) && stream_p->ioctl == NULL)) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +020059 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgee84325b2015-12-08 23:34:59 +000060 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "stream operation not supported"));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020061 }
Damien Georgee84325b2015-12-08 23:34:59 +000062 return stream_p;
63}
64
Damien George4b72b3a2016-01-03 14:21:40 +000065STATIC mp_obj_t stream_read(size_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +000066 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovskye98cf402014-01-08 02:43:48 +020067
Damien George1694bc72014-07-16 11:45:10 +010068 // What to do if sz < -1? Python docs don't specify this case.
69 // CPython does a readall, but here we silently let negatives through,
70 // and they will cause a MemoryError.
Damien George40f3c022014-07-03 13:25:24 +010071 mp_int_t sz;
Paul Sokolovskya671f892014-01-16 12:53:46 +020072 if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) {
73 return stream_readall(args[0]);
74 }
Paul Sokolovskyf5f6c3b2014-06-15 23:23:36 +030075
76 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien Georgee84325b2015-12-08 23:34:59 +000077 if (stream_p->is_text) {
Damien George1694bc72014-07-16 11:45:10 +010078 // We need to read sz number of unicode characters. Because we don't have any
79 // buffering, and because the stream API can only read bytes, we must read here
80 // in units of bytes and must never over read. If we want sz chars, then reading
81 // sz bytes will never over-read, so we follow this approach, in a loop to keep
82 // reading until we have exactly enough chars. This will be 1 read for text
83 // with ASCII-only chars, and about 2 reads for text with a couple of non-ASCII
84 // chars. For text with lots of non-ASCII chars, it'll be pretty inefficient
85 // in time and memory.
86
87 vstr_t vstr;
88 vstr_init(&vstr, sz);
89 mp_uint_t more_bytes = sz;
90 mp_uint_t last_buf_offset = 0;
91 while (more_bytes > 0) {
92 char *p = vstr_add_len(&vstr, more_bytes);
93 if (p == NULL) {
94 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
95 }
96 int error;
Damien Georgee84325b2015-12-08 23:34:59 +000097 mp_uint_t out_sz = stream_p->read(args[0], p, more_bytes, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +010098 if (out_sz == MP_STREAM_ERROR) {
Damien George1694bc72014-07-16 11:45:10 +010099 vstr_cut_tail_bytes(&vstr, more_bytes);
Paul Sokolovsky77994102015-10-18 15:37:19 +0300100 if (mp_is_nonblocking_error(error)) {
Damien George1694bc72014-07-16 11:45:10 +0100101 // With non-blocking streams, we read as much as we can.
102 // If we read nothing, return None, just like read().
103 // Otherwise, return data read so far.
104 // TODO what if we have read only half a non-ASCII char?
105 if (vstr.len == 0) {
106 vstr_clear(&vstr);
107 return mp_const_none;
108 }
109 break;
110 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300111 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Damien George1694bc72014-07-16 11:45:10 +0100112 }
113
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100114 if (out_sz < more_bytes) {
Damien George1694bc72014-07-16 11:45:10 +0100115 // Finish reading.
116 // TODO what if we have read only half a non-ASCII char?
Dave Hylands1d8816c2014-07-21 16:51:07 -0700117 vstr_cut_tail_bytes(&vstr, more_bytes - out_sz);
118 if (out_sz == 0) {
119 break;
120 }
Damien George1694bc72014-07-16 11:45:10 +0100121 }
122
123 // count chars from bytes just read
124 for (mp_uint_t off = last_buf_offset;;) {
125 byte b = vstr.buf[off];
126 int n;
127 if (!UTF8_IS_NONASCII(b)) {
128 // 1-byte ASCII char
129 n = 1;
130 } else if ((b & 0xe0) == 0xc0) {
131 // 2-byte char
132 n = 2;
133 } else if ((b & 0xf0) == 0xe0) {
134 // 3-byte char
135 n = 3;
136 } else if ((b & 0xf8) == 0xf0) {
137 // 4-byte char
138 n = 4;
139 } else {
140 // TODO
141 n = 5;
142 }
143 if (off + n <= vstr.len) {
144 // got a whole char in n bytes
145 off += n;
146 sz -= 1;
147 last_buf_offset = off;
148 if (off >= vstr.len) {
149 more_bytes = sz;
150 break;
151 }
152 } else {
153 // didn't get a whole char, so work out how many extra bytes are needed for
154 // this partial char, plus bytes for additional chars that we want
155 more_bytes = (off + n - vstr.len) + (sz - 1);
156 break;
157 }
158 }
159 }
160
Damien George0b9ee862015-01-21 19:14:25 +0000161 return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
Paul Sokolovskyf5f6c3b2014-06-15 23:23:36 +0300162 }
163 #endif
164
Damien George05005f62015-01-21 22:48:37 +0000165 vstr_t vstr;
166 vstr_init_len(&vstr, sz);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200167 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000168 mp_uint_t out_sz = stream_p->read(args[0], vstr.buf, sz, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100169 if (out_sz == MP_STREAM_ERROR) {
Damien George05005f62015-01-21 22:48:37 +0000170 vstr_clear(&vstr);
Paul Sokolovsky77994102015-10-18 15:37:19 +0300171 if (mp_is_nonblocking_error(error)) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300172 // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
173 // "If the object is in non-blocking mode and no bytes are available,
174 // None is returned."
175 // This is actually very weird, as naive truth check will treat
176 // this as EOF.
177 return mp_const_none;
178 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300179 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200180 } else {
Damien George05005f62015-01-21 22:48:37 +0000181 vstr.len = out_sz;
Damien Georgee84325b2015-12-08 23:34:59 +0000182 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200183 }
184}
Damien George358e5d82016-04-10 12:41:28 +0100185MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200186
Damien George4e7107a2015-11-27 12:19:25 +0000187mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, size_t len) {
Damien Georgee84325b2015-12-08 23:34:59 +0000188 const mp_stream_p_t *stream_p = mp_get_stream_raise(self_in, MP_STREAM_OP_WRITE);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200189
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200190 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000191 mp_uint_t out_sz = stream_p->write(self_in, buf, len, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100192 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300193 if (mp_is_nonblocking_error(error)) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300194 // http://docs.python.org/3/library/io.html#io.RawIOBase.write
195 // "None is returned if the raw stream is set not to block and
196 // no single byte could be readily written to it."
197 // This is for consistency with read() behavior, still weird,
198 // see abobe.
199 return mp_const_none;
200 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300201 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200202 } else {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200203 return MP_OBJ_NEW_SMALL_INT(out_sz);
204 }
205}
206
Damien George999cedb2015-11-27 17:01:44 +0000207// XXX hack
208void mp_stream_write_adaptor(void *self, const char *buf, size_t len) {
209 mp_stream_write(MP_OBJ_FROM_PTR(self), buf, len);
210}
211
Paul Sokolovskyd4c8e622016-03-24 19:09:00 +0200212// Works only with blocking streams
213mp_uint_t mp_stream_writeall(mp_obj_t stream, const byte *buf, mp_uint_t size, int *errcode) {
214 mp_obj_base_t* s = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
Paul Sokolovsky53ad5ed2016-03-27 12:58:33 +0300215 mp_uint_t org_size = size;
216 while (size > 0) {
Paul Sokolovsky4a02a8f2016-03-24 19:43:08 +0200217 mp_uint_t out_sz = s->type->stream_p->write(stream, buf, size, errcode);
Paul Sokolovskyd4c8e622016-03-24 19:09:00 +0200218 if (out_sz == MP_STREAM_ERROR) {
219 return MP_STREAM_ERROR;
220 }
221 buf += out_sz;
Paul Sokolovsky53ad5ed2016-03-27 12:58:33 +0300222 size -= out_sz;
Paul Sokolovskyd4c8e622016-03-24 19:09:00 +0200223 }
Paul Sokolovsky53ad5ed2016-03-27 12:58:33 +0300224 return org_size;
Paul Sokolovskyd4c8e622016-03-24 19:09:00 +0200225}
226
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300227STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) {
228 mp_buffer_info_t bufinfo;
229 mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
230 return mp_stream_write(self_in, bufinfo.buf, bufinfo.len);
231}
Damien George358e5d82016-04-10 12:41:28 +0100232MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write_method);
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300233
Damien George4b72b3a2016-01-03 14:21:40 +0000234STATIC mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000235 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300236 mp_buffer_info_t bufinfo;
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300237 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
238
239 // CPython extension: if 2nd arg is provided, that's max len to read,
240 // instead of full buffer. Similar to
241 // https://docs.python.org/3/library/socket.html#socket.socket.recv_into
242 mp_uint_t len = bufinfo.len;
243 if (n_args > 2) {
Damien Georgec50772d2015-05-12 23:05:53 +0100244 len = mp_obj_get_int(args[2]);
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300245 if (len > bufinfo.len) {
246 len = bufinfo.len;
247 }
248 }
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300249
250 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000251 mp_uint_t out_sz = stream_p->read(args[0], bufinfo.buf, len, &error);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300252 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300253 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300254 return mp_const_none;
255 }
256 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
257 } else {
258 return MP_OBJ_NEW_SMALL_INT(out_sz);
259 }
260}
Damien George358e5d82016-04-10 12:41:28 +0100261MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300262
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200263STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
Damien Georgee84325b2015-12-08 23:34:59 +0000264 const mp_stream_p_t *stream_p = mp_get_stream_raise(self_in, MP_STREAM_OP_READ);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200265
Damien Georged00d8ac2014-10-24 11:26:12 +0000266 mp_uint_t total_size = 0;
Damien George0b9ee862015-01-21 19:14:25 +0000267 vstr_t vstr;
268 vstr_init(&vstr, DEFAULT_BUFFER_SIZE);
269 char *p = vstr.buf;
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200270 mp_uint_t current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200271 while (true) {
Damien Georged00d8ac2014-10-24 11:26:12 +0000272 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000273 mp_uint_t out_sz = stream_p->read(self_in, p, current_read, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100274 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300275 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky6e731432014-05-07 01:48:12 +0300276 // With non-blocking streams, we read as much as we can.
277 // If we read nothing, return None, just like read().
278 // Otherwise, return data read so far.
279 if (total_size == 0) {
280 return mp_const_none;
281 }
282 break;
283 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300284 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200285 }
286 if (out_sz == 0) {
287 break;
288 }
289 total_size += out_sz;
290 if (out_sz < current_read) {
291 current_read -= out_sz;
292 p += out_sz;
293 } else {
Damien George05005f62015-01-21 22:48:37 +0000294 p = vstr_extend(&vstr, DEFAULT_BUFFER_SIZE);
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200295 current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200296 if (p == NULL) {
297 // TODO
Damien Georgeea13f402014-04-05 18:32:08 +0100298 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200299 }
300 }
301 }
Damien George5fa93b62014-01-22 14:35:10 +0000302
Damien George0b9ee862015-01-21 19:14:25 +0000303 vstr.len = total_size;
Damien Georgee84325b2015-12-08 23:34:59 +0000304 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200305}
Damien George358e5d82016-04-10 12:41:28 +0100306MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200307
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200308// Unbuffered, inefficient implementation of readline() for raw I/O files.
Damien George4b72b3a2016-01-03 14:21:40 +0000309STATIC mp_obj_t stream_unbuffered_readline(size_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000310 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200311
Damien George40f3c022014-07-03 13:25:24 +0100312 mp_int_t max_size = -1;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200313 if (n_args > 1) {
314 max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
315 }
316
Damien George0d3cb672015-01-28 23:43:01 +0000317 vstr_t vstr;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200318 if (max_size != -1) {
Damien George0d3cb672015-01-28 23:43:01 +0000319 vstr_init(&vstr, max_size);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200320 } else {
Damien George0d3cb672015-01-28 23:43:01 +0000321 vstr_init(&vstr, 16);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200322 }
323
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200324 while (max_size == -1 || max_size-- != 0) {
Damien George0d3cb672015-01-28 23:43:01 +0000325 char *p = vstr_add_len(&vstr, 1);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200326 if (p == NULL) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100327 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200328 }
329
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(args[0], p, 1, &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)) {
Damien George0d3cb672015-01-28 23:43:01 +0000334 if (vstr.len == 1) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300335 // We just incremented it, but otherwise we read nothing
336 // and immediately got EAGAIN. This is case is not well
337 // specified in
338 // https://docs.python.org/3/library/io.html#io.IOBase.readline
339 // unlike similar case for read(). But we follow the latter's
340 // behavior - return None.
Damien George0d3cb672015-01-28 23:43:01 +0000341 vstr_clear(&vstr);
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300342 return mp_const_none;
343 } else {
344 goto done;
345 }
346 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300347 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200348 }
Paul Sokolovsky09143712014-01-22 10:34:45 +0200349 if (out_sz == 0) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300350done:
Paul Sokolovsky09143712014-01-22 10:34:45 +0200351 // Back out previously added byte
Paul Sokolovsky09143712014-01-22 10:34:45 +0200352 // Consider, what's better - read a char and get OutOfMemory (so read
353 // char is lost), or allocate first as we do.
Damien George0d3cb672015-01-28 23:43:01 +0000354 vstr_cut_tail_bytes(&vstr, 1);
Paul Sokolovsky09143712014-01-22 10:34:45 +0200355 break;
356 }
357 if (*p == '\n') {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200358 break;
359 }
360 }
Damien George0d3cb672015-01-28 23:43:01 +0000361
Damien Georgee84325b2015-12-08 23:34:59 +0000362 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200363}
Damien George358e5d82016-04-10 12:41:28 +0100364MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200365
Damien Georged5f5b2f2014-05-03 22:01:32 +0100366// TODO take an optional extra argument (what does it do exactly?)
367STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) {
368 mp_obj_t lines = mp_obj_new_list(0, NULL);
369 for (;;) {
370 mp_obj_t line = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300371 if (!mp_obj_is_true(line)) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100372 break;
373 }
374 mp_obj_list_append(lines, line);
375 }
376 return lines;
377}
378MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines);
379
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200380mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
381 mp_obj_t l_in = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300382 if (mp_obj_is_true(l_in)) {
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200383 return l_in;
384 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100385 return MP_OBJ_STOP_ITERATION;
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200386}
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200387
Damien George4b72b3a2016-01-03 14:21:40 +0000388STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000389 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200390
391 struct mp_stream_seek_t seek_s;
392 // TODO: Could be uint64
393 seek_s.offset = mp_obj_get_int(args[1]);
394 seek_s.whence = 0;
395 if (n_args == 3) {
396 seek_s.whence = mp_obj_get_int(args[2]);
397 }
398
399 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000400 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 +0200401 if (res == MP_STREAM_ERROR) {
402 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
403 }
404
405 // TODO: Could be uint64
406 return mp_obj_new_int_from_uint(seek_s.offset);
407}
408MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj, 2, 3, stream_seek);
409
blmorrisbdd78c32015-08-04 19:45:30 -0400410STATIC mp_obj_t stream_tell(mp_obj_t self) {
411 mp_obj_t offset = MP_OBJ_NEW_SMALL_INT(0);
412 mp_obj_t whence = MP_OBJ_NEW_SMALL_INT(SEEK_CUR);
413 const mp_obj_t args[3] = {self, offset, whence};
414 return stream_seek(3, args);
415}
416MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
417
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300418STATIC mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
419 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
420
421 mp_buffer_info_t bufinfo;
Paul Sokolovsky558fd5d2016-04-10 13:36:44 +0300422 uintptr_t val = 0;
423 if (n_args > 2) {
Damien George657aef62016-04-10 12:37:59 +0100424 if (mp_get_buffer(args[2], &bufinfo, MP_BUFFER_WRITE)) {
Paul Sokolovsky558fd5d2016-04-10 13:36:44 +0300425 val = (uintptr_t)bufinfo.buf;
Damien George657aef62016-04-10 12:37:59 +0100426 } else {
427 val = mp_obj_get_int_truncated(args[2]);
Paul Sokolovsky558fd5d2016-04-10 13:36:44 +0300428 }
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300429 }
430
431 int error;
Paul Sokolovsky6c3db262016-04-10 13:31:52 +0300432 mp_uint_t res = stream_p->ioctl(args[0], mp_obj_get_int(args[1]), val, &error);
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300433 if (res == MP_STREAM_ERROR) {
434 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
435 }
436
437 return mp_obj_new_int(res);
438}
439MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj, 2, 3, stream_ioctl);