stream.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2014 Damien P. George
  7. * Copyright (c) 2014-2016 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 <string.h>
  28. #include <unistd.h>
  29. #include "py/objstr.h"
  30. #include "py/stream.h"
  31. #include "py/runtime.h"
  32. // This file defines generic Python stream read/write methods which
  33. // dispatch to the underlying stream interface of an object.
  34. // TODO: should be in mpconfig.h
  35. #define DEFAULT_BUFFER_SIZE 256
  36. static mp_obj_t stream_readall(mp_obj_t self_in);
  37. // Returns error condition in *errcode, if non-zero, return value is number of bytes written
  38. // before error condition occurred. If *errcode == 0, returns total bytes written (which will
  39. // be equal to input size).
  40. mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf_, mp_uint_t size, int *errcode, byte flags) {
  41. byte *buf = buf_;
  42. typedef mp_uint_t (*io_func_t)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
  43. io_func_t io_func;
  44. const mp_stream_p_t *stream_p = mp_get_stream(stream);
  45. if (flags & MP_STREAM_RW_WRITE) {
  46. io_func = (io_func_t)stream_p->write;
  47. } else {
  48. io_func = stream_p->read;
  49. }
  50. *errcode = 0;
  51. mp_uint_t done = 0;
  52. while (size > 0) {
  53. mp_uint_t out_sz = io_func(stream, buf, size, errcode);
  54. // For read, out_sz == 0 means EOF. For write, it's unspecified
  55. // what it means, but we don't make any progress, so returning
  56. // is still the best option.
  57. if (out_sz == 0) {
  58. return done;
  59. }
  60. if (out_sz == MP_STREAM_ERROR) {
  61. // If we read something before getting EAGAIN, don't leak it
  62. if (mp_is_nonblocking_error(*errcode) && done != 0) {
  63. *errcode = 0;
  64. }
  65. return done;
  66. }
  67. if (flags & MP_STREAM_RW_ONCE) {
  68. return out_sz;
  69. }
  70. buf += out_sz;
  71. size -= out_sz;
  72. done += out_sz;
  73. }
  74. return done;
  75. }
  76. mp_off_t mp_stream_seek(mp_obj_t stream, mp_off_t offset, int whence, int *errcode) {
  77. struct mp_stream_seek_t seek_s;
  78. seek_s.offset = offset;
  79. seek_s.whence = whence;
  80. const mp_stream_p_t *stream_p = mp_get_stream(stream);
  81. mp_uint_t res = stream_p->ioctl(MP_OBJ_FROM_PTR(stream), MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, errcode);
  82. if (res == MP_STREAM_ERROR) {
  83. return (mp_off_t)-1;
  84. }
  85. return seek_s.offset;
  86. }
  87. const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) {
  88. const mp_obj_type_t *type = mp_obj_get_type(self_in);
  89. if (MP_OBJ_TYPE_HAS_SLOT(type, protocol)) {
  90. const mp_stream_p_t *stream_p = MP_OBJ_TYPE_GET_SLOT(type, protocol);
  91. if (!((flags & MP_STREAM_OP_READ) && stream_p->read == NULL)
  92. && !((flags & MP_STREAM_OP_WRITE) && stream_p->write == NULL)
  93. && !((flags & MP_STREAM_OP_IOCTL) && stream_p->ioctl == NULL)) {
  94. return stream_p;
  95. }
  96. }
  97. // CPython: io.UnsupportedOperation, OSError subclass
  98. mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("stream operation not supported"));
  99. }
  100. static mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte flags) {
  101. // What to do if sz < -1? Python docs don't specify this case.
  102. // CPython does a readall, but here we silently let negatives through,
  103. // and they will cause a MemoryError.
  104. mp_int_t sz;
  105. if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) {
  106. return stream_readall(args[0]);
  107. }
  108. const mp_stream_p_t *stream_p = mp_get_stream(args[0]);
  109. #if MICROPY_PY_BUILTINS_STR_UNICODE
  110. if (stream_p->is_text) {
  111. // We need to read sz number of unicode characters. Because we don't have any
  112. // buffering, and because the stream API can only read bytes, we must read here
  113. // in units of bytes and must never over read. If we want sz chars, then reading
  114. // sz bytes will never over-read, so we follow this approach, in a loop to keep
  115. // reading until we have exactly enough chars. This will be 1 read for text
  116. // with ASCII-only chars, and about 2 reads for text with a couple of non-ASCII
  117. // chars. For text with lots of non-ASCII chars, it'll be pretty inefficient
  118. // in time and memory.
  119. vstr_t vstr;
  120. vstr_init(&vstr, sz);
  121. mp_uint_t more_bytes = sz;
  122. mp_uint_t last_buf_offset = 0;
  123. while (more_bytes > 0) {
  124. char *p = vstr_add_len(&vstr, more_bytes);
  125. int error;
  126. mp_uint_t out_sz = mp_stream_read_exactly(args[0], p, more_bytes, &error);
  127. if (error != 0) {
  128. vstr_cut_tail_bytes(&vstr, more_bytes);
  129. if (mp_is_nonblocking_error(error)) {
  130. // With non-blocking streams, we read as much as we can.
  131. // If we read nothing, return None, just like read().
  132. // Otherwise, return data read so far.
  133. // TODO what if we have read only half a non-ASCII char?
  134. if (vstr.len == 0) {
  135. vstr_clear(&vstr);
  136. return mp_const_none;
  137. }
  138. break;
  139. }
  140. mp_raise_OSError(error);
  141. }
  142. if (out_sz < more_bytes) {
  143. // Finish reading.
  144. // TODO what if we have read only half a non-ASCII char?
  145. vstr_cut_tail_bytes(&vstr, more_bytes - out_sz);
  146. if (out_sz == 0) {
  147. break;
  148. }
  149. }
  150. // count chars from bytes just read
  151. for (mp_uint_t off = last_buf_offset;;) {
  152. byte b = vstr.buf[off];
  153. int n;
  154. if (!UTF8_IS_NONASCII(b)) {
  155. // 1-byte ASCII char
  156. n = 1;
  157. } else if ((b & 0xe0) == 0xc0) {
  158. // 2-byte char
  159. n = 2;
  160. } else if ((b & 0xf0) == 0xe0) {
  161. // 3-byte char
  162. n = 3;
  163. } else if ((b & 0xf8) == 0xf0) {
  164. // 4-byte char
  165. n = 4;
  166. } else {
  167. // TODO
  168. n = 5;
  169. }
  170. if (off + n <= vstr.len) {
  171. // got a whole char in n bytes
  172. off += n;
  173. sz -= 1;
  174. last_buf_offset = off;
  175. if (off >= vstr.len) {
  176. more_bytes = sz;
  177. break;
  178. }
  179. } else {
  180. // didn't get a whole char, so work out how many extra bytes are needed for
  181. // this partial char, plus bytes for additional chars that we want
  182. more_bytes = (off + n - vstr.len) + (sz - 1);
  183. break;
  184. }
  185. }
  186. }
  187. return mp_obj_new_str_from_vstr(&vstr);
  188. }
  189. #endif
  190. vstr_t vstr;
  191. vstr_init_len(&vstr, sz);
  192. int error;
  193. mp_uint_t out_sz = mp_stream_rw(args[0], vstr.buf, sz, &error, flags);
  194. if (error != 0) {
  195. vstr_clear(&vstr);
  196. if (mp_is_nonblocking_error(error)) {
  197. // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
  198. // "If the object is in non-blocking mode and no bytes are available,
  199. // None is returned."
  200. // This is actually very weird, as naive truth check will treat
  201. // this as EOF.
  202. return mp_const_none;
  203. }
  204. mp_raise_OSError(error);
  205. } else {
  206. vstr.len = out_sz;
  207. if (stream_p->is_text) {
  208. return mp_obj_new_str_from_vstr(&vstr);
  209. } else {
  210. return mp_obj_new_bytes_from_vstr(&vstr);
  211. }
  212. }
  213. }
  214. static mp_obj_t stream_read(size_t n_args, const mp_obj_t *args) {
  215. return stream_read_generic(n_args, args, MP_STREAM_RW_READ);
  216. }
  217. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
  218. static mp_obj_t stream_read1(size_t n_args, const mp_obj_t *args) {
  219. return stream_read_generic(n_args, args, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);
  220. }
  221. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read1_obj, 1, 2, stream_read1);
  222. mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, size_t len, byte flags) {
  223. int error;
  224. mp_uint_t out_sz = mp_stream_rw(self_in, (void *)buf, len, &error, flags);
  225. if (error != 0) {
  226. if (mp_is_nonblocking_error(error)) {
  227. // http://docs.python.org/3/library/io.html#io.RawIOBase.write
  228. // "None is returned if the raw stream is set not to block and
  229. // no single byte could be readily written to it."
  230. return mp_const_none;
  231. }
  232. mp_raise_OSError(error);
  233. } else {
  234. return MP_OBJ_NEW_SMALL_INT(out_sz);
  235. }
  236. }
  237. // This is used to adapt a stream object to an mp_print_t interface
  238. void mp_stream_write_adaptor(void *self, const char *buf, size_t len) {
  239. mp_stream_write(MP_OBJ_FROM_PTR(self), buf, len, MP_STREAM_RW_WRITE);
  240. }
  241. static mp_obj_t stream_write_method(size_t n_args, const mp_obj_t *args) {
  242. mp_buffer_info_t bufinfo;
  243. mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
  244. size_t max_len = (size_t)-1;
  245. size_t off = 0;
  246. if (n_args == 3) {
  247. max_len = mp_obj_get_int_truncated(args[2]);
  248. } else if (n_args == 4) {
  249. off = mp_obj_get_int_truncated(args[2]);
  250. max_len = mp_obj_get_int_truncated(args[3]);
  251. if (off > bufinfo.len) {
  252. off = bufinfo.len;
  253. }
  254. }
  255. bufinfo.len -= off;
  256. return mp_stream_write(args[0], (byte *)bufinfo.buf + off, MIN(bufinfo.len, max_len), MP_STREAM_RW_WRITE);
  257. }
  258. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_write_obj, 2, 4, stream_write_method);
  259. static mp_obj_t stream_write1_method(mp_obj_t self_in, mp_obj_t arg) {
  260. mp_buffer_info_t bufinfo;
  261. mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
  262. return mp_stream_write(self_in, bufinfo.buf, bufinfo.len, MP_STREAM_RW_WRITE | MP_STREAM_RW_ONCE);
  263. }
  264. MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write1_obj, stream_write1_method);
  265. static mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
  266. mp_buffer_info_t bufinfo;
  267. mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
  268. // CPython extension: if 2nd arg is provided, that's max len to read,
  269. // instead of full buffer. Similar to
  270. // https://docs.python.org/3/library/socket.html#socket.socket.recv_into
  271. mp_uint_t len = bufinfo.len;
  272. if (n_args > 2) {
  273. len = mp_obj_get_int(args[2]);
  274. if (len > bufinfo.len) {
  275. len = bufinfo.len;
  276. }
  277. }
  278. int error;
  279. mp_uint_t out_sz = mp_stream_read_exactly(args[0], bufinfo.buf, len, &error);
  280. if (error != 0) {
  281. if (mp_is_nonblocking_error(error)) {
  282. return mp_const_none;
  283. }
  284. mp_raise_OSError(error);
  285. } else {
  286. return MP_OBJ_NEW_SMALL_INT(out_sz);
  287. }
  288. }
  289. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
  290. static mp_obj_t stream_readall(mp_obj_t self_in) {
  291. const mp_stream_p_t *stream_p = mp_get_stream(self_in);
  292. mp_uint_t total_size = 0;
  293. vstr_t vstr;
  294. vstr_init(&vstr, DEFAULT_BUFFER_SIZE);
  295. char *p = vstr.buf;
  296. mp_uint_t current_read = DEFAULT_BUFFER_SIZE;
  297. while (true) {
  298. int error;
  299. mp_uint_t out_sz = stream_p->read(self_in, p, current_read, &error);
  300. if (out_sz == MP_STREAM_ERROR) {
  301. if (mp_is_nonblocking_error(error)) {
  302. // With non-blocking streams, we read as much as we can.
  303. // If we read nothing, return None, just like read().
  304. // Otherwise, return data read so far.
  305. if (total_size == 0) {
  306. return mp_const_none;
  307. }
  308. break;
  309. }
  310. mp_raise_OSError(error);
  311. }
  312. if (out_sz == 0) {
  313. break;
  314. }
  315. total_size += out_sz;
  316. if (out_sz < current_read) {
  317. current_read -= out_sz;
  318. p += out_sz;
  319. } else {
  320. p = vstr_extend(&vstr, DEFAULT_BUFFER_SIZE);
  321. current_read = DEFAULT_BUFFER_SIZE;
  322. }
  323. }
  324. vstr.len = total_size;
  325. if (stream_p->is_text) {
  326. return mp_obj_new_str_from_vstr(&vstr);
  327. } else {
  328. return mp_obj_new_bytes_from_vstr(&vstr);
  329. }
  330. }
  331. // Unbuffered, inefficient implementation of readline() for raw I/O files.
  332. static mp_obj_t stream_unbuffered_readline(size_t n_args, const mp_obj_t *args) {
  333. const mp_stream_p_t *stream_p = mp_get_stream(args[0]);
  334. mp_int_t max_size = -1;
  335. if (n_args > 1) {
  336. max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
  337. }
  338. vstr_t vstr;
  339. if (max_size != -1) {
  340. vstr_init(&vstr, max_size);
  341. } else {
  342. vstr_init(&vstr, 16);
  343. }
  344. while (max_size == -1 || max_size-- != 0) {
  345. char *p = vstr_add_len(&vstr, 1);
  346. int error;
  347. mp_uint_t out_sz = stream_p->read(args[0], p, 1, &error);
  348. if (out_sz == MP_STREAM_ERROR) {
  349. if (mp_is_nonblocking_error(error)) {
  350. if (vstr.len == 1) {
  351. // We just incremented it, but otherwise we read nothing
  352. // and immediately got EAGAIN. This case is not well
  353. // specified in
  354. // https://docs.python.org/3/library/io.html#io.IOBase.readline
  355. // unlike similar case for read(). But we follow the latter's
  356. // behavior - return None.
  357. vstr_clear(&vstr);
  358. return mp_const_none;
  359. } else {
  360. goto done;
  361. }
  362. }
  363. mp_raise_OSError(error);
  364. }
  365. if (out_sz == 0) {
  366. done:
  367. // Back out previously added byte
  368. // Consider, what's better - read a char and get OutOfMemory (so read
  369. // char is lost), or allocate first as we do.
  370. vstr_cut_tail_bytes(&vstr, 1);
  371. break;
  372. }
  373. if (*p == '\n') {
  374. break;
  375. }
  376. }
  377. if (stream_p->is_text) {
  378. return mp_obj_new_str_from_vstr(&vstr);
  379. } else {
  380. return mp_obj_new_bytes_from_vstr(&vstr);
  381. }
  382. }
  383. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
  384. // TODO take an optional extra argument (what does it do exactly?)
  385. static mp_obj_t stream_unbuffered_readlines(mp_obj_t self) {
  386. mp_obj_t lines = mp_obj_new_list(0, NULL);
  387. for (;;) {
  388. mp_obj_t line = stream_unbuffered_readline(1, &self);
  389. if (!mp_obj_is_true(line)) {
  390. break;
  391. }
  392. mp_obj_list_append(lines, line);
  393. }
  394. return lines;
  395. }
  396. MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines);
  397. mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
  398. mp_obj_t l_in = stream_unbuffered_readline(1, &self);
  399. if (mp_obj_is_true(l_in)) {
  400. return l_in;
  401. }
  402. return MP_OBJ_STOP_ITERATION;
  403. }
  404. mp_obj_t mp_stream_close(mp_obj_t stream) {
  405. const mp_stream_p_t *stream_p = mp_get_stream(stream);
  406. int error;
  407. mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_CLOSE, 0, &error);
  408. if (res == MP_STREAM_ERROR) {
  409. mp_raise_OSError(error);
  410. }
  411. return mp_const_none;
  412. }
  413. MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_close_obj, mp_stream_close);
  414. static mp_obj_t mp_stream___exit__(size_t n_args, const mp_obj_t *args) {
  415. (void)n_args;
  416. return mp_stream_close(args[0]);
  417. }
  418. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream___exit___obj, 4, 4, mp_stream___exit__);
  419. static mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) {
  420. // TODO: Could be uint64
  421. mp_off_t offset = mp_obj_get_int(args[1]);
  422. int whence = SEEK_SET;
  423. if (n_args == 3) {
  424. whence = mp_obj_get_int(args[2]);
  425. }
  426. // In POSIX, it's error to seek before end of stream, we enforce it here.
  427. if (whence == SEEK_SET && offset < 0) {
  428. mp_raise_OSError(MP_EINVAL);
  429. }
  430. int error;
  431. mp_off_t res = mp_stream_seek(args[0], offset, whence, &error);
  432. if (res == (mp_off_t)-1) {
  433. mp_raise_OSError(error);
  434. }
  435. // TODO: Could be uint64
  436. return mp_obj_new_int_from_uint(res);
  437. }
  438. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj, 2, 3, stream_seek);
  439. static mp_obj_t stream_tell(mp_obj_t self) {
  440. mp_obj_t offset = MP_OBJ_NEW_SMALL_INT(0);
  441. mp_obj_t whence = MP_OBJ_NEW_SMALL_INT(SEEK_CUR);
  442. const mp_obj_t args[3] = {self, offset, whence};
  443. return stream_seek(3, args);
  444. }
  445. MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
  446. static mp_obj_t stream_flush(mp_obj_t self) {
  447. const mp_stream_p_t *stream_p = mp_get_stream(self);
  448. int error;
  449. mp_uint_t res = stream_p->ioctl(self, MP_STREAM_FLUSH, 0, &error);
  450. if (res == MP_STREAM_ERROR) {
  451. mp_raise_OSError(error);
  452. }
  453. return mp_const_none;
  454. }
  455. MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_flush_obj, stream_flush);
  456. static mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
  457. mp_buffer_info_t bufinfo;
  458. uintptr_t val = 0;
  459. if (n_args > 2) {
  460. if (mp_get_buffer(args[2], &bufinfo, MP_BUFFER_WRITE)) {
  461. val = (uintptr_t)bufinfo.buf;
  462. } else {
  463. val = mp_obj_get_int_truncated(args[2]);
  464. }
  465. }
  466. const mp_stream_p_t *stream_p = mp_get_stream(args[0]);
  467. int error;
  468. mp_uint_t res = stream_p->ioctl(args[0], mp_obj_get_int(args[1]), val, &error);
  469. if (res == MP_STREAM_ERROR) {
  470. mp_raise_OSError(error);
  471. }
  472. return mp_obj_new_int(res);
  473. }
  474. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj, 2, 3, stream_ioctl);
  475. #if MICROPY_STREAMS_POSIX_API
  476. /*
  477. * POSIX-like functions
  478. *
  479. * These functions have POSIX-compatible signature (except for "void *stream"
  480. * first argument instead of "int fd"). They are useful to port existing
  481. * POSIX-compatible software to work with MicroPython streams.
  482. */
  483. #include <errno.h>
  484. ssize_t mp_stream_posix_write(void *stream, const void *buf, size_t len) {
  485. mp_obj_base_t *o = stream;
  486. const mp_stream_p_t *stream_p = MP_OBJ_TYPE_GET_SLOT(o->type, protocol);
  487. mp_uint_t out_sz = stream_p->write(MP_OBJ_FROM_PTR(stream), buf, len, &errno);
  488. if (out_sz == MP_STREAM_ERROR) {
  489. return -1;
  490. } else {
  491. return out_sz;
  492. }
  493. }
  494. ssize_t mp_stream_posix_read(void *stream, void *buf, size_t len) {
  495. mp_obj_base_t *o = stream;
  496. const mp_stream_p_t *stream_p = MP_OBJ_TYPE_GET_SLOT(o->type, protocol);
  497. mp_uint_t out_sz = stream_p->read(MP_OBJ_FROM_PTR(stream), buf, len, &errno);
  498. if (out_sz == MP_STREAM_ERROR) {
  499. return -1;
  500. } else {
  501. return out_sz;
  502. }
  503. }
  504. off_t mp_stream_posix_lseek(void *stream, off_t offset, int whence) {
  505. mp_off_t res = mp_stream_seek(MP_OBJ_FROM_PTR(stream), offset, whence, &errno);
  506. if (res == (mp_off_t)-1) {
  507. return -1;
  508. }
  509. return res;
  510. }
  511. int mp_stream_posix_fsync(void *stream) {
  512. mp_obj_base_t *o = stream;
  513. const mp_stream_p_t *stream_p = MP_OBJ_TYPE_GET_SLOT(o->type, protocol);
  514. mp_uint_t res = stream_p->ioctl(MP_OBJ_FROM_PTR(stream), MP_STREAM_FLUSH, 0, &errno);
  515. if (res == MP_STREAM_ERROR) {
  516. return -1;
  517. }
  518. return res;
  519. }
  520. #endif