LCOV - code coverage report
Current view: top level - py - moderrno.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-344-ge60e8079a.info Lines: 4 4 100.0 %
Date: 2024-04-26 14:58:11 Functions: 1 1 100.0 %
Branches: 2 2 100.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) 2016 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/obj.h"
      31                 :            : #include "py/mperrno.h"
      32                 :            : 
      33                 :            : #if MICROPY_PY_ERRNO
      34                 :            : 
      35                 :            : // This list can be defined per port in mpconfigport.h to tailor it to a
      36                 :            : // specific port's needs.  If it's not defined then we provide a default.
      37                 :            : #ifndef MICROPY_PY_ERRNO_LIST
      38                 :            : #define MICROPY_PY_ERRNO_LIST \
      39                 :            :     X(EPERM) \
      40                 :            :     X(ENOENT) \
      41                 :            :     X(EIO) \
      42                 :            :     X(EBADF) \
      43                 :            :     X(EAGAIN) \
      44                 :            :     X(ENOMEM) \
      45                 :            :     X(EACCES) \
      46                 :            :     X(EEXIST) \
      47                 :            :     X(ENODEV) \
      48                 :            :     X(EISDIR) \
      49                 :            :     X(EINVAL) \
      50                 :            :     X(EOPNOTSUPP) \
      51                 :            :     X(EADDRINUSE) \
      52                 :            :     X(ECONNABORTED) \
      53                 :            :     X(ECONNRESET) \
      54                 :            :     X(ENOBUFS) \
      55                 :            :     X(ENOTCONN) \
      56                 :            :     X(ETIMEDOUT) \
      57                 :            :     X(ECONNREFUSED) \
      58                 :            :     X(EHOSTUNREACH) \
      59                 :            :     X(EALREADY) \
      60                 :            :     X(EINPROGRESS) \
      61                 :            : 
      62                 :            : #endif
      63                 :            : 
      64                 :            : #if MICROPY_PY_ERRNO_ERRORCODE
      65                 :            : static const mp_rom_map_elem_t errorcode_table[] = {
      66                 :            :     #define X(e) { MP_ROM_INT(MP_##e), MP_ROM_QSTR(MP_QSTR_##e) },
      67                 :            :     MICROPY_PY_ERRNO_LIST
      68                 :            : #undef X
      69                 :            : };
      70                 :            : 
      71                 :            : static const mp_obj_dict_t errorcode_dict = {
      72                 :            :     .base = {&mp_type_dict},
      73                 :            :     .map = {
      74                 :            :         .all_keys_are_qstrs = 0, // keys are integers
      75                 :            :         .is_fixed = 1,
      76                 :            :         .is_ordered = 1,
      77                 :            :         .used = MP_ARRAY_SIZE(errorcode_table),
      78                 :            :         .alloc = MP_ARRAY_SIZE(errorcode_table),
      79                 :            :         .table = (mp_map_elem_t *)(mp_rom_map_elem_t *)errorcode_table,
      80                 :            :     },
      81                 :            : };
      82                 :            : #endif
      83                 :            : 
      84                 :            : static const mp_rom_map_elem_t mp_module_errno_globals_table[] = {
      85                 :            :     { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_errno) },
      86                 :            :     #if MICROPY_PY_ERRNO_ERRORCODE
      87                 :            :     { MP_ROM_QSTR(MP_QSTR_errorcode), MP_ROM_PTR(&errorcode_dict) },
      88                 :            :     #endif
      89                 :            : 
      90                 :            :     #define X(e) { MP_ROM_QSTR(MP_QSTR_##e), MP_ROM_INT(MP_##e) },
      91                 :            :     MICROPY_PY_ERRNO_LIST
      92                 :            : #undef X
      93                 :            : };
      94                 :            : 
      95                 :            : static MP_DEFINE_CONST_DICT(mp_module_errno_globals, mp_module_errno_globals_table);
      96                 :            : 
      97                 :            : const mp_obj_module_t mp_module_errno = {
      98                 :            :     .base = { &mp_type_module },
      99                 :            :     .globals = (mp_obj_dict_t *)&mp_module_errno_globals,
     100                 :            : };
     101                 :            : 
     102                 :            : MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_errno, mp_module_errno);
     103                 :            : 
     104                 :         14 : qstr mp_errno_to_str(mp_obj_t errno_val) {
     105                 :            :     #if MICROPY_PY_ERRNO_ERRORCODE
     106                 :            :     // We have the errorcode dict so can do a lookup using the hash map
     107                 :         14 :     mp_map_elem_t *elem = mp_map_lookup((mp_map_t *)&errorcode_dict.map, errno_val, MP_MAP_LOOKUP);
     108         [ +  + ]:         14 :     if (elem == NULL) {
     109                 :            :         return MP_QSTRnull;
     110                 :            :     } else {
     111                 :          8 :         return MP_OBJ_QSTR_VALUE(elem->value);
     112                 :            :     }
     113                 :            :     #else
     114                 :            :     // We don't have the errorcode dict so do a simple search in the modules dict
     115                 :            :     for (size_t i = 0; i < MP_ARRAY_SIZE(mp_module_errno_globals_table); ++i) {
     116                 :            :         if (errno_val == mp_module_errno_globals_table[i].value) {
     117                 :            :             return MP_OBJ_QSTR_VALUE(mp_module_errno_globals_table[i].key);
     118                 :            :         }
     119                 :            :     }
     120                 :            :     return MP_QSTRnull;
     121                 :            :     #endif
     122                 :            : }
     123                 :            : 
     124                 :            : #endif // MICROPY_PY_ERRNO

Generated by: LCOV version 1.15-5-g462f71d