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