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 | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 36 | #include "stream.h" |
Paul Sokolovsky | 0ef015b | 2014-05-07 02:23:46 +0300 | [diff] [blame] | 37 | #if MICROPY_STREAMS_NON_BLOCK |
| 38 | #include <errno.h> |
| 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 | |
Paul Sokolovsky | a47b64a | 2014-05-15 07:28:19 +0300 | [diff] [blame] | 57 | #define STREAM_CONTENT_TYPE(stream) (((stream)->is_bytes) ? &mp_type_bytes : &mp_type_str) |
| 58 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 59 | 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] | 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 | |
Paul Sokolovsky | a671f89 | 2014-01-16 12:53:46 +0200 | [diff] [blame] | 66 | machine_int_t sz; |
| 67 | if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) { |
| 68 | return stream_readall(args[0]); |
| 69 | } |
Damien George | 5fa93b6 | 2014-01-22 14:35:10 +0000 | [diff] [blame] | 70 | byte *buf = m_new(byte, sz); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 71 | int error; |
Damien George | 27e735f | 2014-04-05 23:02:23 +0100 | [diff] [blame] | 72 | machine_int_t out_sz = o->type->stream_p->read(o, buf, sz, &error); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 73 | if (out_sz == -1) { |
Paul Sokolovsky | a592104 | 2014-05-07 01:39:38 +0300 | [diff] [blame] | 74 | if (is_nonblocking_error(error)) { |
| 75 | // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read |
| 76 | // "If the object is in non-blocking mode and no bytes are available, |
| 77 | // None is returned." |
| 78 | // This is actually very weird, as naive truth check will treat |
| 79 | // this as EOF. |
| 80 | return mp_const_none; |
| 81 | } |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 82 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error)); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 83 | } else { |
Damien George | f600a6a | 2014-05-25 22:34:34 +0100 | [diff] [blame] | 84 | mp_obj_t s = mp_obj_new_str_of_type(STREAM_CONTENT_TYPE(o->type->stream_p), buf, out_sz); // will reallocate to use exact size |
Damien George | 5fa93b6 | 2014-01-22 14:35:10 +0000 | [diff] [blame] | 85 | m_free(buf, sz); |
| 86 | return s; |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 90 | STATIC mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) { |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 91 | 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] | 92 | if (o->type->stream_p == NULL || o->type->stream_p->write == NULL) { |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 93 | // CPython: io.UnsupportedOperation, OSError subclass |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 94 | 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] | 95 | } |
| 96 | |
Paul Sokolovsky | 45fb143 | 2014-04-26 05:46:06 +0300 | [diff] [blame] | 97 | mp_buffer_info_t bufinfo; |
| 98 | mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); |
| 99 | |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 100 | int error; |
Paul Sokolovsky | 45fb143 | 2014-04-26 05:46:06 +0300 | [diff] [blame] | 101 | machine_int_t out_sz = o->type->stream_p->write(self_in, bufinfo.buf, bufinfo.len, &error); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 102 | if (out_sz == -1) { |
Paul Sokolovsky | a592104 | 2014-05-07 01:39:38 +0300 | [diff] [blame] | 103 | if (is_nonblocking_error(error)) { |
| 104 | // http://docs.python.org/3/library/io.html#io.RawIOBase.write |
| 105 | // "None is returned if the raw stream is set not to block and |
| 106 | // no single byte could be readily written to it." |
| 107 | // This is for consistency with read() behavior, still weird, |
| 108 | // see abobe. |
| 109 | return mp_const_none; |
| 110 | } |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 111 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error)); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 112 | } else { |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 113 | return MP_OBJ_NEW_SMALL_INT(out_sz); |
| 114 | } |
| 115 | } |
| 116 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 117 | STATIC mp_obj_t stream_readall(mp_obj_t self_in) { |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 118 | 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] | 119 | if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) { |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 120 | // CPython: io.UnsupportedOperation, OSError subclass |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 121 | 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] | 122 | } |
| 123 | |
| 124 | int total_size = 0; |
Paul Sokolovsky | b9be45e | 2014-05-07 01:51:07 +0300 | [diff] [blame] | 125 | vstr_t *vstr = vstr_new_size(DEFAULT_BUFFER_SIZE); |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 126 | char *buf = vstr_str(vstr); |
| 127 | char *p = buf; |
| 128 | int error; |
Paul Sokolovsky | b9be45e | 2014-05-07 01:51:07 +0300 | [diff] [blame] | 129 | int current_read = DEFAULT_BUFFER_SIZE; |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 130 | while (true) { |
Damien George | 27e735f | 2014-04-05 23:02:23 +0100 | [diff] [blame] | 131 | machine_int_t out_sz = o->type->stream_p->read(self_in, p, current_read, &error); |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 132 | if (out_sz == -1) { |
Paul Sokolovsky | 6e73143 | 2014-05-07 01:48:12 +0300 | [diff] [blame] | 133 | if (is_nonblocking_error(error)) { |
| 134 | // With non-blocking streams, we read as much as we can. |
| 135 | // If we read nothing, return None, just like read(). |
| 136 | // Otherwise, return data read so far. |
| 137 | if (total_size == 0) { |
| 138 | return mp_const_none; |
| 139 | } |
| 140 | break; |
| 141 | } |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 142 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error)); |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 143 | } |
| 144 | if (out_sz == 0) { |
| 145 | break; |
| 146 | } |
| 147 | total_size += out_sz; |
| 148 | if (out_sz < current_read) { |
| 149 | current_read -= out_sz; |
| 150 | p += out_sz; |
| 151 | } else { |
Paul Sokolovsky | b9be45e | 2014-05-07 01:51:07 +0300 | [diff] [blame] | 152 | current_read = DEFAULT_BUFFER_SIZE; |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 153 | p = vstr_extend(vstr, current_read); |
| 154 | if (p == NULL) { |
| 155 | // TODO |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 156 | 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] | 157 | } |
| 158 | } |
| 159 | } |
Damien George | 5fa93b6 | 2014-01-22 14:35:10 +0000 | [diff] [blame] | 160 | |
Damien George | f600a6a | 2014-05-25 22:34:34 +0100 | [diff] [blame] | 161 | 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] | 162 | vstr_free(vstr); |
| 163 | return s; |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 166 | // Unbuffered, inefficient implementation of readline() for raw I/O files. |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 167 | 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] | 168 | 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] | 169 | if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) { |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 170 | // CPython: io.UnsupportedOperation, OSError subclass |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 171 | 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] | 172 | } |
| 173 | |
| 174 | machine_int_t max_size = -1; |
| 175 | if (n_args > 1) { |
| 176 | max_size = MP_OBJ_SMALL_INT_VALUE(args[1]); |
| 177 | } |
| 178 | |
| 179 | vstr_t *vstr; |
| 180 | if (max_size != -1) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 181 | vstr = vstr_new_size(max_size); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 182 | } else { |
| 183 | vstr = vstr_new(); |
| 184 | } |
| 185 | |
| 186 | int error; |
| 187 | while (max_size == -1 || max_size-- != 0) { |
| 188 | char *p = vstr_add_len(vstr, 1); |
| 189 | if (p == NULL) { |
Damien George | d5f5b2f | 2014-05-03 22:01:32 +0100 | [diff] [blame] | 190 | 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] | 191 | } |
| 192 | |
Damien George | 27e735f | 2014-04-05 23:02:23 +0100 | [diff] [blame] | 193 | machine_int_t out_sz = o->type->stream_p->read(o, p, 1, &error); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 194 | if (out_sz == -1) { |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 195 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error)); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 196 | } |
Paul Sokolovsky | 0914371 | 2014-01-22 10:34:45 +0200 | [diff] [blame] | 197 | if (out_sz == 0) { |
| 198 | // Back out previously added byte |
| 199 | // TODO: This is a bit hacky, does it supported by vstr API contract? |
| 200 | // Consider, what's better - read a char and get OutOfMemory (so read |
| 201 | // char is lost), or allocate first as we do. |
| 202 | vstr_add_len(vstr, -1); |
| 203 | break; |
| 204 | } |
| 205 | if (*p == '\n') { |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 206 | break; |
| 207 | } |
| 208 | } |
Damien George | d5f5b2f | 2014-05-03 22:01:32 +0100 | [diff] [blame] | 209 | // 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] | 210 | 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] | 211 | vstr_free(vstr); |
| 212 | return ret; |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Damien George | d5f5b2f | 2014-05-03 22:01:32 +0100 | [diff] [blame] | 215 | // TODO take an optional extra argument (what does it do exactly?) |
| 216 | STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) { |
| 217 | mp_obj_t lines = mp_obj_new_list(0, NULL); |
| 218 | for (;;) { |
| 219 | mp_obj_t line = stream_unbuffered_readline(1, &self); |
Paul Sokolovsky | e22cddb | 2014-06-13 23:46:21 +0300 | [diff] [blame^] | 220 | if (!mp_obj_is_true(line)) { |
Damien George | d5f5b2f | 2014-05-03 22:01:32 +0100 | [diff] [blame] | 221 | break; |
| 222 | } |
| 223 | mp_obj_list_append(lines, line); |
| 224 | } |
| 225 | return lines; |
| 226 | } |
| 227 | MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines); |
| 228 | |
Paul Sokolovsky | d54bef7 | 2014-01-20 18:35:32 +0200 | [diff] [blame] | 229 | mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) { |
| 230 | mp_obj_t l_in = stream_unbuffered_readline(1, &self); |
Paul Sokolovsky | e22cddb | 2014-06-13 23:46:21 +0300 | [diff] [blame^] | 231 | if (mp_obj_is_true(l_in)) { |
Paul Sokolovsky | d54bef7 | 2014-01-20 18:35:32 +0200 | [diff] [blame] | 232 | return l_in; |
| 233 | } |
Damien George | ea8d06c | 2014-04-17 23:19:36 +0100 | [diff] [blame] | 234 | return MP_OBJ_STOP_ITERATION; |
Paul Sokolovsky | d54bef7 | 2014-01-20 18:35:32 +0200 | [diff] [blame] | 235 | } |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 236 | |
Paul Sokolovsky | a671f89 | 2014-01-16 12:53:46 +0200 | [diff] [blame] | 237 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read); |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 238 | MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 239 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 240 | MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write); |