Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
Paul Sokolovsky | 8fea833 | 2019-01-31 11:55:21 +0300 | [diff] [blame] | 7 | * Copyright (c) 2014-2017 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 | |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 31 | #include "py/objstr.h" |
Damien George | e93c1ca | 2016-10-13 11:43:28 +1100 | [diff] [blame] | 32 | #include "py/objstringio.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 33 | #include "py/runtime.h" |
| 34 | #include "py/stream.h" |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 35 | |
Damien George | ee3fd46 | 2014-05-24 23:03:12 +0100 | [diff] [blame] | 36 | #if MICROPY_PY_IO |
Paul Sokolovsky | 100cd36 | 2014-04-26 20:59:39 +0300 | [diff] [blame] | 37 | |
stijn | bf19541 | 2015-01-16 13:36:18 +0100 | [diff] [blame] | 38 | #if MICROPY_CPYTHON_COMPAT |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 39 | static void check_stringio_is_open(const mp_obj_stringio_t *o) { |
stijn | bf19541 | 2015-01-16 13:36:18 +0100 | [diff] [blame] | 40 | if (o->vstr == NULL) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 41 | mp_raise_ValueError(MP_ERROR_TEXT("I/O operation on closed file")); |
stijn | bf19541 | 2015-01-16 13:36:18 +0100 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | #else |
| 45 | #define check_stringio_is_open(o) |
| 46 | #endif |
| 47 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 48 | static void stringio_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 49 | (void)kind; |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 50 | mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in); |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 51 | mp_printf(print, self->base.type == &mp_type_stringio ? "<io.StringIO 0x%x>" : "<io.BytesIO 0x%x>", self); |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 52 | } |
| 53 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 54 | static mp_uint_t stringio_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 55 | (void)errcode; |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 56 | mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in); |
stijn | bf19541 | 2015-01-16 13:36:18 +0100 | [diff] [blame] | 57 | check_stringio_is_open(o); |
Tom Collins | 53461de | 2017-05-12 13:30:12 -0700 | [diff] [blame] | 58 | if (o->vstr->len <= o->pos) { // read to EOF, or seeked to EOF or beyond |
| 59 | return 0; |
| 60 | } |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 61 | mp_uint_t remaining = o->vstr->len - o->pos; |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 62 | if (size > remaining) { |
| 63 | size = remaining; |
| 64 | } |
| 65 | memcpy(buf, o->vstr->buf + o->pos, size); |
| 66 | o->pos += size; |
| 67 | return size; |
| 68 | } |
| 69 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 70 | static void stringio_copy_on_write(mp_obj_stringio_t *o) { |
Paul Sokolovsky | 07241cd | 2017-06-05 23:54:21 +0300 | [diff] [blame] | 71 | const void *buf = o->vstr->buf; |
| 72 | o->vstr->buf = m_new(char, o->vstr->len); |
Paul Sokolovsky | 07241cd | 2017-06-05 23:54:21 +0300 | [diff] [blame] | 73 | o->vstr->fixed_buf = false; |
| 74 | o->ref_obj = MP_OBJ_NULL; |
Yonatan Goldschmidt | 4318a6d | 2019-11-25 17:21:54 +0200 | [diff] [blame] | 75 | memcpy(o->vstr->buf, buf, o->vstr->len); |
Paul Sokolovsky | 07241cd | 2017-06-05 23:54:21 +0300 | [diff] [blame] | 76 | } |
| 77 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 78 | static mp_uint_t stringio_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 79 | (void)errcode; |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 80 | mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in); |
stijn | bf19541 | 2015-01-16 13:36:18 +0100 | [diff] [blame] | 81 | check_stringio_is_open(o); |
Paul Sokolovsky | 07241cd | 2017-06-05 23:54:21 +0300 | [diff] [blame] | 82 | |
| 83 | if (o->vstr->fixed_buf) { |
| 84 | stringio_copy_on_write(o); |
| 85 | } |
| 86 | |
Tom Collins | e26fb3a | 2017-05-25 13:41:59 -0700 | [diff] [blame] | 87 | mp_uint_t new_pos = o->pos + size; |
| 88 | if (new_pos < size) { |
| 89 | // Writing <size> bytes will overflow o->pos beyond limit of mp_uint_t. |
| 90 | *errcode = MP_EFBIG; |
| 91 | return MP_STREAM_ERROR; |
| 92 | } |
Paul Sokolovsky | 3990b17 | 2016-07-28 01:53:44 +0300 | [diff] [blame] | 93 | mp_uint_t org_len = o->vstr->len; |
Tom Collins | e26fb3a | 2017-05-25 13:41:59 -0700 | [diff] [blame] | 94 | if (new_pos > o->vstr->alloc) { |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 95 | // Take all what's already allocated... |
| 96 | o->vstr->len = o->vstr->alloc; |
| 97 | // ... and add more |
Tom Collins | e26fb3a | 2017-05-25 13:41:59 -0700 | [diff] [blame] | 98 | vstr_add_len(o->vstr, new_pos - o->vstr->alloc); |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 99 | } |
Paul Sokolovsky | 3990b17 | 2016-07-28 01:53:44 +0300 | [diff] [blame] | 100 | // If there was a seek past EOF, clear the hole |
| 101 | if (o->pos > org_len) { |
| 102 | memset(o->vstr->buf + org_len, 0, o->pos - org_len); |
| 103 | } |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 104 | memcpy(o->vstr->buf + o->pos, buf, size); |
Tom Collins | e26fb3a | 2017-05-25 13:41:59 -0700 | [diff] [blame] | 105 | o->pos = new_pos; |
| 106 | if (new_pos > o->vstr->len) { |
| 107 | o->vstr->len = new_pos; |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 108 | } |
| 109 | return size; |
| 110 | } |
| 111 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 112 | static mp_uint_t stringio_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) { |
Paul Sokolovsky | f039ac5 | 2016-07-28 01:14:32 +0300 | [diff] [blame] | 113 | (void)errcode; |
Paul Sokolovsky | 3990b17 | 2016-07-28 01:53:44 +0300 | [diff] [blame] | 114 | mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in); |
Paul Sokolovsky | f039ac5 | 2016-07-28 01:14:32 +0300 | [diff] [blame] | 115 | switch (request) { |
Paul Sokolovsky | 3990b17 | 2016-07-28 01:53:44 +0300 | [diff] [blame] | 116 | case MP_STREAM_SEEK: { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 117 | struct mp_stream_seek_t *s = (struct mp_stream_seek_t *)arg; |
Paul Sokolovsky | 3990b17 | 2016-07-28 01:53:44 +0300 | [diff] [blame] | 118 | mp_uint_t ref = 0; |
| 119 | switch (s->whence) { |
Paul Sokolovsky | 0cd9ab7 | 2017-08-20 21:32:17 +0300 | [diff] [blame] | 120 | case MP_SEEK_CUR: |
Paul Sokolovsky | 3990b17 | 2016-07-28 01:53:44 +0300 | [diff] [blame] | 121 | ref = o->pos; |
| 122 | break; |
Paul Sokolovsky | 0cd9ab7 | 2017-08-20 21:32:17 +0300 | [diff] [blame] | 123 | case MP_SEEK_END: |
Paul Sokolovsky | 3990b17 | 2016-07-28 01:53:44 +0300 | [diff] [blame] | 124 | ref = o->vstr->len; |
| 125 | break; |
| 126 | } |
Tom Collins | 168350c | 2017-05-25 13:53:49 -0700 | [diff] [blame] | 127 | mp_uint_t new_pos = ref + s->offset; |
Paul Sokolovsky | 0cd9ab7 | 2017-08-20 21:32:17 +0300 | [diff] [blame] | 128 | |
| 129 | // For MP_SEEK_SET, offset is unsigned |
| 130 | if (s->whence != MP_SEEK_SET && s->offset < 0) { |
Tom Collins | 168350c | 2017-05-25 13:53:49 -0700 | [diff] [blame] | 131 | if (new_pos > ref) { |
| 132 | // Negative offset from SEEK_CUR or SEEK_END went past 0. |
| 133 | // CPython sets position to 0, POSIX returns an EINVAL error |
| 134 | new_pos = 0; |
| 135 | } |
| 136 | } else if (new_pos < ref) { |
| 137 | // positive offset went beyond the limit of mp_uint_t |
| 138 | *errcode = MP_EINVAL; // replace with MP_EOVERFLOW when defined |
| 139 | return MP_STREAM_ERROR; |
| 140 | } |
| 141 | s->offset = o->pos = new_pos; |
Paul Sokolovsky | 3990b17 | 2016-07-28 01:53:44 +0300 | [diff] [blame] | 142 | return 0; |
| 143 | } |
Paul Sokolovsky | f039ac5 | 2016-07-28 01:14:32 +0300 | [diff] [blame] | 144 | case MP_STREAM_FLUSH: |
| 145 | return 0; |
Damien George | cf31d38 | 2018-03-07 17:48:53 +1100 | [diff] [blame] | 146 | case MP_STREAM_CLOSE: |
| 147 | #if MICROPY_CPYTHON_COMPAT |
| 148 | vstr_free(o->vstr); |
| 149 | o->vstr = NULL; |
| 150 | #else |
| 151 | vstr_clear(o->vstr); |
| 152 | o->vstr->alloc = 0; |
| 153 | o->vstr->len = 0; |
| 154 | o->pos = 0; |
| 155 | #endif |
| 156 | return 0; |
Paul Sokolovsky | f039ac5 | 2016-07-28 01:14:32 +0300 | [diff] [blame] | 157 | default: |
| 158 | *errcode = MP_EINVAL; |
| 159 | return MP_STREAM_ERROR; |
| 160 | } |
| 161 | } |
| 162 | |
Paul Sokolovsky | a47b64a | 2014-05-15 07:28:19 +0300 | [diff] [blame] | 163 | #define STREAM_TO_CONTENT_TYPE(o) (((o)->base.type == &mp_type_stringio) ? &mp_type_str : &mp_type_bytes) |
| 164 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 165 | static mp_obj_t stringio_getvalue(mp_obj_t self_in) { |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 166 | mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in); |
stijn | bf19541 | 2015-01-16 13:36:18 +0100 | [diff] [blame] | 167 | check_stringio_is_open(self); |
Paul Sokolovsky | 2ae6697 | 2016-05-13 01:35:52 +0300 | [diff] [blame] | 168 | // TODO: Try to avoid copying string |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 169 | return mp_obj_new_str_of_type(STREAM_TO_CONTENT_TYPE(self), (byte *)self->vstr->buf, self->vstr->len); |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 170 | } |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 171 | static MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue); |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 172 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 173 | static mp_obj_stringio_t *stringio_new(const mp_obj_type_t *type) { |
Jim Mussared | 0e7bfc8 | 2022-04-22 17:09:15 +1000 | [diff] [blame] | 174 | mp_obj_stringio_t *o = mp_obj_malloc(mp_obj_stringio_t, type); |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 175 | o->pos = 0; |
Paul Sokolovsky | 07241cd | 2017-06-05 23:54:21 +0300 | [diff] [blame] | 176 | o->ref_obj = MP_OBJ_NULL; |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 177 | return o; |
| 178 | } |
| 179 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 180 | static mp_obj_t stringio_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 181 | (void)n_kw; // TODO check n_kw==0 |
Paul Sokolovsky | 50d3a9d | 2017-02-02 00:33:43 +0300 | [diff] [blame] | 182 | |
| 183 | mp_uint_t sz = 16; |
| 184 | bool initdata = false; |
| 185 | mp_buffer_info_t bufinfo; |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 186 | |
Paul Sokolovsky | 07241cd | 2017-06-05 23:54:21 +0300 | [diff] [blame] | 187 | mp_obj_stringio_t *o = stringio_new(type_in); |
| 188 | |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 189 | if (n_args > 0) { |
Damien George | eee1e88 | 2019-01-30 18:49:52 +1100 | [diff] [blame] | 190 | if (mp_obj_is_int(args[0])) { |
Paul Sokolovsky | 50d3a9d | 2017-02-02 00:33:43 +0300 | [diff] [blame] | 191 | sz = mp_obj_get_int(args[0]); |
| 192 | } else { |
| 193 | mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); |
Paul Sokolovsky | 07241cd | 2017-06-05 23:54:21 +0300 | [diff] [blame] | 194 | |
Damien George | eee1e88 | 2019-01-30 18:49:52 +1100 | [diff] [blame] | 195 | if (mp_obj_is_str_or_bytes(args[0])) { |
Paul Sokolovsky | 07241cd | 2017-06-05 23:54:21 +0300 | [diff] [blame] | 196 | o->vstr = m_new_obj(vstr_t); |
| 197 | vstr_init_fixed_buf(o->vstr, bufinfo.len, bufinfo.buf); |
| 198 | o->vstr->len = bufinfo.len; |
| 199 | o->ref_obj = args[0]; |
| 200 | return MP_OBJ_FROM_PTR(o); |
| 201 | } |
| 202 | |
Paul Sokolovsky | 50d3a9d | 2017-02-02 00:33:43 +0300 | [diff] [blame] | 203 | sz = bufinfo.len; |
| 204 | initdata = true; |
| 205 | } |
| 206 | } |
| 207 | |
Paul Sokolovsky | 07241cd | 2017-06-05 23:54:21 +0300 | [diff] [blame] | 208 | o->vstr = vstr_new(sz); |
Paul Sokolovsky | 50d3a9d | 2017-02-02 00:33:43 +0300 | [diff] [blame] | 209 | |
| 210 | if (initdata) { |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 211 | stringio_write(MP_OBJ_FROM_PTR(o), bufinfo.buf, bufinfo.len, NULL); |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 212 | // Cur ptr is always at the beginning of buffer at the construction |
| 213 | o->pos = 0; |
| 214 | } |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 215 | return MP_OBJ_FROM_PTR(o); |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 216 | } |
| 217 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 218 | static const mp_rom_map_elem_t stringio_locals_dict_table[] = { |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 219 | { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, |
Paul Sokolovsky | d22a04d | 2016-10-09 11:56:11 +0300 | [diff] [blame] | 220 | { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 221 | { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, |
| 222 | { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, |
Paul Sokolovsky | 3990b17 | 2016-07-28 01:53:44 +0300 | [diff] [blame] | 223 | { MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) }, |
Andrew Leech | ed93778 | 2020-03-10 15:14:35 +1100 | [diff] [blame] | 224 | { MP_ROM_QSTR(MP_QSTR_tell), MP_ROM_PTR(&mp_stream_tell_obj) }, |
Paul Sokolovsky | f039ac5 | 2016-07-28 01:14:32 +0300 | [diff] [blame] | 225 | { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) }, |
Damien George | cf31d38 | 2018-03-07 17:48:53 +1100 | [diff] [blame] | 226 | { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) }, |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 227 | { MP_ROM_QSTR(MP_QSTR_getvalue), MP_ROM_PTR(&stringio_getvalue_obj) }, |
| 228 | { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) }, |
Jim Mussared | 198311c | 2023-06-26 23:53:12 +1000 | [diff] [blame] | 229 | { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&mp_stream___exit___obj) }, |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 230 | }; |
| 231 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 232 | static MP_DEFINE_CONST_DICT(stringio_locals_dict, stringio_locals_dict_table); |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 233 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 234 | static const mp_stream_p_t stringio_stream_p = { |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 235 | .read = stringio_read, |
| 236 | .write = stringio_write, |
Paul Sokolovsky | f039ac5 | 2016-07-28 01:14:32 +0300 | [diff] [blame] | 237 | .ioctl = stringio_ioctl, |
Damien George | adf0f2a | 2014-07-27 22:38:58 +0100 | [diff] [blame] | 238 | .is_text = true, |
Paul Sokolovsky | cb9dc08 | 2014-04-26 20:26:14 +0300 | [diff] [blame] | 239 | }; |
| 240 | |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 241 | MP_DEFINE_CONST_OBJ_TYPE( |
| 242 | mp_type_stringio, |
| 243 | MP_QSTR_StringIO, |
Jim Mussared | 6da41b5 | 2022-09-16 23:57:38 +1000 | [diff] [blame] | 244 | MP_TYPE_FLAG_ITER_IS_STREAM, |
Jim Mussared | 94beeab | 2022-09-17 00:31:23 +1000 | [diff] [blame] | 245 | make_new, stringio_make_new, |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 246 | print, stringio_print, |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 247 | protocol, &stringio_stream_p, |
Jim Mussared | 9dce827 | 2022-06-24 16:27:46 +1000 | [diff] [blame] | 248 | locals_dict, &stringio_locals_dict |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 249 | ); |
Paul Sokolovsky | 100cd36 | 2014-04-26 20:59:39 +0300 | [diff] [blame] | 250 | |
Damien George | ee3fd46 | 2014-05-24 23:03:12 +0100 | [diff] [blame] | 251 | #if MICROPY_PY_IO_BYTESIO |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 252 | static const mp_stream_p_t bytesio_stream_p = { |
Paul m. p. P | 3b3a474 | 2019-07-16 06:00:36 +0200 | [diff] [blame] | 253 | .read = stringio_read, |
| 254 | .write = stringio_write, |
| 255 | .ioctl = stringio_ioctl, |
| 256 | }; |
| 257 | |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 258 | MP_DEFINE_CONST_OBJ_TYPE( |
| 259 | mp_type_bytesio, |
| 260 | MP_QSTR_BytesIO, |
Jim Mussared | 6da41b5 | 2022-09-16 23:57:38 +1000 | [diff] [blame] | 261 | MP_TYPE_FLAG_ITER_IS_STREAM, |
Jim Mussared | 94beeab | 2022-09-17 00:31:23 +1000 | [diff] [blame] | 262 | make_new, stringio_make_new, |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 263 | print, stringio_print, |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 264 | protocol, &bytesio_stream_p, |
Jim Mussared | 9dce827 | 2022-06-24 16:27:46 +1000 | [diff] [blame] | 265 | locals_dict, &stringio_locals_dict |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 266 | ); |
Paul Sokolovsky | a47b64a | 2014-05-15 07:28:19 +0300 | [diff] [blame] | 267 | #endif |
| 268 | |
Paul Sokolovsky | 100cd36 | 2014-04-26 20:59:39 +0300 | [diff] [blame] | 269 | #endif |