objstringio.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 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("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. memcpy(o->vstr->buf, buf, o->vstr->len);
  67. o->vstr->fixed_buf = false;
  68. o->ref_obj = MP_OBJ_NULL;
  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_t stringio___exit__(size_t n_args, const mp_obj_t *args) {
  159. (void)n_args;
  160. return mp_stream_close(args[0]);
  161. }
  162. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio___exit__);
  163. STATIC mp_obj_stringio_t *stringio_new(const mp_obj_type_t *type) {
  164. mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t);
  165. o->base.type = type;
  166. o->pos = 0;
  167. o->ref_obj = MP_OBJ_NULL;
  168. return o;
  169. }
  170. 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) {
  171. (void)n_kw; // TODO check n_kw==0
  172. mp_uint_t sz = 16;
  173. bool initdata = false;
  174. mp_buffer_info_t bufinfo;
  175. mp_obj_stringio_t *o = stringio_new(type_in);
  176. if (n_args > 0) {
  177. if (MP_OBJ_IS_INT(args[0])) {
  178. sz = mp_obj_get_int(args[0]);
  179. } else {
  180. mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
  181. if (MP_OBJ_IS_STR_OR_BYTES(args[0])) {
  182. o->vstr = m_new_obj(vstr_t);
  183. vstr_init_fixed_buf(o->vstr, bufinfo.len, bufinfo.buf);
  184. o->vstr->len = bufinfo.len;
  185. o->ref_obj = args[0];
  186. return MP_OBJ_FROM_PTR(o);
  187. }
  188. sz = bufinfo.len;
  189. initdata = true;
  190. }
  191. }
  192. o->vstr = vstr_new(sz);
  193. if (initdata) {
  194. stringio_write(MP_OBJ_FROM_PTR(o), bufinfo.buf, bufinfo.len, NULL);
  195. // Cur ptr is always at the beginning of buffer at the construction
  196. o->pos = 0;
  197. }
  198. return MP_OBJ_FROM_PTR(o);
  199. }
  200. STATIC const mp_rom_map_elem_t stringio_locals_dict_table[] = {
  201. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
  202. { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
  203. { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
  204. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
  205. { MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) },
  206. { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
  207. { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
  208. { MP_ROM_QSTR(MP_QSTR_getvalue), MP_ROM_PTR(&stringio_getvalue_obj) },
  209. { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
  210. { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&stringio___exit___obj) },
  211. };
  212. STATIC MP_DEFINE_CONST_DICT(stringio_locals_dict, stringio_locals_dict_table);
  213. STATIC const mp_stream_p_t stringio_stream_p = {
  214. .read = stringio_read,
  215. .write = stringio_write,
  216. .ioctl = stringio_ioctl,
  217. .is_text = true,
  218. };
  219. STATIC const mp_stream_p_t bytesio_stream_p = {
  220. .read = stringio_read,
  221. .write = stringio_write,
  222. .ioctl = stringio_ioctl,
  223. };
  224. const mp_obj_type_t mp_type_stringio = {
  225. { &mp_type_type },
  226. .name = MP_QSTR_StringIO,
  227. .print = stringio_print,
  228. .make_new = stringio_make_new,
  229. .getiter = mp_identity_getiter,
  230. .iternext = mp_stream_unbuffered_iter,
  231. .protocol = &stringio_stream_p,
  232. .locals_dict = (mp_obj_dict_t*)&stringio_locals_dict,
  233. };
  234. #if MICROPY_PY_IO_BYTESIO
  235. const mp_obj_type_t mp_type_bytesio = {
  236. { &mp_type_type },
  237. .name = MP_QSTR_BytesIO,
  238. .print = stringio_print,
  239. .make_new = stringio_make_new,
  240. .getiter = mp_identity_getiter,
  241. .iternext = mp_stream_unbuffered_iter,
  242. .protocol = &bytesio_stream_p,
  243. .locals_dict = (mp_obj_dict_t*)&stringio_locals_dict,
  244. };
  245. #endif
  246. #endif