modio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <assert.h>
  27. #include <string.h>
  28. #include "py/runtime.h"
  29. #include "py/builtin.h"
  30. #include "py/stream.h"
  31. #include "py/binary.h"
  32. #include "py/objarray.h"
  33. #include "py/objstringio.h"
  34. #include "py/frozenmod.h"
  35. #if MICROPY_PY_IO
  36. #if MICROPY_PY_IO_IOBASE
  37. static const mp_obj_type_t mp_type_iobase;
  38. static const mp_obj_base_t iobase_singleton = {&mp_type_iobase};
  39. 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) {
  40. (void)type;
  41. (void)n_args;
  42. (void)n_kw;
  43. (void)args;
  44. return MP_OBJ_FROM_PTR(&iobase_singleton);
  45. }
  46. static mp_uint_t iobase_read_write(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode, qstr qst) {
  47. mp_obj_t dest[3];
  48. mp_load_method(obj, qst, dest);
  49. mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, size, buf};
  50. dest[2] = MP_OBJ_FROM_PTR(&ar);
  51. mp_obj_t ret_obj = mp_call_method_n_kw(1, 0, dest);
  52. if (ret_obj == mp_const_none) {
  53. *errcode = MP_EAGAIN;
  54. return MP_STREAM_ERROR;
  55. }
  56. mp_int_t ret = mp_obj_get_int(ret_obj);
  57. if (ret >= 0) {
  58. return ret;
  59. } else {
  60. *errcode = -ret;
  61. return MP_STREAM_ERROR;
  62. }
  63. }
  64. static mp_uint_t iobase_read(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode) {
  65. return iobase_read_write(obj, buf, size, errcode, MP_QSTR_readinto);
  66. }
  67. static mp_uint_t iobase_write(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode) {
  68. return iobase_read_write(obj, (void *)buf, size, errcode, MP_QSTR_write);
  69. }
  70. static mp_uint_t iobase_ioctl(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode) {
  71. mp_obj_t dest[4];
  72. mp_load_method(obj, MP_QSTR_ioctl, dest);
  73. dest[2] = mp_obj_new_int_from_uint(request);
  74. dest[3] = mp_obj_new_int_from_uint(arg);
  75. mp_int_t ret = mp_obj_get_int(mp_call_method_n_kw(2, 0, dest));
  76. if (ret >= 0) {
  77. return ret;
  78. } else {
  79. *errcode = -ret;
  80. return MP_STREAM_ERROR;
  81. }
  82. }
  83. static const mp_stream_p_t iobase_p = {
  84. .read = iobase_read,
  85. .write = iobase_write,
  86. .ioctl = iobase_ioctl,
  87. };
  88. static MP_DEFINE_CONST_OBJ_TYPE(
  89. mp_type_iobase,
  90. MP_QSTR_IOBase,
  91. MP_TYPE_FLAG_NONE,
  92. make_new, iobase_make_new,
  93. protocol, &iobase_p
  94. );
  95. #endif // MICROPY_PY_IO_IOBASE
  96. #if MICROPY_PY_IO_BUFFEREDWRITER
  97. typedef struct _mp_obj_bufwriter_t {
  98. mp_obj_base_t base;
  99. mp_obj_t stream;
  100. size_t alloc;
  101. size_t len;
  102. byte buf[0];
  103. } mp_obj_bufwriter_t;
  104. static mp_obj_t bufwriter_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  105. mp_arg_check_num(n_args, n_kw, 2, 2, false);
  106. size_t alloc = mp_obj_get_int(args[1]);
  107. mp_obj_bufwriter_t *o = mp_obj_malloc_var(mp_obj_bufwriter_t, buf, byte, alloc, type);
  108. o->stream = args[0];
  109. o->alloc = alloc;
  110. o->len = 0;
  111. return o;
  112. }
  113. static mp_uint_t bufwriter_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
  114. mp_obj_bufwriter_t *self = MP_OBJ_TO_PTR(self_in);
  115. mp_uint_t org_size = size;
  116. while (size > 0) {
  117. mp_uint_t rem = self->alloc - self->len;
  118. if (size < rem) {
  119. memcpy(self->buf + self->len, buf, size);
  120. self->len += size;
  121. return org_size;
  122. }
  123. // Buffer flushing policy here is to flush entire buffer all the time.
  124. // This allows e.g. to have a block device as backing storage and write
  125. // entire block to it. memcpy below is not ideal and could be optimized
  126. // in some cases. But the way it is now it at least ensures that buffer
  127. // is word-aligned, to guard against obscure cases when it matters, e.g.
  128. // https://github.com/micropython/micropython/issues/1863
  129. memcpy(self->buf + self->len, buf, rem);
  130. buf = (byte *)buf + rem;
  131. size -= rem;
  132. mp_uint_t out_sz = mp_stream_write_exactly(self->stream, self->buf, self->alloc, errcode);
  133. (void)out_sz;
  134. if (*errcode != 0) {
  135. return MP_STREAM_ERROR;
  136. }
  137. // TODO: try to recover from a case of non-blocking stream, e.g. move
  138. // remaining chunk to the beginning of buffer.
  139. assert(out_sz == self->alloc);
  140. self->len = 0;
  141. }
  142. return org_size;
  143. }
  144. static mp_obj_t bufwriter_flush(mp_obj_t self_in) {
  145. mp_obj_bufwriter_t *self = MP_OBJ_TO_PTR(self_in);
  146. if (self->len != 0) {
  147. int err;
  148. mp_uint_t out_sz = mp_stream_write_exactly(self->stream, self->buf, self->len, &err);
  149. (void)out_sz;
  150. // TODO: try to recover from a case of non-blocking stream, e.g. move
  151. // remaining chunk to the beginning of buffer.
  152. assert(out_sz == self->len);
  153. self->len = 0;
  154. if (err != 0) {
  155. mp_raise_OSError(err);
  156. }
  157. }
  158. return mp_const_none;
  159. }
  160. static MP_DEFINE_CONST_FUN_OBJ_1(bufwriter_flush_obj, bufwriter_flush);
  161. static const mp_rom_map_elem_t bufwriter_locals_dict_table[] = {
  162. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
  163. { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&bufwriter_flush_obj) },
  164. };
  165. static MP_DEFINE_CONST_DICT(bufwriter_locals_dict, bufwriter_locals_dict_table);
  166. static const mp_stream_p_t bufwriter_stream_p = {
  167. .write = bufwriter_write,
  168. };
  169. static MP_DEFINE_CONST_OBJ_TYPE(
  170. mp_type_bufwriter,
  171. MP_QSTR_BufferedWriter,
  172. MP_TYPE_FLAG_NONE,
  173. make_new, bufwriter_make_new,
  174. protocol, &bufwriter_stream_p,
  175. locals_dict, &bufwriter_locals_dict
  176. );
  177. #endif // MICROPY_PY_IO_BUFFEREDWRITER
  178. static const mp_rom_map_elem_t mp_module_io_globals_table[] = {
  179. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_io) },
  180. // Note: mp_builtin_open_obj should be defined by port, it's not
  181. // part of the core.
  182. { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
  183. #if MICROPY_PY_IO_IOBASE
  184. { MP_ROM_QSTR(MP_QSTR_IOBase), MP_ROM_PTR(&mp_type_iobase) },
  185. #endif
  186. { MP_ROM_QSTR(MP_QSTR_StringIO), MP_ROM_PTR(&mp_type_stringio) },
  187. #if MICROPY_PY_IO_BYTESIO
  188. { MP_ROM_QSTR(MP_QSTR_BytesIO), MP_ROM_PTR(&mp_type_bytesio) },
  189. #endif
  190. #if MICROPY_PY_IO_BUFFEREDWRITER
  191. { MP_ROM_QSTR(MP_QSTR_BufferedWriter), MP_ROM_PTR(&mp_type_bufwriter) },
  192. #endif
  193. };
  194. static MP_DEFINE_CONST_DICT(mp_module_io_globals, mp_module_io_globals_table);
  195. const mp_obj_module_t mp_module_io = {
  196. .base = { &mp_type_module },
  197. .globals = (mp_obj_dict_t *)&mp_module_io_globals,
  198. };
  199. MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_io, mp_module_io);
  200. #endif