objstringio.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. default:
  135. *errcode = MP_EINVAL;
  136. return MP_STREAM_ERROR;
  137. }
  138. }
  139. #define STREAM_TO_CONTENT_TYPE(o) (((o)->base.type == &mp_type_stringio) ? &mp_type_str : &mp_type_bytes)
  140. STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) {
  141. mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
  142. check_stringio_is_open(self);
  143. // TODO: Try to avoid copying string
  144. return mp_obj_new_str_of_type(STREAM_TO_CONTENT_TYPE(self), (byte*)self->vstr->buf, self->vstr->len);
  145. }
  146. STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue);
  147. STATIC mp_obj_t stringio_close(mp_obj_t self_in) {
  148. mp_obj_stringio_t *self = MP_OBJ_TO_PTR(self_in);
  149. #if MICROPY_CPYTHON_COMPAT
  150. vstr_free(self->vstr);
  151. self->vstr = NULL;
  152. #else
  153. vstr_clear(self->vstr);
  154. self->vstr->alloc = 0;
  155. self->vstr->len = 0;
  156. self->pos = 0;
  157. #endif
  158. return mp_const_none;
  159. }
  160. STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_close_obj, stringio_close);
  161. STATIC mp_obj_t stringio___exit__(size_t n_args, const mp_obj_t *args) {
  162. (void)n_args;
  163. return stringio_close(args[0]);
  164. }
  165. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio___exit__);
  166. STATIC mp_obj_stringio_t *stringio_new(const mp_obj_type_t *type) {
  167. mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t);
  168. o->base.type = type;
  169. o->pos = 0;
  170. o->ref_obj = MP_OBJ_NULL;
  171. return o;
  172. }
  173. 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) {
  174. (void)n_kw; // TODO check n_kw==0
  175. mp_uint_t sz = 16;
  176. bool initdata = false;
  177. mp_buffer_info_t bufinfo;
  178. mp_obj_stringio_t *o = stringio_new(type_in);
  179. if (n_args > 0) {
  180. if (MP_OBJ_IS_INT(args[0])) {
  181. sz = mp_obj_get_int(args[0]);
  182. } else {
  183. mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
  184. if (MP_OBJ_IS_STR_OR_BYTES(args[0])) {
  185. o->vstr = m_new_obj(vstr_t);
  186. vstr_init_fixed_buf(o->vstr, bufinfo.len, bufinfo.buf);
  187. o->vstr->len = bufinfo.len;
  188. o->ref_obj = args[0];
  189. return MP_OBJ_FROM_PTR(o);
  190. }
  191. sz = bufinfo.len;
  192. initdata = true;
  193. }
  194. }
  195. o->vstr = vstr_new(sz);
  196. if (initdata) {
  197. stringio_write(MP_OBJ_FROM_PTR(o), bufinfo.buf, bufinfo.len, NULL);
  198. // Cur ptr is always at the beginning of buffer at the construction
  199. o->pos = 0;
  200. }
  201. return MP_OBJ_FROM_PTR(o);
  202. }
  203. STATIC const mp_rom_map_elem_t stringio_locals_dict_table[] = {
  204. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
  205. { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
  206. { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
  207. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
  208. { MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) },
  209. { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
  210. { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&stringio_close_obj) },
  211. { MP_ROM_QSTR(MP_QSTR_getvalue), MP_ROM_PTR(&stringio_getvalue_obj) },
  212. { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
  213. { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&stringio___exit___obj) },
  214. };
  215. STATIC MP_DEFINE_CONST_DICT(stringio_locals_dict, stringio_locals_dict_table);
  216. STATIC const mp_stream_p_t stringio_stream_p = {
  217. .read = stringio_read,
  218. .write = stringio_write,
  219. .ioctl = stringio_ioctl,
  220. .is_text = true,
  221. };
  222. STATIC const mp_stream_p_t bytesio_stream_p = {
  223. .read = stringio_read,
  224. .write = stringio_write,
  225. .ioctl = stringio_ioctl,
  226. };
  227. const mp_obj_type_t mp_type_stringio = {
  228. { &mp_type_type },
  229. .name = MP_QSTR_StringIO,
  230. .print = stringio_print,
  231. .make_new = stringio_make_new,
  232. .getiter = mp_identity_getiter,
  233. .iternext = mp_stream_unbuffered_iter,
  234. .protocol = &stringio_stream_p,
  235. .locals_dict = (mp_obj_dict_t*)&stringio_locals_dict,
  236. };
  237. #if MICROPY_PY_IO_BYTESIO
  238. const mp_obj_type_t mp_type_bytesio = {
  239. { &mp_type_type },
  240. .name = MP_QSTR_BytesIO,
  241. .print = stringio_print,
  242. .make_new = stringio_make_new,
  243. .getiter = mp_identity_getiter,
  244. .iternext = mp_stream_unbuffered_iter,
  245. .protocol = &bytesio_stream_p,
  246. .locals_dict = (mp_obj_dict_t*)&stringio_locals_dict,
  247. };
  248. #endif
  249. #endif