LCOV - code coverage report
Current view: top level - py - objattrtuple.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-325-gd11ca092f.info Lines: 31 31 100.0 %
Date: 2024-04-17 08:48:33 Functions: 4 4 100.0 %
Branches: 10 12 83.3 %

           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) 2015 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 "py/objtuple.h"
      28                 :            : 
      29                 :            : #if MICROPY_PY_ATTRTUPLE || MICROPY_PY_COLLECTIONS
      30                 :            : 
      31                 :            : // this helper function is used by collections.namedtuple
      32                 :            : #if !MICROPY_PY_COLLECTIONS
      33                 :            : static
      34                 :            : #endif
      35                 :         22 : void mp_obj_attrtuple_print_helper(const mp_print_t *print, const qstr *fields, mp_obj_tuple_t *o) {
      36                 :         22 :     mp_print_str(print, "(");
      37         [ +  + ]:         68 :     for (size_t i = 0; i < o->len; i++) {
      38         [ +  + ]:         46 :         if (i > 0) {
      39                 :         28 :             mp_print_str(print, ", ");
      40                 :            :         }
      41                 :         46 :         mp_printf(print, "%q=", fields[i]);
      42                 :         46 :         mp_obj_print_helper(print, o->items[i], PRINT_REPR);
      43                 :            :     }
      44                 :         22 :     mp_print_str(print, ")");
      45                 :         22 : }
      46                 :            : 
      47                 :            : #endif
      48                 :            : 
      49                 :            : #if MICROPY_PY_ATTRTUPLE
      50                 :            : 
      51                 :          6 : static void mp_obj_attrtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
      52                 :          6 :     (void)kind;
      53                 :          6 :     mp_obj_tuple_t *o = MP_OBJ_TO_PTR(o_in);
      54                 :          6 :     const qstr *fields = (const qstr *)MP_OBJ_TO_PTR(o->items[o->len]);
      55                 :          6 :     mp_obj_attrtuple_print_helper(print, fields, o);
      56                 :          6 : }
      57                 :            : 
      58                 :         47 : static void mp_obj_attrtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
      59         [ +  - ]:         47 :     if (dest[0] == MP_OBJ_NULL) {
      60                 :            :         // load attribute
      61                 :         47 :         mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
      62                 :         47 :         size_t len = self->len;
      63                 :         47 :         const qstr *fields = (const qstr *)MP_OBJ_TO_PTR(self->items[len]);
      64         [ +  - ]:        119 :         for (size_t i = 0; i < len; i++) {
      65         [ +  + ]:        119 :             if (fields[i] == attr) {
      66                 :         47 :                 dest[0] = self->items[i];
      67                 :         47 :                 return;
      68                 :            :             }
      69                 :            :         }
      70                 :            :     }
      71                 :            : }
      72                 :            : 
      73                 :          2 : mp_obj_t mp_obj_new_attrtuple(const qstr *fields, size_t n, const mp_obj_t *items) {
      74                 :          2 :     mp_obj_tuple_t *o = mp_obj_malloc_var(mp_obj_tuple_t, items, mp_obj_t, n + 1, &mp_type_attrtuple);
      75                 :          2 :     o->len = n;
      76         [ +  + ]:          8 :     for (size_t i = 0; i < n; i++) {
      77                 :          6 :         o->items[i] = items[i];
      78                 :            :     }
      79                 :          2 :     o->items[n] = MP_OBJ_FROM_PTR(fields);
      80                 :          2 :     return MP_OBJ_FROM_PTR(o);
      81                 :            : }
      82                 :            : 
      83                 :            : MP_DEFINE_CONST_OBJ_TYPE(
      84                 :            :     mp_type_attrtuple,
      85                 :            :     MP_QSTR_tuple,
      86                 :            :     MP_TYPE_FLAG_ITER_IS_GETITER,
      87                 :            :     // reuse tuple to save on a qstr
      88                 :            :     print, mp_obj_attrtuple_print,
      89                 :            :     unary_op, mp_obj_tuple_unary_op,
      90                 :            :     binary_op, mp_obj_tuple_binary_op,
      91                 :            :     attr, mp_obj_attrtuple_attr,
      92                 :            :     subscr, mp_obj_tuple_subscr,
      93                 :            :     iter, mp_obj_tuple_getiter
      94                 :            :     );
      95                 :            : 
      96                 :            : #endif // MICROPY_PY_ATTRTUPLE

Generated by: LCOV version 1.15-5-g462f71d