|
1 | 1 | #include <stdio.h> |
| 2 | +#include <string.h> |
2 | 3 |
|
3 | 4 | #include "py/obj.h" |
4 | 5 | #include "py/objstr.h" |
|
8 | 9 | #include "py/builtin.h" |
9 | 10 | #include "py/emit.h" |
10 | 11 | #include "py/formatfloat.h" |
| 12 | +#include "py/stream.h" |
| 13 | + |
| 14 | +// stream testing object |
| 15 | +typedef struct _mp_obj_streamtest_t { |
| 16 | + mp_obj_base_t base; |
| 17 | + uint8_t *buf; |
| 18 | + size_t len; |
| 19 | + size_t pos; |
| 20 | + int error_code; |
| 21 | +} mp_obj_streamtest_t; |
| 22 | + |
| 23 | +STATIC mp_obj_t stest_set_buf(mp_obj_t o_in, mp_obj_t buf_in) { |
| 24 | + mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in); |
| 25 | + mp_buffer_info_t bufinfo; |
| 26 | + mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); |
| 27 | + o->buf = m_new(uint8_t, bufinfo.len); |
| 28 | + memcpy(o->buf, bufinfo.buf, bufinfo.len); |
| 29 | + o->len = bufinfo.len; |
| 30 | + o->pos = 0; |
| 31 | + return mp_const_none; |
| 32 | +} |
| 33 | +STATIC MP_DEFINE_CONST_FUN_OBJ_2(stest_set_buf_obj, stest_set_buf); |
| 34 | + |
| 35 | +STATIC mp_obj_t stest_set_error(mp_obj_t o_in, mp_obj_t err_in) { |
| 36 | + mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in); |
| 37 | + o->error_code = mp_obj_get_int(err_in); |
| 38 | + return mp_const_none; |
| 39 | +} |
| 40 | +STATIC MP_DEFINE_CONST_FUN_OBJ_2(stest_set_error_obj, stest_set_error); |
| 41 | + |
| 42 | +STATIC mp_uint_t stest_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) { |
| 43 | + mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in); |
| 44 | + if (o->pos < o->len) { |
| 45 | + if (size > o->len - o->pos) { |
| 46 | + size = o->len - o->pos; |
| 47 | + } |
| 48 | + memcpy(buf, o->buf + o->pos, size); |
| 49 | + o->pos += size; |
| 50 | + return size; |
| 51 | + } else if (o->error_code == 0) { |
| 52 | + return 0; |
| 53 | + } else { |
| 54 | + *errcode = o->error_code; |
| 55 | + return MP_STREAM_ERROR; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +STATIC mp_uint_t stest_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) { |
| 60 | + mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in); |
| 61 | + (void)buf; |
| 62 | + (void)size; |
| 63 | + *errcode = o->error_code; |
| 64 | + return MP_STREAM_ERROR; |
| 65 | +} |
| 66 | + |
| 67 | +STATIC mp_uint_t stest_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) { |
| 68 | + mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in); |
| 69 | + (void)arg; |
| 70 | + (void)request; |
| 71 | + (void)errcode; |
| 72 | + if (o->error_code != 0) { |
| 73 | + *errcode = o->error_code; |
| 74 | + return MP_STREAM_ERROR; |
| 75 | + } |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +STATIC const mp_rom_map_elem_t rawfile_locals_dict_table[] = { |
| 80 | + { MP_ROM_QSTR(MP_QSTR_set_buf), MP_ROM_PTR(&stest_set_buf_obj) }, |
| 81 | + { MP_ROM_QSTR(MP_QSTR_set_error), MP_ROM_PTR(&stest_set_error_obj) }, |
| 82 | + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, |
| 83 | + { MP_ROM_QSTR(MP_QSTR_read1), MP_ROM_PTR(&mp_stream_read1_obj) }, |
| 84 | + { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, |
| 85 | + { MP_ROM_QSTR(MP_QSTR_write1), MP_ROM_PTR(&mp_stream_write1_obj) }, |
| 86 | + { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, |
| 87 | + { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, |
| 88 | + { MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&mp_stream_ioctl_obj) }, |
| 89 | +}; |
| 90 | + |
| 91 | +STATIC MP_DEFINE_CONST_DICT(rawfile_locals_dict, rawfile_locals_dict_table); |
| 92 | + |
| 93 | +STATIC const mp_stream_p_t fileio_stream_p = { |
| 94 | + .read = stest_read, |
| 95 | + .write = stest_write, |
| 96 | + .ioctl = stest_ioctl, |
| 97 | +}; |
| 98 | + |
| 99 | +STATIC const mp_obj_type_t mp_type_stest_fileio = { |
| 100 | + { &mp_type_type }, |
| 101 | + .protocol = &fileio_stream_p, |
| 102 | + .locals_dict = (mp_obj_dict_t*)&rawfile_locals_dict, |
| 103 | +}; |
| 104 | + |
| 105 | +// stream read returns non-blocking error |
| 106 | +STATIC mp_uint_t stest_read2(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) { |
| 107 | + (void)o_in; |
| 108 | + (void)buf; |
| 109 | + (void)size; |
| 110 | + *errcode = MP_EAGAIN; |
| 111 | + return MP_STREAM_ERROR; |
| 112 | +} |
| 113 | + |
| 114 | +STATIC const mp_rom_map_elem_t rawfile_locals_dict_table2[] = { |
| 115 | + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, |
| 116 | + { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, |
| 117 | +}; |
| 118 | + |
| 119 | +STATIC MP_DEFINE_CONST_DICT(rawfile_locals_dict2, rawfile_locals_dict_table2); |
| 120 | + |
| 121 | +STATIC const mp_stream_p_t textio_stream_p2 = { |
| 122 | + .read = stest_read2, |
| 123 | + .write = NULL, |
| 124 | + .is_text = true, |
| 125 | +}; |
| 126 | + |
| 127 | +STATIC const mp_obj_type_t mp_type_stest_textio2 = { |
| 128 | + { &mp_type_type }, |
| 129 | + .protocol = &textio_stream_p2, |
| 130 | + .locals_dict = (mp_obj_dict_t*)&rawfile_locals_dict2, |
| 131 | +}; |
| 132 | + |
11 | 133 |
|
12 | 134 | #if defined(MICROPY_UNIX_COVERAGE) |
13 | 135 |
|
@@ -157,8 +279,17 @@ STATIC mp_obj_t extra_coverage(void) { |
157 | 279 | mp_printf(&mp_plat_print, "%s\n", buf2); |
158 | 280 | } |
159 | 281 |
|
| 282 | + mp_obj_streamtest_t *s = m_new_obj(mp_obj_streamtest_t); |
| 283 | + s->base.type = &mp_type_stest_fileio; |
| 284 | + s->buf = NULL; |
| 285 | + s->len = 0; |
| 286 | + s->pos = 0; |
| 287 | + s->error_code = 0; |
| 288 | + mp_obj_streamtest_t *s2 = m_new_obj(mp_obj_streamtest_t); |
| 289 | + s2->base.type = &mp_type_stest_textio2; |
| 290 | + |
160 | 291 | // return a tuple of data for testing on the Python side |
161 | | - mp_obj_t items[] = {(mp_obj_t)&str_no_hash_obj, (mp_obj_t)&bytes_no_hash_obj}; |
| 292 | + mp_obj_t items[] = {(mp_obj_t)&str_no_hash_obj, (mp_obj_t)&bytes_no_hash_obj, MP_OBJ_FROM_PTR(s), MP_OBJ_FROM_PTR(s2)}; |
162 | 293 | return mp_obj_new_tuple(MP_ARRAY_SIZE(items), items); |
163 | 294 | } |
164 | 295 | MP_DEFINE_CONST_FUN_OBJ_0(extra_coverage_obj, extra_coverage); |
|
0 commit comments