blob: d3e563dbcf447a735ec664243e17c155e06e11bc [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +020027#include <assert.h>
28#include <string.h>
29
30#include "py/runtime.h"
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/builtin.h"
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +020032#include "py/stream.h"
Damien Georgeaf0932a2018-06-04 15:54:26 +100033#include "py/binary.h"
34#include "py/objarray.h"
Paul Sokolovskyd7da2db2017-05-03 01:47:08 +030035#include "py/objstringio.h"
36#include "py/frozenmod.h"
Paul Sokolovsky98a627d2014-04-03 14:57:53 +030037
Damien Georgeee3fd462014-05-24 23:03:12 +010038#if MICROPY_PY_IO
Paul Sokolovsky98a627d2014-04-03 14:57:53 +030039
Damien Georgeaf0932a2018-06-04 15:54:26 +100040#if MICROPY_PY_IO_IOBASE
41
Angus Grattondecf8e62024-02-27 15:32:29 +110042static const mp_obj_type_t mp_type_iobase;
Damien Georgeaf0932a2018-06-04 15:54:26 +100043
Angus Grattondecf8e62024-02-27 15:32:29 +110044static const mp_obj_base_t iobase_singleton = {&mp_type_iobase};
Damien Georgeaf0932a2018-06-04 15:54:26 +100045
Angus Grattondecf8e62024-02-27 15:32:29 +110046static mp_obj_t iobase_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien Georgeaf0932a2018-06-04 15:54:26 +100047 (void)type;
48 (void)n_args;
49 (void)n_kw;
50 (void)args;
51 return MP_OBJ_FROM_PTR(&iobase_singleton);
52}
53
Angus Grattondecf8e62024-02-27 15:32:29 +110054static mp_uint_t iobase_read_write(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode, qstr qst) {
Damien Georgeaf0932a2018-06-04 15:54:26 +100055 mp_obj_t dest[3];
56 mp_load_method(obj, qst, dest);
57 mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, size, buf};
58 dest[2] = MP_OBJ_FROM_PTR(&ar);
Damien Georgebd6ca152020-04-20 23:57:13 +100059 mp_obj_t ret_obj = mp_call_method_n_kw(1, 0, dest);
60 if (ret_obj == mp_const_none) {
Damien Georgeaf0932a2018-06-04 15:54:26 +100061 *errcode = MP_EAGAIN;
62 return MP_STREAM_ERROR;
Damien Georgebd6ca152020-04-20 23:57:13 +100063 }
64 mp_int_t ret = mp_obj_get_int(ret_obj);
65 if (ret >= 0) {
66 return ret;
Damien Georgeaf0932a2018-06-04 15:54:26 +100067 } else {
Damien Georgebd6ca152020-04-20 23:57:13 +100068 *errcode = -ret;
69 return MP_STREAM_ERROR;
Damien Georgeaf0932a2018-06-04 15:54:26 +100070 }
71}
Angus Grattondecf8e62024-02-27 15:32:29 +110072static mp_uint_t iobase_read(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode) {
Damien Georgeaf0932a2018-06-04 15:54:26 +100073 return iobase_read_write(obj, buf, size, errcode, MP_QSTR_readinto);
74}
75
Angus Grattondecf8e62024-02-27 15:32:29 +110076static mp_uint_t iobase_write(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode) {
Damien George69661f32020-02-27 15:36:53 +110077 return iobase_read_write(obj, (void *)buf, size, errcode, MP_QSTR_write);
Damien Georgeaf0932a2018-06-04 15:54:26 +100078}
79
Angus Grattondecf8e62024-02-27 15:32:29 +110080static mp_uint_t iobase_ioctl(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode) {
Damien Georgeaf0932a2018-06-04 15:54:26 +100081 mp_obj_t dest[4];
82 mp_load_method(obj, MP_QSTR_ioctl, dest);
83 dest[2] = mp_obj_new_int_from_uint(request);
84 dest[3] = mp_obj_new_int_from_uint(arg);
85 mp_int_t ret = mp_obj_get_int(mp_call_method_n_kw(2, 0, dest));
86 if (ret >= 0) {
87 return ret;
88 } else {
89 *errcode = -ret;
90 return MP_STREAM_ERROR;
91 }
92}
93
Angus Grattondecf8e62024-02-27 15:32:29 +110094static const mp_stream_p_t iobase_p = {
Damien Georgeaf0932a2018-06-04 15:54:26 +100095 .read = iobase_read,
96 .write = iobase_write,
97 .ioctl = iobase_ioctl,
98};
99
Angus Grattondecf8e62024-02-27 15:32:29 +1100100static MP_DEFINE_CONST_OBJ_TYPE(
Jim Mussared662b9762021-07-14 14:38:38 +1000101 mp_type_iobase,
102 MP_QSTR_IOBase,
103 MP_TYPE_FLAG_NONE,
Jim Mussared94beeab2022-09-17 00:31:23 +1000104 make_new, iobase_make_new,
Jim Mussared662b9762021-07-14 14:38:38 +1000105 protocol, &iobase_p
106 );
Damien Georgeaf0932a2018-06-04 15:54:26 +1000107
108#endif // MICROPY_PY_IO_IOBASE
109
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200110#if MICROPY_PY_IO_BUFFEREDWRITER
111typedef struct _mp_obj_bufwriter_t {
112 mp_obj_base_t base;
113 mp_obj_t stream;
114 size_t alloc;
115 size_t len;
116 byte buf[0];
117} mp_obj_bufwriter_t;
118
Angus Grattondecf8e62024-02-27 15:32:29 +1100119static mp_obj_t bufwriter_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200120 mp_arg_check_num(n_args, n_kw, 2, 2, false);
121 size_t alloc = mp_obj_get_int(args[1]);
Damien George24234932024-02-16 11:41:28 +1100122 mp_obj_bufwriter_t *o = mp_obj_malloc_var(mp_obj_bufwriter_t, buf, byte, alloc, type);
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200123 o->stream = args[0];
124 o->alloc = alloc;
125 o->len = 0;
126 return o;
127}
128
Angus Grattondecf8e62024-02-27 15:32:29 +1100129static mp_uint_t bufwriter_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200130 mp_obj_bufwriter_t *self = MP_OBJ_TO_PTR(self_in);
131
132 mp_uint_t org_size = size;
133
134 while (size > 0) {
135 mp_uint_t rem = self->alloc - self->len;
136 if (size < rem) {
137 memcpy(self->buf + self->len, buf, size);
138 self->len += size;
139 return org_size;
140 }
141
Paul Sokolovsky2c81b9b2016-03-25 14:59:30 +0200142 // Buffer flushing policy here is to flush entire buffer all the time.
143 // This allows e.g. to have a block device as backing storage and write
144 // entire block to it. memcpy below is not ideal and could be optimized
145 // in some cases. But the way it is now it at least ensures that buffer
146 // is word-aligned, to guard against obscure cases when it matters, e.g.
147 // https://github.com/micropython/micropython/issues/1863
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200148 memcpy(self->buf + self->len, buf, rem);
Damien George69661f32020-02-27 15:36:53 +1100149 buf = (byte *)buf + rem;
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200150 size -= rem;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300151 mp_uint_t out_sz = mp_stream_write_exactly(self->stream, self->buf, self->alloc, errcode);
Damien George4ee2c2a2019-02-25 14:52:36 +1100152 (void)out_sz;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300153 if (*errcode != 0) {
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200154 return MP_STREAM_ERROR;
155 }
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300156 // TODO: try to recover from a case of non-blocking stream, e.g. move
157 // remaining chunk to the beginning of buffer.
158 assert(out_sz == self->alloc);
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200159 self->len = 0;
160 }
161
162 return org_size;
163}
164
Angus Grattondecf8e62024-02-27 15:32:29 +1100165static mp_obj_t bufwriter_flush(mp_obj_t self_in) {
Paul Sokolovsky063e6e72016-03-25 14:33:38 +0200166 mp_obj_bufwriter_t *self = MP_OBJ_TO_PTR(self_in);
167
168 if (self->len != 0) {
169 int err;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300170 mp_uint_t out_sz = mp_stream_write_exactly(self->stream, self->buf, self->len, &err);
Damien George4ee2c2a2019-02-25 14:52:36 +1100171 (void)out_sz;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300172 // TODO: try to recover from a case of non-blocking stream, e.g. move
173 // remaining chunk to the beginning of buffer.
174 assert(out_sz == self->len);
Paul Sokolovsky063e6e72016-03-25 14:33:38 +0200175 self->len = 0;
Paul Sokolovsky7f7c84b2016-05-18 02:40:03 +0300176 if (err != 0) {
Damien George3a0a7712016-10-07 13:31:59 +1100177 mp_raise_OSError(err);
Paul Sokolovsky063e6e72016-03-25 14:33:38 +0200178 }
179 }
180
181 return mp_const_none;
182}
Angus Grattondecf8e62024-02-27 15:32:29 +1100183static MP_DEFINE_CONST_FUN_OBJ_1(bufwriter_flush_obj, bufwriter_flush);
Paul Sokolovsky063e6e72016-03-25 14:33:38 +0200184
Angus Grattondecf8e62024-02-27 15:32:29 +1100185static const mp_rom_map_elem_t bufwriter_locals_dict_table[] = {
Paul Sokolovsky45645042017-07-28 21:41:42 +0300186 { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
187 { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&bufwriter_flush_obj) },
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200188};
Angus Grattondecf8e62024-02-27 15:32:29 +1100189static MP_DEFINE_CONST_DICT(bufwriter_locals_dict, bufwriter_locals_dict_table);
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200190
Angus Grattondecf8e62024-02-27 15:32:29 +1100191static const mp_stream_p_t bufwriter_stream_p = {
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200192 .write = bufwriter_write,
193};
194
Angus Grattondecf8e62024-02-27 15:32:29 +1100195static MP_DEFINE_CONST_OBJ_TYPE(
Jim Mussared662b9762021-07-14 14:38:38 +1000196 mp_type_bufwriter,
197 MP_QSTR_BufferedWriter,
198 MP_TYPE_FLAG_NONE,
Jim Mussared94beeab2022-09-17 00:31:23 +1000199 make_new, bufwriter_make_new,
Jim Mussared662b9762021-07-14 14:38:38 +1000200 protocol, &bufwriter_stream_p,
Jim Mussared9dce8272022-06-24 16:27:46 +1000201 locals_dict, &bufwriter_locals_dict
Jim Mussared662b9762021-07-14 14:38:38 +1000202 );
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200203#endif // MICROPY_PY_IO_BUFFEREDWRITER
204
Angus Grattondecf8e62024-02-27 15:32:29 +1100205static const mp_rom_map_elem_t mp_module_io_globals_table[] = {
Jim Mussared30628d12022-08-17 16:18:31 +1000206 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_io) },
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300207 // Note: mp_builtin_open_obj should be defined by port, it's not
208 // part of the core.
Damien Georgecbf76742015-11-27 13:38:15 +0000209 { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
Damien Georgeaf0932a2018-06-04 15:54:26 +1000210 #if MICROPY_PY_IO_IOBASE
211 { MP_ROM_QSTR(MP_QSTR_IOBase), MP_ROM_PTR(&mp_type_iobase) },
212 #endif
Damien Georgecbf76742015-11-27 13:38:15 +0000213 { MP_ROM_QSTR(MP_QSTR_StringIO), MP_ROM_PTR(&mp_type_stringio) },
Damien Georgeee3fd462014-05-24 23:03:12 +0100214 #if MICROPY_PY_IO_BYTESIO
Damien Georgecbf76742015-11-27 13:38:15 +0000215 { MP_ROM_QSTR(MP_QSTR_BytesIO), MP_ROM_PTR(&mp_type_bytesio) },
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300216 #endif
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200217 #if MICROPY_PY_IO_BUFFEREDWRITER
Damien George9fef1c02021-03-26 13:48:34 +1100218 { MP_ROM_QSTR(MP_QSTR_BufferedWriter), MP_ROM_PTR(&mp_type_bufwriter) },
Paul Sokolovsky5d93dfb2016-03-25 01:10:49 +0200219 #endif
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300220};
221
Angus Grattondecf8e62024-02-27 15:32:29 +1100222static MP_DEFINE_CONST_DICT(mp_module_io_globals, mp_module_io_globals_table);
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300223
224const mp_obj_module_t mp_module_io = {
225 .base = { &mp_type_module },
Damien George69661f32020-02-27 15:36:53 +1100226 .globals = (mp_obj_dict_t *)&mp_module_io_globals,
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300227};
228
Jim Mussared2eba98f2023-06-02 12:33:25 +1000229MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_io, mp_module_io);
Jim Mussaredd8d3e6a2022-04-20 16:14:22 +1000230
Paul Sokolovsky98a627d2014-04-03 14:57:53 +0300231#endif