LCOV - code coverage report
Current view: top level - py - modio.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-315-g20a86eff5.info Lines: 69 70 98.6 %
Date: 2024-03-29 10:17:09 Functions: 8 8 100.0 %
Branches: 17 20 85.0 %

           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) 2013, 2014 Damien P. George
       7                 :            :  *
       8                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       9                 :            :  * of this software and associated documentation files (the "Software"), to deal
      10                 :            :  * in the Software without restriction, including without limitation the rights
      11                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      12                 :            :  * copies of the Software, and to permit persons to whom the Software is
      13                 :            :  * furnished to do so, subject to the following conditions:
      14                 :            :  *
      15                 :            :  * The above copyright notice and this permission notice shall be included in
      16                 :            :  * all copies or substantial portions of the Software.
      17                 :            :  *
      18                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      19                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      20                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      21                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      22                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      23                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      24                 :            :  * THE SOFTWARE.
      25                 :            :  */
      26                 :            : 
      27                 :            : #include <assert.h>
      28                 :            : #include <string.h>
      29                 :            : 
      30                 :            : #include "py/runtime.h"
      31                 :            : #include "py/builtin.h"
      32                 :            : #include "py/stream.h"
      33                 :            : #include "py/binary.h"
      34                 :            : #include "py/objarray.h"
      35                 :            : #include "py/objstringio.h"
      36                 :            : #include "py/frozenmod.h"
      37                 :            : 
      38                 :            : #if MICROPY_PY_IO
      39                 :            : 
      40                 :            : #if MICROPY_PY_IO_IOBASE
      41                 :            : 
      42                 :            : static const mp_obj_type_t mp_type_iobase;
      43                 :            : 
      44                 :            : static const mp_obj_base_t iobase_singleton = {&mp_type_iobase};
      45                 :            : 
      46                 :         94 : static mp_obj_t iobase_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
      47                 :         94 :     (void)type;
      48                 :         94 :     (void)n_args;
      49                 :         94 :     (void)n_kw;
      50                 :         94 :     (void)args;
      51                 :         94 :     return MP_OBJ_FROM_PTR(&iobase_singleton);
      52                 :            : }
      53                 :            : 
      54                 :        210 : static mp_uint_t iobase_read_write(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode, qstr qst) {
      55                 :        210 :     mp_obj_t dest[3];
      56                 :        210 :     mp_load_method(obj, qst, dest);
      57                 :        210 :     mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, size, buf};
      58                 :        210 :     dest[2] = MP_OBJ_FROM_PTR(&ar);
      59                 :        210 :     mp_obj_t ret_obj = mp_call_method_n_kw(1, 0, dest);
      60         [ +  + ]:        210 :     if (ret_obj == mp_const_none) {
      61                 :         14 :         *errcode = MP_EAGAIN;
      62                 :         14 :         return MP_STREAM_ERROR;
      63                 :            :     }
      64                 :        196 :     mp_int_t ret = mp_obj_get_int(ret_obj);
      65         [ +  + ]:        196 :     if (ret >= 0) {
      66                 :        182 :         return ret;
      67                 :            :     } else {
      68                 :         14 :         *errcode = -ret;
      69                 :         14 :         return MP_STREAM_ERROR;
      70                 :            :     }
      71                 :            : }
      72                 :        130 : static mp_uint_t iobase_read(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode) {
      73                 :        130 :     return iobase_read_write(obj, buf, size, errcode, MP_QSTR_readinto);
      74                 :            : }
      75                 :            : 
      76                 :         80 : static mp_uint_t iobase_write(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode) {
      77                 :         80 :     return iobase_read_write(obj, (void *)buf, size, errcode, MP_QSTR_write);
      78                 :            : }
      79                 :            : 
      80                 :        158 : static mp_uint_t iobase_ioctl(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode) {
      81                 :        158 :     mp_obj_t dest[4];
      82                 :        158 :     mp_load_method(obj, MP_QSTR_ioctl, dest);
      83                 :        158 :     dest[2] = mp_obj_new_int_from_uint(request);
      84                 :        158 :     dest[3] = mp_obj_new_int_from_uint(arg);
      85                 :        158 :     mp_int_t ret = mp_obj_get_int(mp_call_method_n_kw(2, 0, dest));
      86         [ +  + ]:        158 :     if (ret >= 0) {
      87                 :        142 :         return ret;
      88                 :            :     } else {
      89                 :         16 :         *errcode = -ret;
      90                 :         16 :         return MP_STREAM_ERROR;
      91                 :            :     }
      92                 :            : }
      93                 :            : 
      94                 :            : static const mp_stream_p_t iobase_p = {
      95                 :            :     .read = iobase_read,
      96                 :            :     .write = iobase_write,
      97                 :            :     .ioctl = iobase_ioctl,
      98                 :            : };
      99                 :            : 
     100                 :            : static MP_DEFINE_CONST_OBJ_TYPE(
     101                 :            :     mp_type_iobase,
     102                 :            :     MP_QSTR_IOBase,
     103                 :            :     MP_TYPE_FLAG_NONE,
     104                 :            :     make_new, iobase_make_new,
     105                 :            :     protocol, &iobase_p
     106                 :            :     );
     107                 :            : 
     108                 :            : #endif // MICROPY_PY_IO_IOBASE
     109                 :            : 
     110                 :            : #if MICROPY_PY_IO_BUFFEREDWRITER
     111                 :            : typedef struct _mp_obj_bufwriter_t {
     112                 :            :     mp_obj_base_t base;
     113                 :            :     mp_obj_t stream;
     114                 :            :     size_t alloc;
     115                 :            :     size_t len;
     116                 :            :     byte buf[0];
     117                 :            : } mp_obj_bufwriter_t;
     118                 :            : 
     119                 :         10 : static mp_obj_t bufwriter_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
     120                 :         10 :     mp_arg_check_num(n_args, n_kw, 2, 2, false);
     121                 :         10 :     size_t alloc = mp_obj_get_int(args[1]);
     122                 :         10 :     mp_obj_bufwriter_t *o = mp_obj_malloc_var(mp_obj_bufwriter_t, buf, byte, alloc, type);
     123                 :         10 :     o->stream = args[0];
     124                 :         10 :     o->alloc = alloc;
     125                 :         10 :     o->len = 0;
     126                 :         10 :     return o;
     127                 :            : }
     128                 :            : 
     129                 :         14 : static mp_uint_t bufwriter_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
     130                 :         14 :     mp_obj_bufwriter_t *self = MP_OBJ_TO_PTR(self_in);
     131                 :            : 
     132                 :         14 :     mp_uint_t org_size = size;
     133                 :            : 
     134         [ +  + ]:         30 :     while (size > 0) {
     135                 :         26 :         mp_uint_t rem = self->alloc - self->len;
     136         [ +  + ]:         26 :         if (size < rem) {
     137                 :          8 :             memcpy(self->buf + self->len, buf, size);
     138                 :          8 :             self->len += size;
     139                 :          8 :             return org_size;
     140                 :            :         }
     141                 :            : 
     142                 :            :         // Buffer flushing policy here is to flush entire buffer all the time.
     143                 :            :         // This allows e.g. to have a block device as backing storage and write
     144                 :            :         // entire block to it. memcpy below is not ideal and could be optimized
     145                 :            :         // in some cases. But the way it is now it at least ensures that buffer
     146                 :            :         // is word-aligned, to guard against obscure cases when it matters, e.g.
     147                 :            :         // https://github.com/micropython/micropython/issues/1863
     148                 :         18 :         memcpy(self->buf + self->len, buf, rem);
     149                 :         18 :         buf = (byte *)buf + rem;
     150                 :         18 :         size -= rem;
     151                 :         18 :         mp_uint_t out_sz = mp_stream_write_exactly(self->stream, self->buf, self->alloc, errcode);
     152                 :         18 :         (void)out_sz;
     153         [ +  + ]:         18 :         if (*errcode != 0) {
     154                 :            :             return MP_STREAM_ERROR;
     155                 :            :         }
     156                 :            :         // TODO: try to recover from a case of non-blocking stream, e.g. move
     157                 :            :         // remaining chunk to the beginning of buffer.
     158         [ -  + ]:         16 :         assert(out_sz == self->alloc);
     159                 :         16 :         self->len = 0;
     160                 :            :     }
     161                 :            : 
     162                 :            :     return org_size;
     163                 :            : }
     164                 :            : 
     165                 :          8 : static mp_obj_t bufwriter_flush(mp_obj_t self_in) {
     166                 :          8 :     mp_obj_bufwriter_t *self = MP_OBJ_TO_PTR(self_in);
     167                 :            : 
     168         [ +  + ]:          8 :     if (self->len != 0) {
     169                 :          4 :         int err;
     170                 :          4 :         mp_uint_t out_sz = mp_stream_write_exactly(self->stream, self->buf, self->len, &err);
     171                 :          4 :         (void)out_sz;
     172                 :            :         // TODO: try to recover from a case of non-blocking stream, e.g. move
     173                 :            :         // remaining chunk to the beginning of buffer.
     174         [ -  + ]:          4 :         assert(out_sz == self->len);
     175                 :          4 :         self->len = 0;
     176         [ -  + ]:          4 :         if (err != 0) {
     177                 :          0 :             mp_raise_OSError(err);
     178                 :            :         }
     179                 :            :     }
     180                 :            : 
     181                 :          8 :     return mp_const_none;
     182                 :            : }
     183                 :            : static MP_DEFINE_CONST_FUN_OBJ_1(bufwriter_flush_obj, bufwriter_flush);
     184                 :            : 
     185                 :            : static const mp_rom_map_elem_t bufwriter_locals_dict_table[] = {
     186                 :            :     { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
     187                 :            :     { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&bufwriter_flush_obj) },
     188                 :            : };
     189                 :            : static MP_DEFINE_CONST_DICT(bufwriter_locals_dict, bufwriter_locals_dict_table);
     190                 :            : 
     191                 :            : static const mp_stream_p_t bufwriter_stream_p = {
     192                 :            :     .write = bufwriter_write,
     193                 :            : };
     194                 :            : 
     195                 :            : static MP_DEFINE_CONST_OBJ_TYPE(
     196                 :            :     mp_type_bufwriter,
     197                 :            :     MP_QSTR_BufferedWriter,
     198                 :            :     MP_TYPE_FLAG_NONE,
     199                 :            :     make_new, bufwriter_make_new,
     200                 :            :     protocol, &bufwriter_stream_p,
     201                 :            :     locals_dict, &bufwriter_locals_dict
     202                 :            :     );
     203                 :            : #endif // MICROPY_PY_IO_BUFFEREDWRITER
     204                 :            : 
     205                 :            : static const mp_rom_map_elem_t mp_module_io_globals_table[] = {
     206                 :            :     { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_io) },
     207                 :            :     // Note: mp_builtin_open_obj should be defined by port, it's not
     208                 :            :     // part of the core.
     209                 :            :     { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
     210                 :            :     #if MICROPY_PY_IO_IOBASE
     211                 :            :     { MP_ROM_QSTR(MP_QSTR_IOBase), MP_ROM_PTR(&mp_type_iobase) },
     212                 :            :     #endif
     213                 :            :     { MP_ROM_QSTR(MP_QSTR_StringIO), MP_ROM_PTR(&mp_type_stringio) },
     214                 :            :     #if MICROPY_PY_IO_BYTESIO
     215                 :            :     { MP_ROM_QSTR(MP_QSTR_BytesIO), MP_ROM_PTR(&mp_type_bytesio) },
     216                 :            :     #endif
     217                 :            :     #if MICROPY_PY_IO_BUFFEREDWRITER
     218                 :            :     { MP_ROM_QSTR(MP_QSTR_BufferedWriter), MP_ROM_PTR(&mp_type_bufwriter) },
     219                 :            :     #endif
     220                 :            : };
     221                 :            : 
     222                 :            : static MP_DEFINE_CONST_DICT(mp_module_io_globals, mp_module_io_globals_table);
     223                 :            : 
     224                 :            : const mp_obj_module_t mp_module_io = {
     225                 :            :     .base = { &mp_type_module },
     226                 :            :     .globals = (mp_obj_dict_t *)&mp_module_io_globals,
     227                 :            : };
     228                 :            : 
     229                 :            : MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_io, mp_module_io);
     230                 :            : 
     231                 :            : #endif

Generated by: LCOV version 1.15-5-g462f71d