blob: fcdd6be72520a94be961e912893c72b3be6507fb [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}
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 Sokolovskyd4c8e622016-03-24 19:09:00 +0200211// Works only with blocking streams
212mp_uint_t mp_stream_writeall(mp_obj_t stream, const byte *buf, mp_uint_t size, int *errcode) {
213 mp_obj_base_t* s = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
Paul Sokolovsky53ad5ed2016-03-27 12:58:33 +0300214 mp_uint_t org_size = size;
215 while (size > 0) {
Paul Sokolovsky4a02a8f2016-03-24 19:43:08 +0200216 mp_uint_t out_sz = s->type->stream_p->write(stream, buf, size, errcode);
Paul Sokolovskyd4c8e622016-03-24 19:09:00 +0200217 if (out_sz == MP_STREAM_ERROR) {
218 return MP_STREAM_ERROR;
219 }
220 buf += out_sz;
Paul Sokolovsky53ad5ed2016-03-27 12:58:33 +0300221 size -= out_sz;
Paul Sokolovskyd4c8e622016-03-24 19:09:00 +0200222 }
Paul Sokolovsky53ad5ed2016-03-27 12:58:33 +0300223 return org_size;
Paul Sokolovskyd4c8e622016-03-24 19:09:00 +0200224}
225
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300226STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) {
227 mp_buffer_info_t bufinfo;
228 mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
229 return mp_stream_write(self_in, bufinfo.buf, bufinfo.len);
230}
231
Damien George4b72b3a2016-01-03 14:21:40 +0000232STATIC mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000233 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300234 mp_buffer_info_t bufinfo;
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300235 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
236
237 // CPython extension: if 2nd arg is provided, that's max len to read,
238 // instead of full buffer. Similar to
239 // https://docs.python.org/3/library/socket.html#socket.socket.recv_into
240 mp_uint_t len = bufinfo.len;
241 if (n_args > 2) {
Damien Georgec50772d2015-05-12 23:05:53 +0100242 len = mp_obj_get_int(args[2]);
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300243 if (len > bufinfo.len) {
244 len = bufinfo.len;
245 }
246 }
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300247
248 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000249 mp_uint_t out_sz = stream_p->read(args[0], bufinfo.buf, len, &error);
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300250 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300251 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky1a55b6a2014-10-18 22:44:07 +0300252 return mp_const_none;
253 }
254 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
255 } else {
256 return MP_OBJ_NEW_SMALL_INT(out_sz);
257 }
258}
259
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200260STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
Damien Georgee84325b2015-12-08 23:34:59 +0000261 const mp_stream_p_t *stream_p = mp_get_stream_raise(self_in, MP_STREAM_OP_READ);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200262
Damien Georged00d8ac2014-10-24 11:26:12 +0000263 mp_uint_t total_size = 0;
Damien George0b9ee862015-01-21 19:14:25 +0000264 vstr_t vstr;
265 vstr_init(&vstr, DEFAULT_BUFFER_SIZE);
266 char *p = vstr.buf;
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200267 mp_uint_t current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200268 while (true) {
Damien Georged00d8ac2014-10-24 11:26:12 +0000269 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000270 mp_uint_t out_sz = stream_p->read(self_in, p, current_read, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100271 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300272 if (mp_is_nonblocking_error(error)) {
Paul Sokolovsky6e731432014-05-07 01:48:12 +0300273 // With non-blocking streams, we read as much as we can.
274 // If we read nothing, return None, just like read().
275 // Otherwise, return data read so far.
276 if (total_size == 0) {
277 return mp_const_none;
278 }
279 break;
280 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300281 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200282 }
283 if (out_sz == 0) {
284 break;
285 }
286 total_size += out_sz;
287 if (out_sz < current_read) {
288 current_read -= out_sz;
289 p += out_sz;
290 } else {
Damien George05005f62015-01-21 22:48:37 +0000291 p = vstr_extend(&vstr, DEFAULT_BUFFER_SIZE);
Paul Sokolovsky425f9522015-01-23 17:58:05 +0200292 current_read = DEFAULT_BUFFER_SIZE;
Paul Sokolovsky52254502014-01-13 23:25:33 +0200293 if (p == NULL) {
294 // TODO
Damien Georgeea13f402014-04-05 18:32:08 +0100295 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
Paul Sokolovsky52254502014-01-13 23:25:33 +0200296 }
297 }
298 }
Damien George5fa93b62014-01-22 14:35:10 +0000299
Damien George0b9ee862015-01-21 19:14:25 +0000300 vstr.len = total_size;
Damien Georgee84325b2015-12-08 23:34:59 +0000301 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200302}
303
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200304// Unbuffered, inefficient implementation of readline() for raw I/O files.
Damien George4b72b3a2016-01-03 14:21:40 +0000305STATIC mp_obj_t stream_unbuffered_readline(size_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000306 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200307
Damien George40f3c022014-07-03 13:25:24 +0100308 mp_int_t max_size = -1;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200309 if (n_args > 1) {
310 max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
311 }
312
Damien George0d3cb672015-01-28 23:43:01 +0000313 vstr_t vstr;
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200314 if (max_size != -1) {
Damien George0d3cb672015-01-28 23:43:01 +0000315 vstr_init(&vstr, max_size);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200316 } else {
Damien George0d3cb672015-01-28 23:43:01 +0000317 vstr_init(&vstr, 16);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200318 }
319
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200320 while (max_size == -1 || max_size-- != 0) {
Damien George0d3cb672015-01-28 23:43:01 +0000321 char *p = vstr_add_len(&vstr, 1);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200322 if (p == NULL) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100323 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory"));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200324 }
325
Damien Georged00d8ac2014-10-24 11:26:12 +0000326 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000327 mp_uint_t out_sz = stream_p->read(args[0], p, 1, &error);
Damien Georgeadf0f2a2014-07-27 22:38:58 +0100328 if (out_sz == MP_STREAM_ERROR) {
Paul Sokolovsky77994102015-10-18 15:37:19 +0300329 if (mp_is_nonblocking_error(error)) {
Damien George0d3cb672015-01-28 23:43:01 +0000330 if (vstr.len == 1) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300331 // We just incremented it, but otherwise we read nothing
332 // and immediately got EAGAIN. This is case is not well
333 // specified in
334 // https://docs.python.org/3/library/io.html#io.IOBase.readline
335 // unlike similar case for read(). But we follow the latter's
336 // behavior - return None.
Damien George0d3cb672015-01-28 23:43:01 +0000337 vstr_clear(&vstr);
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300338 return mp_const_none;
339 } else {
340 goto done;
341 }
342 }
Paul Sokolovsky0c7b26c2014-10-16 02:56:24 +0300343 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200344 }
Paul Sokolovsky09143712014-01-22 10:34:45 +0200345 if (out_sz == 0) {
Paul Sokolovsky923a8a82014-10-16 12:22:52 +0300346done:
Paul Sokolovsky09143712014-01-22 10:34:45 +0200347 // Back out previously added byte
Paul Sokolovsky09143712014-01-22 10:34:45 +0200348 // Consider, what's better - read a char and get OutOfMemory (so read
349 // char is lost), or allocate first as we do.
Damien George0d3cb672015-01-28 23:43:01 +0000350 vstr_cut_tail_bytes(&vstr, 1);
Paul Sokolovsky09143712014-01-22 10:34:45 +0200351 break;
352 }
353 if (*p == '\n') {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200354 break;
355 }
356 }
Damien George0d3cb672015-01-28 23:43:01 +0000357
Damien Georgee84325b2015-12-08 23:34:59 +0000358 return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200359}
360
Damien Georged5f5b2f2014-05-03 22:01:32 +0100361// TODO take an optional extra argument (what does it do exactly?)
362STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) {
363 mp_obj_t lines = mp_obj_new_list(0, NULL);
364 for (;;) {
365 mp_obj_t line = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300366 if (!mp_obj_is_true(line)) {
Damien Georged5f5b2f2014-05-03 22:01:32 +0100367 break;
368 }
369 mp_obj_list_append(lines, line);
370 }
371 return lines;
372}
373MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines);
374
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200375mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
376 mp_obj_t l_in = stream_unbuffered_readline(1, &self);
Paul Sokolovskye22cddb2014-06-13 23:46:21 +0300377 if (mp_obj_is_true(l_in)) {
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200378 return l_in;
379 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100380 return MP_OBJ_STOP_ITERATION;
Paul Sokolovskyd54bef72014-01-20 18:35:32 +0200381}
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200382
Damien George4b72b3a2016-01-03 14:21:40 +0000383STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) {
Damien Georgee84325b2015-12-08 23:34:59 +0000384 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
Paul Sokolovsky838eb1f2014-11-17 00:16:14 +0200385
386 struct mp_stream_seek_t seek_s;
387 // TODO: Could be uint64
388 seek_s.offset = mp_obj_get_int(args[1]);
389 seek_s.whence = 0;
390 if (n_args == 3) {
391 seek_s.whence = mp_obj_get_int(args[2]);
392 }
393
394 int error;
Damien Georgee84325b2015-12-08 23:34:59 +0000395 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 +0200396 if (res == MP_STREAM_ERROR) {
397 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
398 }
399
400 // TODO: Could be uint64
401 return mp_obj_new_int_from_uint(seek_s.offset);
402}
403MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj, 2, 3, stream_seek);
404
blmorrisbdd78c32015-08-04 19:45:30 -0400405STATIC mp_obj_t stream_tell(mp_obj_t self) {
406 mp_obj_t offset = MP_OBJ_NEW_SMALL_INT(0);
407 mp_obj_t whence = MP_OBJ_NEW_SMALL_INT(SEEK_CUR);
408 const mp_obj_t args[3] = {self, offset, whence};
409 return stream_seek(3, args);
410}
411MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
412
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300413STATIC mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
414 const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
415
416 mp_buffer_info_t bufinfo;
417 uintptr_t val;
418 if (MP_OBJ_IS_INT(args[2])) {
419 val = mp_obj_get_int(args[2]);
420 } else {
421 mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
422 val = (uintptr_t)bufinfo.buf;
423 }
424
425 int error;
Paul Sokolovsky6c3db262016-04-10 13:31:52 +0300426 mp_uint_t res = stream_p->ioctl(args[0], mp_obj_get_int(args[1]), val, &error);
Paul Sokolovsky0c97e4c2016-04-10 12:45:46 +0300427 if (res == MP_STREAM_ERROR) {
428 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
429 }
430
431 return mp_obj_new_int(res);
432}
433MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj, 2, 3, stream_ioctl);
434
Paul Sokolovskya671f892014-01-16 12:53:46 +0200435MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
Paul Sokolovskye2f8d982014-10-23 21:22:08 +0300436MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200437MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200438MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
Paul Sokolovskyac736f12014-07-13 23:04:17 +0300439MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write_method);