blob: d29db621bc468b20430bf87d9063baf76d32f442 [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
65STATIC mp_obj_t stream_read(mp_uint_t n_args, const mp_obj_t *args) {
66 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}
185
Damien George4e7107a2015-11-27 12:19:25 +0000186mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, size_t len) {
Damien Georgee84325b2015-12-08 23:34:59 +0000187 const mp_stream_p_t *stream_p = mp_get_stream_raise(self_in, MP_STREAM_OP_WRITE);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200188
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200189 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000190 mp_uint_t out_sz = stream_p->write(self_in, buf, len, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100191 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300192 if (mp_is_nonblocking_error(error)) {
Paul Sokolovskya5921042014-05-07 01:39:38 +0300193 // http://docs.python.org/3/library/io.html#io.RawIOBase.write
194 // "None is returned if the raw stream is set not to block and
195 // no single byte could be readily written to it."
196 // This is for consistency with read() behavior, still weird,
197 // see abobe.
198 return mp_const_none;
199 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300200 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200201 } else {
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200202 return MP_OBJ_NEW_SMALL_INT(out_sz);
203 }
204}
205
Damien George999cedb2015-11-27 17:01:44 +0000206// XXX hack
207void mp_stream_write_adaptor(void *self, const char *buf, size_t len) {
208 mp_stream_write(MP_OBJ_FROM_PTR(self), buf, len);
209}
210
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300211STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) {
212 mp_buffer_info_t bufinfo;
213 mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
214 return mp_stream_write(self_in, bufinfo.buf, bufinfo.len);
215}
216
Damien Georged00d8ac2014-10-24 11:26:12 +0000217STATIC mp_obj_t stream_readinto(mp_uint_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000218 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300219 mp_buffer_info_t bufinfo;
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300220 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
221
222 // CPython extension: if 2nd arg is provided, that's max len to read,
223 // instead of full buffer. Similar to
224 // https://docs.python.org/3/library/socket.html#socket.socket.recv_into
225 mp_uint_t len = bufinfo.len;
226 if (n_args > 2) {
Damien Georgec50772d2015-05-12 23:05:53 +0100227 len = mp_obj_get_int(args[2]);
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300228 if (len > bufinfo.len) {
229 len = bufinfo.len;
230 }
231 }
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300232
233 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000234 mp_uint_t out_sz = stream_p->read(args[0], bufinfo.buf, len, &error);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300235 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300236 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300237 return mp_const_none;
238 }
239 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
240 } else {
241 return MP_OBJ_NEW_SMALL_INT(out_sz);
242 }
243}
244
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200245STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
Damien Georgee84325b2015-12-08 23:34:59 +0000246 const mp_stream_p_t *stream_p = mp_get_stream_raise(self_in, MP_STREAM_OP_READ);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200247
Damien Georged00d8ac2014-10-24 11:26:12 +0000248 mp_uint_t total_size = 0;
Damien George0b9ee862015-01-21 19:14:25 +0000249 vstr_t vstr;
250 vstr_init(&vstr, DEFAULT_BUFFER_SIZE);
251 char *p = vstr.buf;
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200252 mp_uint_t current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200253 while (true) {
Damien Georged00d8ac2014-10-24 11:26:12 +0000254 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000255 mp_uint_t out_sz = stream_p->read(self_in, p, current_read, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100256 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300257 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky6e731432014-05-07 01:48:12 +0300258 // With non-blocking streams, we read as much as we can.
259 // If we read nothing, return None, just like read().
260 // Otherwise, return data read so far.
261 if (total_size == 0) {
262 return mp_const_none;
263 }
264 break;
265 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300266 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200267 }
268 if (out_sz == 0) {
269 break;
270 }
271 total_size += out_sz;
272 if (out_sz < current_read) {
273 current_read -= out_sz;
274 p += out_sz;
275 } else {
Damien George05005f62015-01-21 22:48:37 +0000276 p = vstr_extend(&vstr, DEFAULT_BUFFER_SIZE);
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200277 current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200278 if (p == NULL) {
279 // TODO
Damien Georgeea13f402014-04-05 18:32:08 +0100280 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200281 }
282 }
283 }
Damien George5fa93b62014-01-22 14:35:10 +0000284
Damien George0b9ee862015-01-21 19:14:25 +0000285 vstr.len = total_size;
Damien Georgee84325b2015-12-08 23:34:59 +0000286 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200287}
288
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200289// Unbuffered, inefficient implementation of readline() for raw I/O files.
Damien Georged00d8ac2014-10-24 11:26:12 +0000290STATIC mp_obj_t stream_unbuffered_readline(mp_uint_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000291 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200292
Damien George40f3c022014-07-03 13:25:24 +0100293 mp_int_t max_size = -1;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200294 if (n_args > 1) {
295 max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
296 }
297
Damien George0d3cb672015-01-28 23:43:01 +0000298 vstr_t vstr;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200299 if (max_size != -1) {
Damien George0d3cb672015-01-28 23:43:01 +0000300 vstr_init(&vstr, max_size);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200301 } else {
Damien George0d3cb672015-01-28 23:43:01 +0000302 vstr_init(&vstr, 16);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200303 }
304
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200305 while (max_size == -1 || max_size-- != 0) {
Damien George0d3cb672015-01-28 23:43:01 +0000306 char *p = vstr_add_len(&vstr, 1);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200307 if (p == NULL) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100308 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200309 }
310
Damien Georged00d8ac2014-10-24 11:26:12 +0000311 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000312 mp_uint_t out_sz = stream_p->read(args[0], p, 1, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100313 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300314 if (mp_is_nonblocking_error(error)) {
Damien George0d3cb672015-01-28 23:43:01 +0000315 if (vstr.len == 1) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300316 // We just incremented it, but otherwise we read nothing
317 // and immediately got EAGAIN. This is case is not well
318 // specified in
319 // https://docs.python.org/3/library/io.html#io.IOBase.readline
320 // unlike similar case for read(). But we follow the latter's
321 // behavior - return None.
Damien George0d3cb672015-01-28 23:43:01 +0000322 vstr_clear(&vstr);
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300323 return mp_const_none;
324 } else {
325 goto done;
326 }
327 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300328 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200329 }
Paul Sokolovsky09143712014-01-22 10:34:45 +0200330 if (out_sz == 0) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300331done:
Paul Sokolovsky09143712014-01-22 10:34:45 +0200332 // Back out previously added byte
Paul Sokolovsky09143712014-01-22 10:34:45 +0200333 // Consider, what's better - read a char and get OutOfMemory (so read
334 // char is lost), or allocate first as we do.
Damien George0d3cb672015-01-28 23:43:01 +0000335 vstr_cut_tail_bytes(&vstr, 1);
Paul Sokolovsky09143712014-01-22 10:34:45 +0200336 break;
337 }
338 if (*p == '\n') {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200339 break;
340 }
341 }
Damien George0d3cb672015-01-28 23:43:01 +0000342
Damien Georgee84325b2015-12-08 23:34:59 +0000343 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200344}
345
Damien Georged5f5b2f2014-05-03 22:01:32 +0100346// TODO take an optional extra argument (what does it do exactly?)
347STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) {
348 mp_obj_t lines = mp_obj_new_list(0, NULL);
349 for (;;) {
350 mp_obj_t line = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300351 if (!mp_obj_is_true(line)) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100352 break;
353 }
354 mp_obj_list_append(lines, line);
355 }
356 return lines;
357}
358MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines);
359
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200360mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
361 mp_obj_t l_in = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300362 if (mp_obj_is_true(l_in)) {
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200363 return l_in;
364 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100365 return MP_OBJ_STOP_ITERATION;
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200366}
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200367
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200368STATIC mp_obj_t stream_seek(mp_uint_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000369 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200370
371 struct mp_stream_seek_t seek_s;
372 // TODO: Could be uint64
373 seek_s.offset = mp_obj_get_int(args[1]);
374 seek_s.whence = 0;
375 if (n_args == 3) {
376 seek_s.whence = mp_obj_get_int(args[2]);
377 }
378
379 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000380 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 +0200381 if (res == MP_STREAM_ERROR) {
382 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
383 }
384
385 // TODO: Could be uint64
386 return mp_obj_new_int_from_uint(seek_s.offset);
387}
388MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj, 2, 3, stream_seek);
389
blmorrisbdd78c32015-08-04 19:45:30 -0400390STATIC mp_obj_t stream_tell(mp_obj_t self) {
391 mp_obj_t offset = MP_OBJ_NEW_SMALL_INT(0);
392 mp_obj_t whence = MP_OBJ_NEW_SMALL_INT(SEEK_CUR);
393 const mp_obj_t args[3] = {self, offset, whence};
394 return stream_seek(3, args);
395}
396MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
397
Paul Sokolovskya671f892014-01-16 12:53:46 +0200398MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300399MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200400MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200401MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300402MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write_method);