LCOV - code coverage report
Current view: top level - py - stream.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.20.0-528-gfbe58553c.info Lines: 271 273 99.3 %
Date: 2023-09-29 18:18:57 Functions: 24 24 100.0 %
Branches: 139 150 92.7 %

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

Generated by: LCOV version 1.15-5-g462f71d