|
30 | 30 | #include "py/runtime.h"
|
31 | 31 | #include "py/builtin.h"
|
32 | 32 | #include "py/stream.h"
|
| 33 | +#include "py/binary.h" |
| 34 | +#include "py/objarray.h" |
33 | 35 | #include "py/objstringio.h"
|
34 | 36 | #include "py/frozenmod.h"
|
35 | 37 |
|
|
38 | 40 | extern const mp_obj_type_t mp_type_fileio;
|
39 | 41 | extern const mp_obj_type_t mp_type_textio;
|
40 | 42 |
|
| 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 | + |
41 | 107 | #if MICROPY_PY_IO_BUFFEREDWRITER
|
42 | 108 | typedef struct _mp_obj_bufwriter_t {
|
43 | 109 | mp_obj_base_t base;
|
@@ -187,6 +253,9 @@ STATIC const mp_rom_map_elem_t mp_module_io_globals_table[] = {
|
187 | 253 | // Note: mp_builtin_open_obj should be defined by port, it's not
|
188 | 254 | // part of the core.
|
189 | 255 | { 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 |
190 | 259 | #if MICROPY_PY_IO_RESOURCE_STREAM
|
191 | 260 | { MP_ROM_QSTR(MP_QSTR_resource_stream), MP_ROM_PTR(&resource_stream_obj) },
|
192 | 261 | #endif
|
|
0 commit comments