blob: 5c161ec2dc2012838788c46810070d7b49a646d5 [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 Georged00d8ac2014-10-24 11:26:12 +000052STATIC mp_obj_t stream_read(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovskya671f892014-01-16 12:53:46 +020053 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
Damien George27e735f2014-04-05 23:02:23 +010054 if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +020055 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +010056 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020057 }
58
Damien George1694bc72014-07-16 11:45:10 +010059 // What to do if sz < -1? Python docs don't specify this case.
60 // CPython does a readall, but here we silently let negatives through,
61 // and they will cause a MemoryError.
Damien George40f3c022014-07-03 13:25:24 +010062 mp_int_t sz;
Paul Sokolovskya671f892014-01-16 12:53:46 +020063 if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) {
64 return stream_readall(args[0]);
65 }
Paul Sokolovskyf5f6c3b2014-06-15 23:23:36 +030066
67 #if MICROPY_PY_BUILTINS_STR_UNICODE
Damien Georgeadf0f2a2014-07-27 22:38:58 +010068 if (o->type->stream_p->is_text) {
Damien George1694bc72014-07-16 11:45:10 +010069 // We need to read sz number of unicode characters. Because we don't have any
70 // buffering, and because the stream API can only read bytes, we must read here
71 // in units of bytes and must never over read. If we want sz chars, then reading
72 // sz bytes will never over-read, so we follow this approach, in a loop to keep
73 // reading until we have exactly enough chars. This will be 1 read for text
74 // with ASCII-only chars, and about 2 reads for text with a couple of non-ASCII
75 // chars. For text with lots of non-ASCII chars, it'll be pretty inefficient
76 // in time and memory.
77
78 vstr_t vstr;
79 vstr_init(&vstr, sz);
80 mp_uint_t more_bytes = sz;
81 mp_uint_t last_buf_offset = 0;
82 while (more_bytes > 0) {
83 char *p = vstr_add_len(&vstr, more_bytes);
84 if (p == NULL) {
85 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
86 }
87 int error;
Damien Georgeadf0f2a2014-07-27 22:38:58 +010088 mp_uint_t out_sz = o->type->stream_p->read(o, p, more_bytes, &error);
89 if (out_sz == MP_STREAM_ERROR) {
Damien George1694bc72014-07-16 11:45:10 +010090 vstr_cut_tail_bytes(&vstr, more_bytes);
Paul Sokolovsky77994102015-10-18 15:37:19 +030091 if (mp_is_nonblocking_error(error)) {
Damien George1694bc72014-07-16 11:45:10 +010092 // With non-blocking streams, we read as much as we can.
93 // If we read nothing, return None, just like read().
94 // Otherwise, return data read so far.
95 // TODO what if we have read only half a non-ASCII char?
96 if (vstr.len == 0) {
97 vstr_clear(&vstr);
98 return mp_const_none;
99 }
100 break;
101 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300102 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Damien George1694bc72014-07-16 11:45:10 +0100103 }
104
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100105 if (out_sz < more_bytes) {
Damien George1694bc72014-07-16 11:45:10 +0100106 // Finish reading.
107 // TODO what if we have read only half a non-ASCII char?
Dave Hylands1d8816c2014-07-21 16:51:07 -0700108 vstr_cut_tail_bytes(&vstr, more_bytes - out_sz);
109 if (out_sz == 0) {
110 break;
111 }
Damien George1694bc72014-07-16 11:45:10 +0100112 }
113
114 // count chars from bytes just read
115 for (mp_uint_t off = last_buf_offset;;) {
116 byte b = vstr.buf[off];
117 int n;
118 if (!UTF8_IS_NONASCII(b)) {
119 // 1-byte ASCII char
120 n = 1;
121 } else if ((b & 0xe0) == 0xc0) {
122 // 2-byte char
123 n = 2;
124 } else if ((b & 0xf0) == 0xe0) {
125 // 3-byte char
126 n = 3;
127 } else if ((b & 0xf8) == 0xf0) {
128 // 4-byte char
129 n = 4;
130 } else {
131 // TODO
132 n = 5;
133 }
134 if (off + n <= vstr.len) {
135 // got a whole char in n bytes
136 off += n;
137 sz -= 1;
138 last_buf_offset = off;
139 if (off >= vstr.len) {
140 more_bytes = sz;
141 break;
142 }
143 } else {
144 // didn't get a whole char, so work out how many extra bytes are needed for
145 // this partial char, plus bytes for additional chars that we want
146 more_bytes = (off + n - vstr.len) + (sz - 1);
147 break;
148 }
149 }
150 }
151
Damien George0b9ee862015-01-21 19:14:25 +0000152 return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
Paul Sokolovskyf5f6c3b2014-06-15 23:23:36 +0300153 }
154 #endif
155
Damien George05005f62015-01-21 22:48:37 +0000156 vstr_t vstr;
157 vstr_init_len(&vstr, sz);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200158 int error;
Damien George05005f62015-01-21 22:48:37 +0000159 mp_uint_t out_sz = o->type->stream_p->read(o, vstr.buf, sz, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100160 if (out_sz == MP_STREAM_ERROR) {
Damien George05005f62015-01-21 22:48:37 +0000161 vstr_clear(&vstr);
Paul Sokolovsky77994102015-10-18 15:37:19 +0300162 if (mp_is_nonblocking_error(error)) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300163 // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
164 // "If the object is in non-blocking mode and no bytes are available,
165 // None is returned."
166 // This is actually very weird, as naive truth check will treat
167 // this as EOF.
168 return mp_const_none;
169 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300170 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200171 } else {
Damien George05005f62015-01-21 22:48:37 +0000172 vstr.len = out_sz;
Damien George05005f62015-01-21 22:48:37 +0000173 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), &vstr);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200174 }
175}
176
Damien George4e7107a2015-11-27 12:19:25 +0000177mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, size_t len) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200178 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
Damien George27e735f2014-04-05 23:02:23 +0100179 if (o->type->stream_p == NULL || o->type->stream_p->write == NULL) {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200180 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +0100181 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200182 }
183
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200184 int error;
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100185 mp_uint_t out_sz = o->type->stream_p->write(self_in, buf, len, &error);
186 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300187 if (mp_is_nonblocking_error(error)) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300188 // http://docs.python.org/3/library/io.html#io.RawIOBase.write
189 // "None is returned if the raw stream is set not to block and
190 // no single byte could be readily written to it."
191 // This is for consistency with read() behavior, still weird,
192 // see abobe.
193 return mp_const_none;
194 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300195 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200196 } else {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200197 return MP_OBJ_NEW_SMALL_INT(out_sz);
198 }
199}
200
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300201STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) {
202 mp_buffer_info_t bufinfo;
203 mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
204 return mp_stream_write(self_in, bufinfo.buf, bufinfo.len);
205}
206
Damien Georged00d8ac2014-10-24 11:26:12 +0000207STATIC mp_obj_t stream_readinto(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300208 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300209 if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
210 // CPython: io.UnsupportedOperation, OSError subclass
211 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
212 }
213 mp_buffer_info_t bufinfo;
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300214 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
215
216 // CPython extension: if 2nd arg is provided, that's max len to read,
217 // instead of full buffer. Similar to
218 // https://docs.python.org/3/library/socket.html#socket.socket.recv_into
219 mp_uint_t len = bufinfo.len;
220 if (n_args > 2) {
Damien Georgec50772d2015-05-12 23:05:53 +0100221 len = mp_obj_get_int(args[2]);
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300222 if (len > bufinfo.len) {
223 len = bufinfo.len;
224 }
225 }
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300226
227 int error;
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300228 mp_uint_t out_sz = o->type->stream_p->read(o, bufinfo.buf, len, &error);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300229 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300230 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300231 return mp_const_none;
232 }
233 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
234 } else {
235 return MP_OBJ_NEW_SMALL_INT(out_sz);
236 }
237}
238
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200239STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
Paul Sokolovsky52254502014-01-13 23:25:33 +0200240 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
Damien George27e735f2014-04-05 23:02:23 +0100241 if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
Paul Sokolovsky52254502014-01-13 23:25:33 +0200242 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +0100243 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200244 }
245
Damien Georged00d8ac2014-10-24 11:26:12 +0000246 mp_uint_t total_size = 0;
Damien George0b9ee862015-01-21 19:14:25 +0000247 vstr_t vstr;
248 vstr_init(&vstr, DEFAULT_BUFFER_SIZE);
249 char *p = vstr.buf;
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200250 mp_uint_t current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200251 while (true) {
Damien Georged00d8ac2014-10-24 11:26:12 +0000252 int error;
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100253 mp_uint_t out_sz = o->type->stream_p->read(self_in, p, current_read, &error);
254 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300255 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky6e731432014-05-07 01:48:12 +0300256 // With non-blocking streams, we read as much as we can.
257 // If we read nothing, return None, just like read().
258 // Otherwise, return data read so far.
259 if (total_size == 0) {
260 return mp_const_none;
261 }
262 break;
263 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300264 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200265 }
266 if (out_sz == 0) {
267 break;
268 }
269 total_size += out_sz;
270 if (out_sz < current_read) {
271 current_read -= out_sz;
272 p += out_sz;
273 } else {
Damien George05005f62015-01-21 22:48:37 +0000274 p = vstr_extend(&vstr, DEFAULT_BUFFER_SIZE);
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200275 current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200276 if (p == NULL) {
277 // TODO
Damien Georgeea13f402014-04-05 18:32:08 +0100278 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200279 }
280 }
281 }
Damien George5fa93b62014-01-22 14:35:10 +0000282
Damien George0b9ee862015-01-21 19:14:25 +0000283 vstr.len = total_size;
Damien George0b9ee862015-01-21 19:14:25 +0000284 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), &vstr);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200285}
286
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200287// Unbuffered, inefficient implementation of readline() for raw I/O files.
Damien Georged00d8ac2014-10-24 11:26:12 +0000288STATIC mp_obj_t stream_unbuffered_readline(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200289 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
Damien George27e735f2014-04-05 23:02:23 +0100290 if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200291 // CPython: io.UnsupportedOperation, OSError subclass
Damien Georgeea13f402014-04-05 18:32:08 +0100292 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200293 }
294
Damien George40f3c022014-07-03 13:25:24 +0100295 mp_int_t max_size = -1;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200296 if (n_args > 1) {
297 max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
298 }
299
Damien George0d3cb672015-01-28 23:43:01 +0000300 vstr_t vstr;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200301 if (max_size != -1) {
Damien George0d3cb672015-01-28 23:43:01 +0000302 vstr_init(&vstr, max_size);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200303 } else {
Damien George0d3cb672015-01-28 23:43:01 +0000304 vstr_init(&vstr, 16);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200305 }
306
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200307 while (max_size == -1 || max_size-- != 0) {
Damien George0d3cb672015-01-28 23:43:01 +0000308 char *p = vstr_add_len(&vstr, 1);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200309 if (p == NULL) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100310 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200311 }
312
Damien Georged00d8ac2014-10-24 11:26:12 +0000313 int error;
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100314 mp_uint_t out_sz = o->type->stream_p->read(o, p, 1, &error);
315 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300316 if (mp_is_nonblocking_error(error)) {
Damien George0d3cb672015-01-28 23:43:01 +0000317 if (vstr.len == 1) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300318 // We just incremented it, but otherwise we read nothing
319 // and immediately got EAGAIN. This is case is not well
320 // specified in
321 // https://docs.python.org/3/library/io.html#io.IOBase.readline
322 // unlike similar case for read(). But we follow the latter's
323 // behavior - return None.
Damien George0d3cb672015-01-28 23:43:01 +0000324 vstr_clear(&vstr);
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300325 return mp_const_none;
326 } else {
327 goto done;
328 }
329 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300330 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200331 }
Paul Sokolovsky09143712014-01-22 10:34:45 +0200332 if (out_sz == 0) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300333done:
Paul Sokolovsky09143712014-01-22 10:34:45 +0200334 // Back out previously added byte
Paul Sokolovsky09143712014-01-22 10:34:45 +0200335 // Consider, what's better - read a char and get OutOfMemory (so read
336 // char is lost), or allocate first as we do.
Damien George0d3cb672015-01-28 23:43:01 +0000337 vstr_cut_tail_bytes(&vstr, 1);
Paul Sokolovsky09143712014-01-22 10:34:45 +0200338 break;
339 }
340 if (*p == '\n') {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200341 break;
342 }
343 }
Damien George0d3cb672015-01-28 23:43:01 +0000344
345 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), &vstr);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200346}
347
Damien Georged5f5b2f2014-05-03 22:01:32 +0100348// TODO take an optional extra argument (what does it do exactly?)
349STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) {
350 mp_obj_t lines = mp_obj_new_list(0, NULL);
351 for (;;) {
352 mp_obj_t line = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300353 if (!mp_obj_is_true(line)) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100354 break;
355 }
356 mp_obj_list_append(lines, line);
357 }
358 return lines;
359}
360MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines);
361
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200362mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
363 mp_obj_t l_in = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300364 if (mp_obj_is_true(l_in)) {
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200365 return l_in;
366 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100367 return MP_OBJ_STOP_ITERATION;
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200368}
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200369
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200370STATIC mp_obj_t stream_seek(mp_uint_t n_args, const mp_obj_t *args) {
371 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
Damien George5694cc52014-11-16 23:56:37 +0000372 if (o->type->stream_p == NULL || o->type->stream_p->ioctl == NULL) {
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200373 // CPython: io.UnsupportedOperation, OSError subclass
374 nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
375 }
376
377 struct mp_stream_seek_t seek_s;
378 // TODO: Could be uint64
379 seek_s.offset = mp_obj_get_int(args[1]);
380 seek_s.whence = 0;
381 if (n_args == 3) {
382 seek_s.whence = mp_obj_get_int(args[2]);
383 }
384
385 int error;
386 mp_uint_t res = o->type->stream_p->ioctl(o, MP_STREAM_SEEK, (mp_uint_t)&seek_s, &error);
387 if (res == MP_STREAM_ERROR) {
388 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
389 }
390
391 // TODO: Could be uint64
392 return mp_obj_new_int_from_uint(seek_s.offset);
393}
394MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj, 2, 3, stream_seek);
395
blmorrisbdd78c32015-08-04 19:45:30 -0400396STATIC mp_obj_t stream_tell(mp_obj_t self) {
397 mp_obj_t offset = MP_OBJ_NEW_SMALL_INT(0);
398 mp_obj_t whence = MP_OBJ_NEW_SMALL_INT(SEEK_CUR);
399 const mp_obj_t args[3] = {self, offset, whence};
400 return stream_seek(3, args);
401}
402MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
403
Paul Sokolovskya671f892014-01-16 12:53:46 +0200404MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300405MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200406MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200407MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300408MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write_method);