Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 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 Sokolovsky | da9f092 | 2014-05-13 08:44:45 +0300 | [diff] [blame] | 7 | * Copyright (c) 2014 Paul Sokolovsky |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 8 | * |
| 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 Dmitrichenko | 5630b01 | 2014-04-12 16:44:32 +0100 | [diff] [blame] | 28 | #include <string.h> |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 29 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 30 | #include "py/nlr.h" |
| 31 | #include "py/objstr.h" |
| 32 | #include "py/stream.h" |
| 33 | |
Paul Sokolovsky | 0ef015b | 2014-05-07 02:23:46 +0300 | [diff] [blame] | 34 | #if MICROPY_STREAMS_NON_BLOCK |
| 35 | #include <errno.h> |
stijn | ec6fa87 | 2014-06-28 21:04:20 +0200 | [diff] [blame] | 36 | #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) |
| 37 | #define EWOULDBLOCK 140 |
| 38 | #endif |
Paul Sokolovsky | 0ef015b | 2014-05-07 02:23:46 +0300 | [diff] [blame] | 39 | #endif |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 40 | |
| 41 | // This file defines generic Python stream read/write methods which |
| 42 | // dispatch to the underlying stream interface of an object. |
| 43 | |
Paul Sokolovsky | b9be45e | 2014-05-07 01:51:07 +0300 | [diff] [blame] | 44 | // TODO: should be in mpconfig.h |
| 45 | #define DEFAULT_BUFFER_SIZE 256 |
| 46 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 47 | STATIC mp_obj_t stream_readall(mp_obj_t self_in); |
Paul Sokolovsky | a671f89 | 2014-01-16 12:53:46 +0200 | [diff] [blame] | 48 | |
Paul Sokolovsky | 0ef015b | 2014-05-07 02:23:46 +0300 | [diff] [blame] | 49 | #if MICROPY_STREAMS_NON_BLOCK |
Paul Sokolovsky | a592104 | 2014-05-07 01:39:38 +0300 | [diff] [blame] | 50 | // TODO: This is POSIX-specific (but then POSIX is the only real thing, |
| 51 | // and anything else just emulates it, right?) |
| 52 | #define is_nonblocking_error(errno) ((errno) == EAGAIN || (errno) == EWOULDBLOCK) |
Paul Sokolovsky | 0ef015b | 2014-05-07 02:23:46 +0300 | [diff] [blame] | 53 | #else |
| 54 | #define is_nonblocking_error(errno) (0) |
| 55 | #endif |
Paul Sokolovsky | a592104 | 2014-05-07 01:39:38 +0300 | [diff] [blame] | 56 | |
Damien George | adf0f2a | 2014-07-27 22:38:58 +0100 | [diff] [blame] | 57 | #define STREAM_CONTENT_TYPE(stream) (((stream)->is_text) ? &mp_type_str : &mp_type_bytes) |
Paul Sokolovsky | a47b64a | 2014-05-15 07:28:19 +0300 | [diff] [blame] | 58 | |
Damien George | d00d8ac | 2014-10-24 11:26:12 +0000 | [diff] [blame] | 59 | STATIC mp_obj_t stream_read(mp_uint_t n_args, const mp_obj_t *args) { |
Paul Sokolovsky | a671f89 | 2014-01-16 12:53:46 +0200 | [diff] [blame] | 60 | struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0]; |
Damien George | 27e735f | 2014-04-05 23:02:23 +0100 | [diff] [blame] | 61 | if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) { |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 62 | // CPython: io.UnsupportedOperation, OSError subclass |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 63 | nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported")); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Damien George | 1694bc7 | 2014-07-16 11:45:10 +0100 | [diff] [blame] | 66 | // What to do if sz < -1? Python docs don't specify this case. |
| 67 | // CPython does a readall, but here we silently let negatives through, |
| 68 | // and they will cause a MemoryError. |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 69 | mp_int_t sz; |
Paul Sokolovsky | a671f89 | 2014-01-16 12:53:46 +0200 | [diff] [blame] | 70 | if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) { |
| 71 | return stream_readall(args[0]); |
| 72 | } |
Paul Sokolovsky | f5f6c3b | 2014-06-15 23:23:36 +0300 | [diff] [blame] | 73 | |
| 74 | #if MICROPY_PY_BUILTINS_STR_UNICODE |
Damien George | adf0f2a | 2014-07-27 22:38:58 +0100 | [diff] [blame] | 75 | if (o->type->stream_p->is_text) { |
Damien George | 1694bc7 | 2014-07-16 11:45:10 +0100 | [diff] [blame] | 76 | // We need to read sz number of unicode characters. Because we don't have any |
| 77 | // buffering, and because the stream API can only read bytes, we must read here |
| 78 | // in units of bytes and must never over read. If we want sz chars, then reading |
| 79 | // sz bytes will never over-read, so we follow this approach, in a loop to keep |
| 80 | // reading until we have exactly enough chars. This will be 1 read for text |
| 81 | // with ASCII-only chars, and about 2 reads for text with a couple of non-ASCII |
| 82 | // chars. For text with lots of non-ASCII chars, it'll be pretty inefficient |
| 83 | // in time and memory. |
| 84 | |
| 85 | vstr_t vstr; |
| 86 | vstr_init(&vstr, sz); |
| 87 | mp_uint_t more_bytes = sz; |
| 88 | mp_uint_t last_buf_offset = 0; |
| 89 | while (more_bytes > 0) { |
| 90 | char *p = vstr_add_len(&vstr, more_bytes); |
| 91 | if (p == NULL) { |
| 92 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory")); |
| 93 | } |
| 94 | int error; |
Damien George | adf0f2a | 2014-07-27 22:38:58 +0100 | [diff] [blame] | 95 | mp_uint_t out_sz = o->type->stream_p->read(o, p, more_bytes, &error); |
| 96 | if (out_sz == MP_STREAM_ERROR) { |
Damien George | 1694bc7 | 2014-07-16 11:45:10 +0100 | [diff] [blame] | 97 | vstr_cut_tail_bytes(&vstr, more_bytes); |
| 98 | if (is_nonblocking_error(error)) { |
| 99 | // With non-blocking streams, we read as much as we can. |
| 100 | // If we read nothing, return None, just like read(). |
| 101 | // Otherwise, return data read so far. |
| 102 | // TODO what if we have read only half a non-ASCII char? |
| 103 | if (vstr.len == 0) { |
| 104 | vstr_clear(&vstr); |
| 105 | return mp_const_none; |
| 106 | } |
| 107 | break; |
| 108 | } |
Paul Sokolovsky | 0c7b26c | 2014-10-16 02:56:24 +0300 | [diff] [blame] | 109 | nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error))); |
Damien George | 1694bc7 | 2014-07-16 11:45:10 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Damien George | adf0f2a | 2014-07-27 22:38:58 +0100 | [diff] [blame] | 112 | if (out_sz < more_bytes) { |
Damien George | 1694bc7 | 2014-07-16 11:45:10 +0100 | [diff] [blame] | 113 | // Finish reading. |
| 114 | // TODO what if we have read only half a non-ASCII char? |
Dave Hylands | 1d8816c | 2014-07-21 16:51:07 -0700 | [diff] [blame] | 115 | vstr_cut_tail_bytes(&vstr, more_bytes - out_sz); |
| 116 | if (out_sz == 0) { |
| 117 | break; |
| 118 | } |
Damien George | 1694bc7 | 2014-07-16 11:45:10 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // count chars from bytes just read |
| 122 | for (mp_uint_t off = last_buf_offset;;) { |
| 123 | byte b = vstr.buf[off]; |
| 124 | int n; |
| 125 | if (!UTF8_IS_NONASCII(b)) { |
| 126 | // 1-byte ASCII char |
| 127 | n = 1; |
| 128 | } else if ((b & 0xe0) == 0xc0) { |
| 129 | // 2-byte char |
| 130 | n = 2; |
| 131 | } else if ((b & 0xf0) == 0xe0) { |
| 132 | // 3-byte char |
| 133 | n = 3; |
| 134 | } else if ((b & 0xf8) == 0xf0) { |
| 135 | // 4-byte char |
| 136 | n = 4; |
| 137 | } else { |
| 138 | // TODO |
| 139 | n = 5; |
| 140 | } |
| 141 | if (off + n <= vstr.len) { |
| 142 | // got a whole char in n bytes |
| 143 | off += n; |
| 144 | sz -= 1; |
| 145 | last_buf_offset = off; |
| 146 | if (off >= vstr.len) { |
| 147 | more_bytes = sz; |
| 148 | break; |
| 149 | } |
| 150 | } else { |
| 151 | // didn't get a whole char, so work out how many extra bytes are needed for |
| 152 | // this partial char, plus bytes for additional chars that we want |
| 153 | more_bytes = (off + n - vstr.len) + (sz - 1); |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
Damien George | 0b9ee86 | 2015-01-21 19:14:25 +0000 | [diff] [blame] | 159 | return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); |
Paul Sokolovsky | f5f6c3b | 2014-06-15 23:23:36 +0300 | [diff] [blame] | 160 | } |
| 161 | #endif |
| 162 | |
Damien George | 05005f6 | 2015-01-21 22:48:37 +0000 | [diff] [blame] | 163 | vstr_t vstr; |
| 164 | vstr_init_len(&vstr, sz); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 165 | int error; |
Damien George | 05005f6 | 2015-01-21 22:48:37 +0000 | [diff] [blame] | 166 | mp_uint_t out_sz = o->type->stream_p->read(o, vstr.buf, sz, &error); |
Damien George | adf0f2a | 2014-07-27 22:38:58 +0100 | [diff] [blame] | 167 | if (out_sz == MP_STREAM_ERROR) { |
Damien George | 05005f6 | 2015-01-21 22:48:37 +0000 | [diff] [blame] | 168 | vstr_clear(&vstr); |
Paul Sokolovsky | a592104 | 2014-05-07 01:39:38 +0300 | [diff] [blame] | 169 | if (is_nonblocking_error(error)) { |
| 170 | // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read |
| 171 | // "If the object is in non-blocking mode and no bytes are available, |
| 172 | // None is returned." |
| 173 | // This is actually very weird, as naive truth check will treat |
| 174 | // this as EOF. |
| 175 | return mp_const_none; |
| 176 | } |
Paul Sokolovsky | 0c7b26c | 2014-10-16 02:56:24 +0300 | [diff] [blame] | 177 | nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error))); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 178 | } else { |
Damien George | 05005f6 | 2015-01-21 22:48:37 +0000 | [diff] [blame] | 179 | vstr.len = out_sz; |
Damien George | 05005f6 | 2015-01-21 22:48:37 +0000 | [diff] [blame] | 180 | return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), &vstr); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Paul Sokolovsky | ac736f1 | 2014-07-13 23:04:17 +0300 | [diff] [blame] | 184 | mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, mp_uint_t len) { |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 185 | struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in; |
Damien George | 27e735f | 2014-04-05 23:02:23 +0100 | [diff] [blame] | 186 | if (o->type->stream_p == NULL || o->type->stream_p->write == NULL) { |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 187 | // CPython: io.UnsupportedOperation, OSError subclass |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 188 | nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported")); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 191 | int error; |
Damien George | adf0f2a | 2014-07-27 22:38:58 +0100 | [diff] [blame] | 192 | mp_uint_t out_sz = o->type->stream_p->write(self_in, buf, len, &error); |
| 193 | if (out_sz == MP_STREAM_ERROR) { |
Paul Sokolovsky | a592104 | 2014-05-07 01:39:38 +0300 | [diff] [blame] | 194 | if (is_nonblocking_error(error)) { |
| 195 | // http://docs.python.org/3/library/io.html#io.RawIOBase.write |
| 196 | // "None is returned if the raw stream is set not to block and |
| 197 | // no single byte could be readily written to it." |
| 198 | // This is for consistency with read() behavior, still weird, |
| 199 | // see abobe. |
| 200 | return mp_const_none; |
| 201 | } |
Paul Sokolovsky | 0c7b26c | 2014-10-16 02:56:24 +0300 | [diff] [blame] | 202 | nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error))); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 203 | } else { |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 204 | return MP_OBJ_NEW_SMALL_INT(out_sz); |
| 205 | } |
| 206 | } |
| 207 | |
Paul Sokolovsky | ac736f1 | 2014-07-13 23:04:17 +0300 | [diff] [blame] | 208 | STATIC mp_obj_t stream_write_method(mp_obj_t self_in, mp_obj_t arg) { |
| 209 | mp_buffer_info_t bufinfo; |
| 210 | mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); |
| 211 | return mp_stream_write(self_in, bufinfo.buf, bufinfo.len); |
| 212 | } |
| 213 | |
Damien George | d00d8ac | 2014-10-24 11:26:12 +0000 | [diff] [blame] | 214 | STATIC mp_obj_t stream_readinto(mp_uint_t n_args, const mp_obj_t *args) { |
Paul Sokolovsky | e2f8d98 | 2014-10-23 21:22:08 +0300 | [diff] [blame] | 215 | struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0]; |
Paul Sokolovsky | 1a55b6a | 2014-10-18 22:44:07 +0300 | [diff] [blame] | 216 | if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) { |
| 217 | // CPython: io.UnsupportedOperation, OSError subclass |
| 218 | nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported")); |
| 219 | } |
| 220 | mp_buffer_info_t bufinfo; |
Paul Sokolovsky | e2f8d98 | 2014-10-23 21:22:08 +0300 | [diff] [blame] | 221 | mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE); |
| 222 | |
| 223 | // CPython extension: if 2nd arg is provided, that's max len to read, |
| 224 | // instead of full buffer. Similar to |
| 225 | // https://docs.python.org/3/library/socket.html#socket.socket.recv_into |
| 226 | mp_uint_t len = bufinfo.len; |
| 227 | if (n_args > 2) { |
Damien George | c50772d | 2015-05-12 23:05:53 +0100 | [diff] [blame^] | 228 | len = mp_obj_get_int(args[2]); |
Paul Sokolovsky | e2f8d98 | 2014-10-23 21:22:08 +0300 | [diff] [blame] | 229 | if (len > bufinfo.len) { |
| 230 | len = bufinfo.len; |
| 231 | } |
| 232 | } |
Paul Sokolovsky | 1a55b6a | 2014-10-18 22:44:07 +0300 | [diff] [blame] | 233 | |
| 234 | int error; |
Paul Sokolovsky | e2f8d98 | 2014-10-23 21:22:08 +0300 | [diff] [blame] | 235 | mp_uint_t out_sz = o->type->stream_p->read(o, bufinfo.buf, len, &error); |
Paul Sokolovsky | 1a55b6a | 2014-10-18 22:44:07 +0300 | [diff] [blame] | 236 | if (out_sz == MP_STREAM_ERROR) { |
| 237 | if (is_nonblocking_error(error)) { |
| 238 | return mp_const_none; |
| 239 | } |
| 240 | nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error))); |
| 241 | } else { |
| 242 | return MP_OBJ_NEW_SMALL_INT(out_sz); |
| 243 | } |
| 244 | } |
| 245 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 246 | STATIC mp_obj_t stream_readall(mp_obj_t self_in) { |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 247 | struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in; |
Damien George | 27e735f | 2014-04-05 23:02:23 +0100 | [diff] [blame] | 248 | if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) { |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 249 | // CPython: io.UnsupportedOperation, OSError subclass |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 250 | nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported")); |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Damien George | d00d8ac | 2014-10-24 11:26:12 +0000 | [diff] [blame] | 253 | mp_uint_t total_size = 0; |
Damien George | 0b9ee86 | 2015-01-21 19:14:25 +0000 | [diff] [blame] | 254 | vstr_t vstr; |
| 255 | vstr_init(&vstr, DEFAULT_BUFFER_SIZE); |
| 256 | char *p = vstr.buf; |
Paul Sokolovsky | 425f952 | 2015-01-23 17:58:05 +0200 | [diff] [blame] | 257 | mp_uint_t current_read = DEFAULT_BUFFER_SIZE; |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 258 | while (true) { |
Damien George | d00d8ac | 2014-10-24 11:26:12 +0000 | [diff] [blame] | 259 | int error; |
Damien George | adf0f2a | 2014-07-27 22:38:58 +0100 | [diff] [blame] | 260 | mp_uint_t out_sz = o->type->stream_p->read(self_in, p, current_read, &error); |
| 261 | if (out_sz == MP_STREAM_ERROR) { |
Paul Sokolovsky | 6e73143 | 2014-05-07 01:48:12 +0300 | [diff] [blame] | 262 | if (is_nonblocking_error(error)) { |
| 263 | // With non-blocking streams, we read as much as we can. |
| 264 | // If we read nothing, return None, just like read(). |
| 265 | // Otherwise, return data read so far. |
| 266 | if (total_size == 0) { |
| 267 | return mp_const_none; |
| 268 | } |
| 269 | break; |
| 270 | } |
Paul Sokolovsky | 0c7b26c | 2014-10-16 02:56:24 +0300 | [diff] [blame] | 271 | nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error))); |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 272 | } |
| 273 | if (out_sz == 0) { |
| 274 | break; |
| 275 | } |
| 276 | total_size += out_sz; |
| 277 | if (out_sz < current_read) { |
| 278 | current_read -= out_sz; |
| 279 | p += out_sz; |
| 280 | } else { |
Damien George | 05005f6 | 2015-01-21 22:48:37 +0000 | [diff] [blame] | 281 | p = vstr_extend(&vstr, DEFAULT_BUFFER_SIZE); |
Paul Sokolovsky | 425f952 | 2015-01-23 17:58:05 +0200 | [diff] [blame] | 282 | current_read = DEFAULT_BUFFER_SIZE; |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 283 | if (p == NULL) { |
| 284 | // TODO |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 285 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory")); |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } |
Damien George | 5fa93b6 | 2014-01-22 14:35:10 +0000 | [diff] [blame] | 289 | |
Damien George | 0b9ee86 | 2015-01-21 19:14:25 +0000 | [diff] [blame] | 290 | vstr.len = total_size; |
Damien George | 0b9ee86 | 2015-01-21 19:14:25 +0000 | [diff] [blame] | 291 | return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), &vstr); |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 294 | // Unbuffered, inefficient implementation of readline() for raw I/O files. |
Damien George | d00d8ac | 2014-10-24 11:26:12 +0000 | [diff] [blame] | 295 | STATIC mp_obj_t stream_unbuffered_readline(mp_uint_t n_args, const mp_obj_t *args) { |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 296 | struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0]; |
Damien George | 27e735f | 2014-04-05 23:02:23 +0100 | [diff] [blame] | 297 | if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) { |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 298 | // CPython: io.UnsupportedOperation, OSError subclass |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 299 | nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported")); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 302 | mp_int_t max_size = -1; |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 303 | if (n_args > 1) { |
| 304 | max_size = MP_OBJ_SMALL_INT_VALUE(args[1]); |
| 305 | } |
| 306 | |
Damien George | 0d3cb67 | 2015-01-28 23:43:01 +0000 | [diff] [blame] | 307 | vstr_t vstr; |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 308 | if (max_size != -1) { |
Damien George | 0d3cb67 | 2015-01-28 23:43:01 +0000 | [diff] [blame] | 309 | vstr_init(&vstr, max_size); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 310 | } else { |
Damien George | 0d3cb67 | 2015-01-28 23:43:01 +0000 | [diff] [blame] | 311 | vstr_init(&vstr, 16); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 314 | while (max_size == -1 || max_size-- != 0) { |
Damien George | 0d3cb67 | 2015-01-28 23:43:01 +0000 | [diff] [blame] | 315 | char *p = vstr_add_len(&vstr, 1); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 316 | if (p == NULL) { |
Damien George | d5f5b2f | 2014-05-03 22:01:32 +0100 | [diff] [blame] | 317 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_MemoryError, "out of memory")); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Damien George | d00d8ac | 2014-10-24 11:26:12 +0000 | [diff] [blame] | 320 | int error; |
Damien George | adf0f2a | 2014-07-27 22:38:58 +0100 | [diff] [blame] | 321 | mp_uint_t out_sz = o->type->stream_p->read(o, p, 1, &error); |
| 322 | if (out_sz == MP_STREAM_ERROR) { |
Paul Sokolovsky | 923a8a8 | 2014-10-16 12:22:52 +0300 | [diff] [blame] | 323 | if (is_nonblocking_error(error)) { |
Damien George | 0d3cb67 | 2015-01-28 23:43:01 +0000 | [diff] [blame] | 324 | if (vstr.len == 1) { |
Paul Sokolovsky | 923a8a8 | 2014-10-16 12:22:52 +0300 | [diff] [blame] | 325 | // We just incremented it, but otherwise we read nothing |
| 326 | // and immediately got EAGAIN. This is case is not well |
| 327 | // specified in |
| 328 | // https://docs.python.org/3/library/io.html#io.IOBase.readline |
| 329 | // unlike similar case for read(). But we follow the latter's |
| 330 | // behavior - return None. |
Damien George | 0d3cb67 | 2015-01-28 23:43:01 +0000 | [diff] [blame] | 331 | vstr_clear(&vstr); |
Paul Sokolovsky | 923a8a8 | 2014-10-16 12:22:52 +0300 | [diff] [blame] | 332 | return mp_const_none; |
| 333 | } else { |
| 334 | goto done; |
| 335 | } |
| 336 | } |
Paul Sokolovsky | 0c7b26c | 2014-10-16 02:56:24 +0300 | [diff] [blame] | 337 | nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error))); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 338 | } |
Paul Sokolovsky | 0914371 | 2014-01-22 10:34:45 +0200 | [diff] [blame] | 339 | if (out_sz == 0) { |
Paul Sokolovsky | 923a8a8 | 2014-10-16 12:22:52 +0300 | [diff] [blame] | 340 | done: |
Paul Sokolovsky | 0914371 | 2014-01-22 10:34:45 +0200 | [diff] [blame] | 341 | // Back out previously added byte |
Paul Sokolovsky | 0914371 | 2014-01-22 10:34:45 +0200 | [diff] [blame] | 342 | // Consider, what's better - read a char and get OutOfMemory (so read |
| 343 | // char is lost), or allocate first as we do. |
Damien George | 0d3cb67 | 2015-01-28 23:43:01 +0000 | [diff] [blame] | 344 | vstr_cut_tail_bytes(&vstr, 1); |
Paul Sokolovsky | 0914371 | 2014-01-22 10:34:45 +0200 | [diff] [blame] | 345 | break; |
| 346 | } |
| 347 | if (*p == '\n') { |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 348 | break; |
| 349 | } |
| 350 | } |
Damien George | 0d3cb67 | 2015-01-28 23:43:01 +0000 | [diff] [blame] | 351 | |
| 352 | return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), &vstr); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 353 | } |
| 354 | |
Damien George | d5f5b2f | 2014-05-03 22:01:32 +0100 | [diff] [blame] | 355 | // TODO take an optional extra argument (what does it do exactly?) |
| 356 | STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) { |
| 357 | mp_obj_t lines = mp_obj_new_list(0, NULL); |
| 358 | for (;;) { |
| 359 | mp_obj_t line = stream_unbuffered_readline(1, &self); |
Paul Sokolovsky | e22cddb | 2014-06-13 23:46:21 +0300 | [diff] [blame] | 360 | if (!mp_obj_is_true(line)) { |
Damien George | d5f5b2f | 2014-05-03 22:01:32 +0100 | [diff] [blame] | 361 | break; |
| 362 | } |
| 363 | mp_obj_list_append(lines, line); |
| 364 | } |
| 365 | return lines; |
| 366 | } |
| 367 | MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines); |
| 368 | |
Paul Sokolovsky | d54bef7 | 2014-01-20 18:35:32 +0200 | [diff] [blame] | 369 | mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) { |
| 370 | mp_obj_t l_in = stream_unbuffered_readline(1, &self); |
Paul Sokolovsky | e22cddb | 2014-06-13 23:46:21 +0300 | [diff] [blame] | 371 | if (mp_obj_is_true(l_in)) { |
Paul Sokolovsky | d54bef7 | 2014-01-20 18:35:32 +0200 | [diff] [blame] | 372 | return l_in; |
| 373 | } |
Damien George | ea8d06c | 2014-04-17 23:19:36 +0100 | [diff] [blame] | 374 | return MP_OBJ_STOP_ITERATION; |
Paul Sokolovsky | d54bef7 | 2014-01-20 18:35:32 +0200 | [diff] [blame] | 375 | } |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 376 | |
Paul Sokolovsky | 838eb1f | 2014-11-17 00:16:14 +0200 | [diff] [blame] | 377 | STATIC mp_obj_t stream_seek(mp_uint_t n_args, const mp_obj_t *args) { |
| 378 | struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0]; |
Damien George | 5694cc5 | 2014-11-16 23:56:37 +0000 | [diff] [blame] | 379 | if (o->type->stream_p == NULL || o->type->stream_p->ioctl == NULL) { |
Paul Sokolovsky | 838eb1f | 2014-11-17 00:16:14 +0200 | [diff] [blame] | 380 | // CPython: io.UnsupportedOperation, OSError subclass |
| 381 | nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported")); |
| 382 | } |
| 383 | |
| 384 | struct mp_stream_seek_t seek_s; |
| 385 | // TODO: Could be uint64 |
| 386 | seek_s.offset = mp_obj_get_int(args[1]); |
| 387 | seek_s.whence = 0; |
| 388 | if (n_args == 3) { |
| 389 | seek_s.whence = mp_obj_get_int(args[2]); |
| 390 | } |
| 391 | |
| 392 | int error; |
| 393 | mp_uint_t res = o->type->stream_p->ioctl(o, MP_STREAM_SEEK, (mp_uint_t)&seek_s, &error); |
| 394 | if (res == MP_STREAM_ERROR) { |
| 395 | nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error))); |
| 396 | } |
| 397 | |
| 398 | // TODO: Could be uint64 |
| 399 | return mp_obj_new_int_from_uint(seek_s.offset); |
| 400 | } |
| 401 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj, 2, 3, stream_seek); |
| 402 | |
Paul Sokolovsky | a671f89 | 2014-01-16 12:53:46 +0200 | [diff] [blame] | 403 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read); |
Paul Sokolovsky | e2f8d98 | 2014-10-23 21:22:08 +0300 | [diff] [blame] | 404 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto); |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 405 | MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 406 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline); |
Paul Sokolovsky | ac736f1 | 2014-07-13 23:04:17 +0300 | [diff] [blame] | 407 | MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write_method); |