Skip to content

Commit cb9dc08

Browse files
committed
modio: Implement io.StringIO class.
1 parent dbc81df commit cb9dc08

File tree

6 files changed

+160
-1
lines changed

6 files changed

+160
-1
lines changed

py/modio.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ STATIC const mp_map_elem_t mp_module_io_globals_table[] = {
1111
// Note: mp_builtin_open_obj should be defined by port, it's not
1212
// part of the core.
1313
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },
14+
{ MP_OBJ_NEW_QSTR(MP_QSTR_BytesIO), (mp_obj_t)&mp_type_stringio },
15+
{ MP_OBJ_NEW_QSTR(MP_QSTR_StringIO), (mp_obj_t)&mp_type_stringio },
1416
};
1517

1618
STATIC const mp_obj_dict_t mp_module_io_globals = {

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ extern const mp_obj_type_t mp_type_module;
292292
extern const mp_obj_type_t mp_type_staticmethod;
293293
extern const mp_obj_type_t mp_type_classmethod;
294294
extern const mp_obj_type_t mp_type_property;
295+
extern const mp_obj_type_t mp_type_stringio;
295296

296297
// Exceptions
297298
extern const mp_obj_type_t mp_type_BaseException;

py/objstringio.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#include "nlr.h"
5+
#include "misc.h"
6+
#include "mpconfig.h"
7+
#include "qstr.h"
8+
#include "obj.h"
9+
#include "runtime.h"
10+
#include "stream.h"
11+
12+
typedef struct _mp_obj_stringio_t {
13+
mp_obj_base_t base;
14+
vstr_t *vstr;
15+
// StringIO has single pointer used for both reading and writing
16+
machine_uint_t pos;
17+
} mp_obj_stringio_t;
18+
19+
STATIC void stringio_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
20+
mp_obj_stringio_t *self = self_in;
21+
print(env, "<io.StringIO 0x%x>", self->vstr);
22+
}
23+
24+
STATIC machine_int_t stringio_read(mp_obj_t o_in, void *buf, machine_uint_t size, int *errcode) {
25+
mp_obj_stringio_t *o = o_in;
26+
machine_uint_t remaining = o->vstr->len - o->pos;
27+
if (size > remaining) {
28+
size = remaining;
29+
}
30+
memcpy(buf, o->vstr->buf + o->pos, size);
31+
o->pos += size;
32+
return size;
33+
}
34+
35+
STATIC machine_int_t stringio_write(mp_obj_t o_in, const void *buf, machine_uint_t size, int *errcode) {
36+
mp_obj_stringio_t *o = o_in;
37+
machine_uint_t remaining = o->vstr->alloc - o->pos;
38+
if (size > remaining) {
39+
// Take all what's already allocated...
40+
o->vstr->len = o->vstr->alloc;
41+
// ... and add more
42+
vstr_add_len(o->vstr, size - remaining);
43+
}
44+
memcpy(o->vstr->buf + o->pos, buf, size);
45+
o->pos += size;
46+
if (o->pos > o->vstr->len) {
47+
o->vstr->len = o->pos;
48+
}
49+
return size;
50+
}
51+
52+
STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) {
53+
mp_obj_stringio_t *self = self_in;
54+
return mp_obj_new_str((byte*)self->vstr->buf, self->vstr->len, false);
55+
}
56+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue);
57+
58+
STATIC mp_obj_t stringio_close(mp_obj_t self_in) {
59+
mp_obj_stringio_t *self = self_in;
60+
vstr_free(self->vstr);
61+
self->vstr = NULL;
62+
return mp_const_none;
63+
}
64+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_close_obj, stringio_close);
65+
66+
mp_obj_t stringio___exit__(uint n_args, const mp_obj_t *args) {
67+
return stringio_close(args[0]);
68+
}
69+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio___exit__);
70+
71+
STATIC mp_obj_stringio_t *stringio_new() {
72+
mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t);
73+
o->base.type = &mp_type_stringio;
74+
o->vstr = vstr_new();
75+
o->pos = 0;
76+
return o;
77+
}
78+
79+
STATIC mp_obj_t stringio_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
80+
mp_obj_stringio_t *o = stringio_new();
81+
82+
if (n_args > 0) {
83+
mp_buffer_info_t bufinfo;
84+
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
85+
stringio_write(o, bufinfo.buf, bufinfo.len, NULL);
86+
// Cur ptr is always at the beginning of buffer at the construction
87+
o->pos = 0;
88+
}
89+
return o;
90+
}
91+
92+
STATIC const mp_map_elem_t stringio_locals_dict_table[] = {
93+
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj },
94+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj },
95+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
96+
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
97+
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&stringio_close_obj },
98+
{ MP_OBJ_NEW_QSTR(MP_QSTR_getvalue), (mp_obj_t)&stringio_getvalue_obj },
99+
{ MP_OBJ_NEW_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj },
100+
{ MP_OBJ_NEW_QSTR(MP_QSTR___exit__), (mp_obj_t)&stringio___exit___obj },
101+
};
102+
103+
STATIC MP_DEFINE_CONST_DICT(stringio_locals_dict, stringio_locals_dict_table);
104+
105+
STATIC const mp_stream_p_t stringio_stream_p = {
106+
.read = stringio_read,
107+
.write = stringio_write,
108+
};
109+
110+
const mp_obj_type_t mp_type_stringio = {
111+
{ &mp_type_type },
112+
.name = MP_QSTR_StringIO,
113+
.print = stringio_print,
114+
.make_new = stringio_make_new,
115+
.getiter = mp_identity,
116+
.iternext = mp_stream_unbuffered_iter,
117+
.stream_p = &stringio_stream_p,
118+
.locals_dict = (mp_obj_t)&stringio_locals_dict,
119+
};

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ PY_O_BASENAME = \
7474
objset.o \
7575
objslice.o \
7676
objstr.o \
77+
objstringio.o \
7778
objtuple.o \
7879
objtype.o \
7980
objzip.o \

py/qstrdefs.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ Q(hash)
118118
Q(hex)
119119
Q(%#x)
120120
Q(id)
121-
Q(io)
122121
Q(int)
123122
Q(isinstance)
124123
Q(issubclass)
@@ -308,6 +307,13 @@ Q(pack)
308307
Q(unpack)
309308
#endif
310309

310+
#if MICROPY_ENABLE_MOD_IO
311+
Q(io)
312+
Q(StringIO)
313+
Q(BytesIO)
314+
Q(getvalue)
315+
#endif
316+
311317
#if MICROPY_ENABLE_PROPERTY
312318
Q(property)
313319
Q(getter)

tests/io/stringio1.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import io
2+
3+
a = io.StringIO()
4+
print(a.getvalue())
5+
print(a.read())
6+
7+
a = io.StringIO("foobar")
8+
print(a.getvalue())
9+
print(a.read())
10+
print(a.read())
11+
12+
a = io.StringIO()
13+
a.write("foo")
14+
print(a.getvalue())
15+
16+
a = io.StringIO("foo")
17+
a.write("12")
18+
print(a.getvalue())
19+
20+
a = io.StringIO("foo")
21+
a.write("123")
22+
print(a.getvalue())
23+
24+
a = io.StringIO("foo")
25+
a.write("1234")
26+
print(a.getvalue())
27+
28+
a = io.StringIO()
29+
a.write("foo")
30+
print(a.read())

0 commit comments

Comments
 (0)