Skip to content

Commit 36cbd0d

Browse files
committed
Merge branch 'master' of github.com:micropython/micropython
2 parents 32bef31 + 27f5bdd commit 36cbd0d

7 files changed

Lines changed: 168 additions & 5 deletions

File tree

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

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: 11 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)
@@ -139,6 +138,7 @@ Q(path)
139138
Q(pow)
140139
Q(print)
141140
Q(range)
141+
Q(read)
142142
Q(repr)
143143
Q(set)
144144
Q(sorted)
@@ -151,6 +151,7 @@ Q(to_bytes)
151151
Q(tuple)
152152
Q(type)
153153
Q(value)
154+
Q(write)
154155
Q(zip)
155156

156157
Q(sep)
@@ -308,6 +309,15 @@ Q(pack)
308309
Q(unpack)
309310
#endif
310311

312+
#if MICROPY_ENABLE_MOD_IO
313+
Q(io)
314+
Q(readall)
315+
Q(readline)
316+
Q(StringIO)
317+
Q(BytesIO)
318+
Q(getvalue)
319+
#endif
320+
311321
#if MICROPY_ENABLE_PROPERTY
312322
Q(property)
313323
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())

unix/qstrdefsport.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
Q(Test)
44

55
Q(fileno)
6-
Q(read)
7-
Q(readall)
8-
Q(readline)
9-
Q(write)
106
Q(makefile)
117

128
Q(FileIO)

0 commit comments

Comments
 (0)