objstringio.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. * Copyright (c) 2014-2017 Paul Sokolovsky
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "py/objstr.h"
  30. #include "py/objstringio.h"
  31. #include "py/runtime.h"
  32. #include "py/stream.h"
  33. #if MICROPY_PY_IO
  34. #if MICROPY_CPYTHON_COMPAT
  35. static void check_stringio_is_open(const mp_obj_stringio_t *o) {
  36. if (o->vstr == NULL) {
  37. mp_raise_ValueError(MP_ERROR_TEXT("I/O operation on closed file"));
  38. }
  39. }
  40. #else
  41. #define check_stringio_is_open(o)
  42. #endif
  43. static void stringio_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  44. (void)kind;
  45. mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
  46. mp_printf(print, self->base.type == &mp_type_stringio ? "<io.StringIO 0x%x>" : "<io.BytesIO 0x%x>", self);
  47. }
  48. static mp_uint_t stringio_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
  49. (void)errcode;
  50. mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
  51. check_stringio_is_open(o);
  52. if (o->vstr->len <= o->pos) { // read to EOF, or seeked to EOF or beyond
  53. return 0;
  54. }
  55. mp_uint_t remaining = o->vstr->len - o->pos;
  56. if (size > remaining) {
  57. size = remaining;
  58. }
  59. memcpy(buf, o->vstr->buf + o->pos, size);
  60. o->pos += size;
  61. return size;
  62. }
  63. static void stringio_copy_on_write(mp_obj_stringio_t *o) {
  64. const void *buf = o->vstr->buf;
  65. o->vstr->buf = m_new(char, o->vstr->len);
  66. o->vstr->fixed_buf = false;
  67. o->ref_obj = MP_OBJ_NULL;
  68. memcpy(o->vstr->buf, buf, o->vstr->len);
  69. }
  70. static mp_uint_t stringio_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
  71. (void)errcode;
  72. mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
  73. check_stringio_is_open(o);
  74. if (o->vstr->fixed_buf) {
  75. stringio_copy_on_write(o);
  76. }
  77. mp_uint_t new_pos = o->pos + size;
  78. if (new_pos < size) {
  79. // Writing <size> bytes will overflow o->pos beyond limit of mp_uint_t.
  80. *errcode = MP_EFBIG;
  81. return MP_STREAM_ERROR;
  82. }
  83. mp_uint_t org_len = o->vstr->len;
  84. if (new_pos > o->vstr->alloc) {
  85. // Take all what's already allocated...
  86. o->vstr->len = o->vstr->alloc;
  87. // ... and add more
  88. vstr_add_len(o->vstr, new_pos - o->vstr->alloc);
  89. }
  90. // If there was a seek past EOF, clear the hole
  91. if (o->pos > org_len) {
  92. memset(o->vstr->buf + org_len, 0, o->pos - org_len);
  93. }
  94. memcpy(o->vstr->buf + o->pos, buf, size);
  95. o->pos = new_pos;
  96. if (new_pos > o->vstr->len) {
  97. o->vstr->len = new_pos;
  98. }
  99. return size;
  100. }
  101. static mp_uint_t stringio_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
  102. (void)errcode;
  103. mp_obj_stringio_t *o = MP_OBJ_TO_PTR(o_in);
  104. switch (request) {
  105. case MP_STREAM_SEEK: {
  106. struct mp_stream_seek_t *s = (struct mp_stream_seek_t *)arg;
  107. mp_uint_t ref = 0;
  108. switch (s->whence) {
  109. case MP_SEEK_CUR:
  110. ref = o->pos;
  111. break;
  112. case MP_SEEK_END:
  113. ref = o->vstr->len;
  114. break;
  115. }
  116. mp_uint_t new_pos = ref + s->offset;
  117. // For MP_SEEK_SET, offset is unsigned
  118. if (s->whence != MP_SEEK_SET && s->offset < 0) {
  119. if (new_pos > ref) {
  120. // Negative offset from SEEK_CUR or SEEK_END went past 0.
  121. // CPython sets position to 0, POSIX returns an EINVAL error
  122. new_pos = 0;
  123. }
  124. } else if (new_pos < ref) {
  125. // positive offset went beyond the limit of mp_uint_t
  126. *errcode = MP_EINVAL; // replace with MP_EOVERFLOW when defined
  127. return MP_STREAM_ERROR;
  128. }
  129. s->offset = o->pos = new_pos;
  130. return 0;
  131. }
  132. case MP_STREAM_FLUSH:
  133. return 0;
  134. case MP_STREAM_CLOSE:
  135. #if MICROPY_CPYTHON_COMPAT
  136. vstr_free(o->vstr);
  137. o->vstr = NULL;
  138. #else
  139. vstr_clear(o->vstr);
  140. o->vstr->alloc = 0;
  141. o->vstr->len = 0;
  142. o->pos = 0;
  143. #endif
  144. return 0;
  145. default:
  146. *errcode = MP_EINVAL;
  147. return MP_STREAM_ERROR;
  148. }
  149. }
  150. #define STREAM_TO_CONTENT_TYPE(o) (((o)->base.type == &mp_type_stringio) ? &mp_type_str : &mp_type_bytes)
  151. static mp_obj_t stringio_getvalue(mp_obj_t self_in) {
  152. mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
  153. check_stringio_is_open(self);
  154. // TODO: Try to avoid copying string
  155. return mp_obj_new_str_of_type(STREAM_TO_CONTENT_TYPE(self), (byte *)self->vstr->buf, self->vstr->len);
  156. }
  157. static MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue);
  158. static mp_obj_stringio_t *stringio_new(const mp_obj_type_t *type) {
  159. mp_obj_stringio_t *o = mp_obj_malloc(mp_obj_stringio_t, type);
  160. o->pos = 0;
  161. o->ref_obj = MP_OBJ_NULL;
  162. return o;
  163. }
  164. static mp_obj_t stringio_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  165. (void)n_kw; // TODO check n_kw==0
  166. mp_uint_t sz = 16;
  167. bool initdata = false;
  168. mp_buffer_info_t bufinfo;
  169. mp_obj_stringio_t *o = stringio_new(type_in);
  170. if (n_args > 0) {
  171. if (mp_obj_is_int(args[0])) {
  172. sz = mp_obj_get_int(args[0]);
  173. } else {
  174. mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
  175. if (mp_obj_is_str_or_bytes(args[0])) {
  176. o->vstr = m_new_obj(vstr_t);
  177. vstr_init_fixed_buf(o->vstr, bufinfo.len, bufinfo.buf);
  178. o->vstr->len = bufinfo.len;
  179. o->ref_obj = args[0];
  180. return MP_OBJ_FROM_PTR(o);
  181. }
  182. sz = bufinfo.len;
  183. initdata = true;
  184. }
  185. }
  186. o->vstr = vstr_new(sz);
  187. if (initdata) {
  188. stringio_write(MP_OBJ_FROM_PTR(o), bufinfo.buf, bufinfo.len, NULL);
  189. // Cur ptr is always at the beginning of buffer at the construction
  190. o->pos = 0;
  191. }
  192. return MP_OBJ_FROM_PTR(o);
  193. }
  194. static const mp_rom_map_elem_t stringio_locals_dict_table[] = {
  195. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
  196. { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
  197. { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
  198. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
  199. { MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) },
  200. { MP_ROM_QSTR(MP_QSTR_tell), MP_ROM_PTR(&mp_stream_tell_obj) },
  201. { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
  202. { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
  203. { MP_ROM_QSTR(MP_QSTR_getvalue), MP_ROM_PTR(&stringio_getvalue_obj) },
  204. { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
  205. { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&mp_stream___exit___obj) },
  206. };
  207. static MP_DEFINE_CONST_DICT(stringio_locals_dict, stringio_locals_dict_table);
  208. static const mp_stream_p_t stringio_stream_p = {
  209. .read = stringio_read,
  210. .write = stringio_write,
  211. .ioctl = stringio_ioctl,
  212. .is_text = true,
  213. };
  214. MP_DEFINE_CONST_OBJ_TYPE(
  215. mp_type_stringio,
  216. MP_QSTR_StringIO,
  217. MP_TYPE_FLAG_ITER_IS_STREAM,
  218. make_new, stringio_make_new,
  219. print, stringio_print,
  220. protocol, &stringio_stream_p,
  221. locals_dict, &stringio_locals_dict
  222. );
  223. #if MICROPY_PY_IO_BYTESIO
  224. static const mp_stream_p_t bytesio_stream_p = {
  225. .read = stringio_read,
  226. .write = stringio_write,
  227. .ioctl = stringio_ioctl,
  228. };
  229. MP_DEFINE_CONST_OBJ_TYPE(
  230. mp_type_bytesio,
  231. MP_QSTR_BytesIO,
  232. MP_TYPE_FLAG_ITER_IS_STREAM,
  233. make_new, stringio_make_new,
  234. print, stringio_print,
  235. protocol, &bytesio_stream_p,
  236. locals_dict, &stringio_locals_dict
  237. );
  238. #endif
  239. #endif