2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2021-04-22 20:55:39 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2014-2017 Paul Sokolovsky
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-04-26 13:26:14 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/objstr.h"
|
2016-10-12 20:43:28 -04:00
|
|
|
#include "py/objstringio.h"
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stream.h"
|
2014-04-26 13:26:14 -04:00
|
|
|
|
2018-08-08 21:24:49 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
|
|
|
|
2014-05-24 18:03:12 -04:00
|
|
|
#if MICROPY_PY_IO
|
2014-04-26 13:59:39 -04:00
|
|
|
|
2015-01-16 07:36:18 -05:00
|
|
|
#if MICROPY_CPYTHON_COMPAT
|
|
|
|
STATIC void check_stringio_is_open(const mp_obj_stringio_t *o) {
|
|
|
|
if (o->vstr == NULL) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("I/O operation on closed file"));
|
2015-01-16 07:36:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define check_stringio_is_open(o)
|
|
|
|
#endif
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void stringio_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_printf(print, self->base.type == &mp_type_stringio ? "<io.StringIO 0x%x>" : "<io.BytesIO 0x%x>", self);
|
2014-04-26 13:26:14 -04:00
|
|
|
}
|
|
|
|
|
2014-07-27 17:38:58 -04:00
|
|
|
STATIC mp_uint_t stringio_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)errcode;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
|
2015-01-16 07:36:18 -05:00
|
|
|
check_stringio_is_open(o);
|
2017-05-12 16:30:12 -04:00
|
|
|
if (o->vstr->len <= o->pos) { // read to EOF, or seeked to EOF or beyond
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_uint_t remaining = o->vstr->len - o->pos;
|
2014-04-26 13:26:14 -04:00
|
|
|
if (size > remaining) {
|
|
|
|
size = remaining;
|
|
|
|
}
|
|
|
|
memcpy(buf, o->vstr->buf + o->pos, size);
|
|
|
|
o->pos += size;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-06-05 16:54:21 -04:00
|
|
|
STATIC void stringio_copy_on_write(mp_obj_stringio_t *o) {
|
|
|
|
const void *buf = o->vstr->buf;
|
|
|
|
o->vstr->buf = m_new(char, o->vstr->len);
|
|
|
|
o->vstr->fixed_buf = false;
|
|
|
|
o->ref_obj = MP_OBJ_NULL;
|
2021-04-23 15:26:42 -04:00
|
|
|
memcpy(o->vstr->buf, buf, o->vstr->len);
|
2017-06-05 16:54:21 -04:00
|
|
|
}
|
|
|
|
|
2014-07-27 17:38:58 -04:00
|
|
|
STATIC mp_uint_t stringio_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)errcode;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
|
2015-01-16 07:36:18 -05:00
|
|
|
check_stringio_is_open(o);
|
2017-06-05 16:54:21 -04:00
|
|
|
|
|
|
|
if (o->vstr->fixed_buf) {
|
|
|
|
stringio_copy_on_write(o);
|
|
|
|
}
|
|
|
|
|
2017-05-25 16:41:59 -04:00
|
|
|
mp_uint_t new_pos = o->pos + size;
|
|
|
|
if (new_pos < size) {
|
|
|
|
// Writing <size> bytes will overflow o->pos beyond limit of mp_uint_t.
|
|
|
|
*errcode = MP_EFBIG;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
2016-07-27 18:53:44 -04:00
|
|
|
mp_uint_t org_len = o->vstr->len;
|
2017-05-25 16:41:59 -04:00
|
|
|
if (new_pos > o->vstr->alloc) {
|
2014-04-26 13:26:14 -04:00
|
|
|
// Take all what's already allocated...
|
|
|
|
o->vstr->len = o->vstr->alloc;
|
|
|
|
// ... and add more
|
2017-05-25 16:41:59 -04:00
|
|
|
vstr_add_len(o->vstr, new_pos - o->vstr->alloc);
|
2014-04-26 13:26:14 -04:00
|
|
|
}
|
2016-07-27 18:53:44 -04:00
|
|
|
// If there was a seek past EOF, clear the hole
|
|
|
|
if (o->pos > org_len) {
|
|
|
|
memset(o->vstr->buf + org_len, 0, o->pos - org_len);
|
|
|
|
}
|
2014-04-26 13:26:14 -04:00
|
|
|
memcpy(o->vstr->buf + o->pos, buf, size);
|
2017-05-25 16:41:59 -04:00
|
|
|
o->pos = new_pos;
|
|
|
|
if (new_pos > o->vstr->len) {
|
|
|
|
o->vstr->len = new_pos;
|
2014-04-26 13:26:14 -04:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-07-27 18:14:32 -04:00
|
|
|
STATIC mp_uint_t stringio_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
|
|
|
|
(void)errcode;
|
2016-07-27 18:53:44 -04:00
|
|
|
mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
|
2016-07-27 18:14:32 -04:00
|
|
|
switch (request) {
|
2016-07-27 18:53:44 -04:00
|
|
|
case MP_STREAM_SEEK: {
|
2021-03-15 09:57:36 -04:00
|
|
|
struct mp_stream_seek_t *s = (struct mp_stream_seek_t *)arg;
|
2016-07-27 18:53:44 -04:00
|
|
|
mp_uint_t ref = 0;
|
|
|
|
switch (s->whence) {
|
2017-08-20 14:32:17 -04:00
|
|
|
case MP_SEEK_CUR:
|
2016-07-27 18:53:44 -04:00
|
|
|
ref = o->pos;
|
|
|
|
break;
|
2017-08-20 14:32:17 -04:00
|
|
|
case MP_SEEK_END:
|
2016-07-27 18:53:44 -04:00
|
|
|
ref = o->vstr->len;
|
|
|
|
break;
|
|
|
|
}
|
2017-05-25 16:53:49 -04:00
|
|
|
mp_uint_t new_pos = ref + s->offset;
|
2017-08-20 14:32:17 -04:00
|
|
|
|
|
|
|
// For MP_SEEK_SET, offset is unsigned
|
|
|
|
if (s->whence != MP_SEEK_SET && s->offset < 0) {
|
2017-05-25 16:53:49 -04:00
|
|
|
if (new_pos > ref) {
|
|
|
|
// Negative offset from SEEK_CUR or SEEK_END went past 0.
|
|
|
|
// CPython sets position to 0, POSIX returns an EINVAL error
|
|
|
|
new_pos = 0;
|
|
|
|
}
|
|
|
|
} else if (new_pos < ref) {
|
|
|
|
// positive offset went beyond the limit of mp_uint_t
|
|
|
|
*errcode = MP_EINVAL; // replace with MP_EOVERFLOW when defined
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
s->offset = o->pos = new_pos;
|
2016-07-27 18:53:44 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2016-07-27 18:14:32 -04:00
|
|
|
case MP_STREAM_FLUSH:
|
|
|
|
return 0;
|
2018-03-07 01:48:53 -05:00
|
|
|
case MP_STREAM_CLOSE:
|
|
|
|
#if MICROPY_CPYTHON_COMPAT
|
|
|
|
vstr_free(o->vstr);
|
|
|
|
o->vstr = NULL;
|
|
|
|
#else
|
|
|
|
vstr_clear(o->vstr);
|
|
|
|
o->vstr->alloc = 0;
|
|
|
|
o->vstr->len = 0;
|
|
|
|
o->pos = 0;
|
|
|
|
#endif
|
|
|
|
return 0;
|
2016-07-27 18:14:32 -04:00
|
|
|
default:
|
|
|
|
*errcode = MP_EINVAL;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 00:28:19 -04:00
|
|
|
#define STREAM_TO_CONTENT_TYPE(o) (((o)->base.type == &mp_type_stringio) ? &mp_type_str : &mp_type_bytes)
|
|
|
|
|
2014-04-26 13:26:14 -04:00
|
|
|
STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-01-16 07:36:18 -05:00
|
|
|
check_stringio_is_open(self);
|
2016-05-12 18:35:52 -04:00
|
|
|
// TODO: Try to avoid copying string
|
2021-03-15 09:57:36 -04:00
|
|
|
return mp_obj_new_str_of_type(STREAM_TO_CONTENT_TYPE(self), (byte *)self->vstr->buf, self->vstr->len);
|
2014-04-26 13:26:14 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue);
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t stringio___exit__(size_t n_args, const mp_obj_t *args) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)n_args;
|
2018-03-07 01:48:53 -05:00
|
|
|
return mp_stream_close(args[0]);
|
2014-04-26 13:26:14 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio___exit__);
|
|
|
|
|
2017-06-05 16:54:21 -04:00
|
|
|
STATIC mp_obj_stringio_t *stringio_new(const mp_obj_type_t *type) {
|
2014-04-26 13:26:14 -04:00
|
|
|
mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t);
|
2016-01-03 10:55:55 -05:00
|
|
|
o->base.type = type;
|
2014-04-26 13:26:14 -04:00
|
|
|
o->pos = 0;
|
2017-06-05 16:54:21 -04:00
|
|
|
o->ref_obj = MP_OBJ_NULL;
|
2014-04-26 13:26:14 -04:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:14:40 -05:00
|
|
|
STATIC mp_obj_t stringio_make_new(const mp_obj_type_t *type_in, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
|
|
|
(void)kw_args; // TODO check kw_args->used == 0
|
2017-02-01 16:33:43 -05:00
|
|
|
|
|
|
|
mp_uint_t sz = 16;
|
|
|
|
bool initdata = false;
|
|
|
|
mp_buffer_info_t bufinfo;
|
2014-04-26 13:26:14 -04:00
|
|
|
|
2017-06-05 16:54:21 -04:00
|
|
|
mp_obj_stringio_t *o = stringio_new(type_in);
|
|
|
|
|
2014-04-26 13:26:14 -04:00
|
|
|
if (n_args > 0) {
|
2021-04-20 21:04:23 -04:00
|
|
|
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
|
2017-06-05 16:54:21 -04:00
|
|
|
|
2021-04-22 20:55:39 -04:00
|
|
|
if (mp_obj_is_str_or_bytes(args[0])) {
|
2021-04-20 21:04:23 -04:00
|
|
|
o->vstr = m_new_obj(vstr_t);
|
|
|
|
vstr_init_fixed_buf(o->vstr, bufinfo.len, bufinfo.buf);
|
|
|
|
o->vstr->len = bufinfo.len;
|
|
|
|
o->ref_obj = args[0];
|
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2017-02-01 16:33:43 -05:00
|
|
|
}
|
2021-04-20 21:04:23 -04:00
|
|
|
|
|
|
|
sz = bufinfo.len;
|
|
|
|
initdata = true;
|
2017-02-01 16:33:43 -05:00
|
|
|
}
|
|
|
|
|
2017-06-05 16:54:21 -04:00
|
|
|
o->vstr = vstr_new(sz);
|
2017-02-01 16:33:43 -05:00
|
|
|
|
|
|
|
if (initdata) {
|
2015-11-27 12:01:44 -05:00
|
|
|
stringio_write(MP_OBJ_FROM_PTR(o), bufinfo.buf, bufinfo.len, NULL);
|
2014-04-26 13:26:14 -04:00
|
|
|
// Cur ptr is always at the beginning of buffer at the construction
|
|
|
|
o->pos = 0;
|
|
|
|
}
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-04-26 13:26:14 -04:00
|
|
|
}
|
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t stringio_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
|
2016-10-09 04:56:11 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
|
2016-07-27 18:53:44 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) },
|
2020-03-10 00:14:35 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_tell), MP_ROM_PTR(&mp_stream_tell_obj) },
|
2016-07-27 18:14:32 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
|
2018-03-07 01:48:53 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_getvalue), MP_ROM_PTR(&stringio_getvalue_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&stringio___exit___obj) },
|
2014-04-26 13:26:14 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(stringio_locals_dict, stringio_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC const mp_stream_p_t stringio_stream_p = {
|
protocols: Allow them to be (optionally) type-safe
Protocols are nice, but there is no way for C code to verify whether
a type's "protocol" structure actually implements some particular
protocol. As a result, you can pass an object that implements the
"vfs" protocol to one that expects the "stream" protocol, and the
opposite of awesomeness ensues.
This patch adds an OPTIONAL (but enabled by default) protocol identifier
as the first member of any protocol structure. This identifier is
simply a unique QSTR chosen by the protocol designer and used by each
protocol implementer. When checking for protocol support, instead of
just checking whether the object's type has a non-NULL protocol field,
use `mp_proto_get` which implements the protocol check when possible.
The existing protocols are now named:
protocol_framebuf
protocol_i2c
protocol_pin
protocol_stream
protocol_spi
protocol_vfs
(most of these are unused in CP and are just inherited from MP; vfs and
stream are definitely used though)
I did not find any crashing examples, but here's one to give a flavor of what
is improved, using `micropython_coverage`. Before the change,
the vfs "ioctl" protocol is invoked, and the result is not intelligible
as json (but it could have resulted in a hard fault, potentially):
>>> import uos, ujson
>>> u = uos.VfsPosix('/tmp')
>>> ujson.load(u)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: syntax error in JSON
After the change, the vfs object is correctly detected as not supporting
the stream protocol:
>>> ujson.load(p)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: stream operation not supported
2019-12-03 15:50:37 -05:00
|
|
|
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_stream)
|
2014-04-26 13:26:14 -04:00
|
|
|
.read = stringio_read,
|
|
|
|
.write = stringio_write,
|
2016-07-27 18:14:32 -04:00
|
|
|
.ioctl = stringio_ioctl,
|
2014-07-27 17:38:58 -04:00
|
|
|
.is_text = true,
|
2014-04-26 13:26:14 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const mp_obj_type_t mp_type_stringio = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_StringIO,
|
|
|
|
.print = stringio_print,
|
|
|
|
.make_new = stringio_make_new,
|
2016-01-09 18:14:54 -05:00
|
|
|
.getiter = mp_identity_getiter,
|
2014-04-26 13:26:14 -04:00
|
|
|
.iternext = mp_stream_unbuffered_iter,
|
2016-06-18 11:19:24 -04:00
|
|
|
.protocol = &stringio_stream_p,
|
2021-03-15 09:57:36 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&stringio_locals_dict,
|
2014-04-26 13:26:14 -04:00
|
|
|
};
|
2014-04-26 13:59:39 -04:00
|
|
|
|
2014-05-24 18:03:12 -04:00
|
|
|
#if MICROPY_PY_IO_BYTESIO
|
2021-04-23 15:26:42 -04:00
|
|
|
STATIC const mp_stream_p_t bytesio_stream_p = {
|
|
|
|
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_stream)
|
|
|
|
.read = stringio_read,
|
|
|
|
.write = stringio_write,
|
|
|
|
.ioctl = stringio_ioctl,
|
|
|
|
};
|
|
|
|
|
2014-05-15 00:28:19 -04:00
|
|
|
const mp_obj_type_t mp_type_bytesio = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_BytesIO,
|
|
|
|
.print = stringio_print,
|
|
|
|
.make_new = stringio_make_new,
|
2016-01-09 18:14:54 -05:00
|
|
|
.getiter = mp_identity_getiter,
|
2014-05-15 00:28:19 -04:00
|
|
|
.iternext = mp_stream_unbuffered_iter,
|
2016-06-18 11:19:24 -04:00
|
|
|
.protocol = &bytesio_stream_p,
|
2021-03-15 09:57:36 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&stringio_locals_dict,
|
2014-05-15 00:28:19 -04:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2014-04-26 13:59:39 -04:00
|
|
|
#endif
|