10000 py/modio: Add uio.IOBase class to allow to define user streams. · micropython/micropython@af0932a · GitHub
[go: up one dir, main page]

Skip to content

Commit af0932a

Browse files
committed
py/modio: Add uio.IOBase class to allow to define user streams.
A user class derived from IOBase and implementing readinto/write/ioctl can now be used anywhere a native stream object is accepted. The mapping from C to Python is: stream_p->read --> readinto(buf) stream_p->write --> write(buf) stream_p->ioctl --> ioctl(request, arg) Among other things it allows the user to: - create an object which can be passed as the file argument to print: print(..., file=myobj), and then print will pass all the data to the object via the objects write method (same as CPython) - pass a user object to uio.BufferedWriter to buffer the writes (same as CPython) - use select.select on a user object - register user objects with select.poll, in particular so user objects can be used with uasyncio - create user files that can be returned from user filesystems, and import can import scripts from these user files For example: class MyOut(io.IOBase): def write(self, buf): print('write', repr(buf)) return len(buf) print('hello', file=MyOut()) The feature is enabled via MICROPY_PY_IO_IOBASE which is disabled by default.
1 parent 6a445b6 commit af0932a

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

py/modio.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "py/runtime.h"
3131
#include "py/builtin.h"
3232
#include "py/stream.h"
33+
#include "py/binary.h"
34+
#include "py/objarray.h"
3335
#include "py/objstringio.h"
3436
#include "py/frozenmod.h"
3537

@@ -38,6 +40,70 @@
3840
extern const mp_obj_type_t mp_type_fileio;
3941
extern const mp_obj_type_t mp_type_textio;
4042

43+
#if MICROPY_PY_IO_IOBASE
44+
45+
STATIC const mp_obj_type_t mp_type_iobase;
46+
47+
STATIC mp_obj_base_t iobase_singleton = {&mp_type_iobase};
48+
49+
STATIC 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) {
50+
(void)type;
51+
(void)n_args;
52+
(void)n_kw;
53+
(void)args;
54+
return MP_OBJ_FROM_PTR(&iobase_singleton);
55+
}
56+
57+
STATIC mp_uint_t iobase_read_write(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode, qstr qst) {
58+
mp_obj_t dest[3];
59+
mp_load_method(obj, qst, dest);
60+
mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, size, buf};
61+
dest[2] = MP_OBJ_FROM_PTR(&ar);
62+
mp_obj_t ret = mp_call_method_n_kw(1, 0, dest);
63+
if (ret == mp_const_none) {
64+
*errcode = MP_EAGAIN;
65+
return MP_STREAM_ERROR;
66+
} else {
67+
return mp_obj_get_int(ret);
68+
}
69+
}
70+
STATIC mp_uint_t iobase_read(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode) {
71+
return iobase_read_write(obj, buf, size, errcode, MP_QSTR_readinto);
72+
}
73+
74+
STATIC mp_uint_t iobase_write(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode) {
75+
return iobase_read_write(obj, (void*)buf, size, errcode, MP_QSTR_write);
76+
}
77+
78+
STATIC mp_uint_t iobase_ioctl(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode) {
79+
mp_obj_t dest[4];
80+
mp_load_method(obj, MP_QSTR_ioctl, dest);
81+
dest[2] = mp_obj_new_int_from_uint(request);
82+
dest[3] = mp_obj_new_int_from_uint(arg);
83+
mp_int_t ret = mp_obj_get_int(mp_call_method_n_kw(2, 0, dest));
84+
if (ret >= 0) {
85+
return ret;
86+
} else {
87+
*errcode = -ret;
88+
return MP_STREAM_ERROR;
89+
}
90+
}
91+
92+
STATIC const mp_stream_p_t iobase_p = {
93+
.read = iobase_read,
94+
.write = iobase_write,
95+
.ioctl = iobase_ioctl,
96+
};
97+
98+
STATIC const mp_obj_type_t mp_type_iobase = {
99+
{ &mp_type_type },
100+
.name = MP_QSTR_IOBase,
101+
.make_new = iobase_make_new,
102+
.protocol = &iobase_p,
103+
};
104+
105+
#endif // MICROPY_PY_IO_IOBASE
106+
41107
#if MICROPY_PY_IO_BUFFEREDWRITER
42108
typedef struct _mp_obj_bufwriter_t {
43109
mp_obj_base_t base;
@@ -187,6 +253,9 @@ STATIC const mp_rom_map_elem_t mp_module_io_globals_table[] = {
187253
// Note: mp_builtin_open_obj should be defined by port, it's not
188254
// part of the core.
189255
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
256+
#if MICROPY_PY_IO_IOBASE
257+
{ MP_ROM_QSTR(MP_QSTR_IOBase), MP_ROM_PTR(&mp_type_iobase) },
258+
#endif
190259
#if MICROPY_PY_IO_RESOURCE_STREAM
191260
{ MP_ROM_QSTR(MP_QSTR_resource_stream), MP_ROM_PTR(&resource_stream_obj) },
192261
#endif

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,11 @@ typedef double mp_float_t;
998998
#define MICROPY_PY_IO (1)
999999
#endif
10001000

1001+
// Whether to provide "io.IOBase" class to support user streams
1002+
#ifndef MICROPY_PY_IO_IOBASE
1003+
#define MICROPY_PY_IO_IOBASE (0)
1004+
#endif
1005+
10011006
// Whether to provide "uio.resource_stream()" function with
10021007
// the semantics of CPython's pkg_resources.resource_stream()
10031008
// (allows to access binary resources in frozen source packages).

0 commit comments

Comments
 (0)
0