LCOV - code coverage report
Current view: top level - py - objproperty.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.24.0-161-gc73204128.info Lines: 29 29 100.0 %
Date: 2024-12-23 07:29:41 Functions: 5 5 100.0 %
Branches: 0 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 <stdlib.h>
      28                 :            : #include <assert.h>
      29                 :            : 
      30                 :            : #include "py/runtime.h"
      31                 :            : 
      32                 :            : #if MICROPY_PY_BUILTINS_PROPERTY
      33                 :            : 
      34                 :            : typedef struct _mp_obj_property_t {
      35                 :            :     mp_obj_base_t base;
      36                 :            :     mp_obj_t proxy[3]; // getter, setter, deleter
      37                 :            : } mp_obj_property_t;
      38                 :            : 
      39                 :         72 : static mp_obj_t property_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
      40                 :         72 :     enum { ARG_fget, ARG_fset, ARG_fdel, ARG_doc };
      41                 :         72 :     static const mp_arg_t allowed_args[] = {
      42                 :            :         { MP_QSTR_, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
      43                 :            :         { MP_QSTR_, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
      44                 :            :         { MP_QSTR_, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
      45                 :            :         { MP_QSTR_doc, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
      46                 :            :     };
      47                 :         72 :     mp_arg_val_t vals[MP_ARRAY_SIZE(allowed_args)];
      48                 :         72 :     mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, vals);
      49                 :            : 
      50                 :         72 :     mp_obj_property_t *o = mp_obj_malloc(mp_obj_property_t, type);
      51                 :         72 :     o->proxy[0] = vals[ARG_fget].u_obj;
      52                 :         72 :     o->proxy[1] = vals[ARG_fset].u_obj;
      53                 :         72 :     o->proxy[2] = vals[ARG_fdel].u_obj;
      54                 :            :     // vals[ARG_doc] is silently discarded
      55                 :         72 :     return MP_OBJ_FROM_PTR(o);
      56                 :            : }
      57                 :            : 
      58                 :          4 : static mp_obj_t property_getter(mp_obj_t self_in, mp_obj_t getter) {
      59                 :          4 :     mp_obj_property_t *p2 = m_new_obj(mp_obj_property_t);
      60                 :          4 :     *p2 = *(mp_obj_property_t *)MP_OBJ_TO_PTR(self_in);
      61                 :          4 :     p2->proxy[0] = getter;
      62                 :          4 :     return MP_OBJ_FROM_PTR(p2);
      63                 :            : }
      64                 :            : 
      65                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(property_getter_obj, property_getter);
      66                 :            : 
      67                 :         26 : static mp_obj_t property_setter(mp_obj_t self_in, mp_obj_t setter) {
      68                 :         26 :     mp_obj_property_t *p2 = m_new_obj(mp_obj_property_t);
      69                 :         26 :     *p2 = *(mp_obj_property_t *)MP_OBJ_TO_PTR(self_in);
      70                 :         26 :     p2->proxy[1] = setter;
      71                 :         26 :     return MP_OBJ_FROM_PTR(p2);
      72                 :            : }
      73                 :            : 
      74                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(property_setter_obj, property_setter);
      75                 :            : 
      76                 :          8 : static mp_obj_t property_deleter(mp_obj_t self_in, mp_obj_t deleter) {
      77                 :          8 :     mp_obj_property_t *p2 = m_new_obj(mp_obj_property_t);
      78                 :          8 :     *p2 = *(mp_obj_property_t *)MP_OBJ_TO_PTR(self_in);
      79                 :          8 :     p2->proxy[2] = deleter;
      80                 :          8 :     return MP_OBJ_FROM_PTR(p2);
      81                 :            : }
      82                 :            : 
      83                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(property_deleter_obj, property_deleter);
      84                 :            : 
      85                 :            : static const mp_rom_map_elem_t property_locals_dict_table[] = {
      86                 :            :     { MP_ROM_QSTR(MP_QSTR_getter), MP_ROM_PTR(&property_getter_obj) },
      87                 :            :     { MP_ROM_QSTR(MP_QSTR_setter), MP_ROM_PTR(&property_setter_obj) },
      88                 :            :     { MP_ROM_QSTR(MP_QSTR_deleter), MP_ROM_PTR(&property_deleter_obj) },
      89                 :            : };
      90                 :            : 
      91                 :            : static MP_DEFINE_CONST_DICT(property_locals_dict, property_locals_dict_table);
      92                 :            : 
      93                 :            : MP_DEFINE_CONST_OBJ_TYPE(
      94                 :            :     mp_type_property,
      95                 :            :     MP_QSTR_property,
      96                 :            :     MP_TYPE_FLAG_NONE,
      97                 :            :     make_new, property_make_new,
      98                 :            :     locals_dict, &property_locals_dict
      99                 :            :     );
     100                 :            : 
     101                 :        120 : const mp_obj_t *mp_obj_property_get(mp_obj_t self_in) {
     102                 :        120 :     mp_check_self(mp_obj_is_type(self_in, &mp_type_property));
     103                 :        120 :     mp_obj_property_t *self = MP_OBJ_TO_PTR(self_in);
     104                 :        120 :     return self->proxy;
     105                 :            : }
     106                 :            : 
     107                 :            : #endif // MICROPY_PY_BUILTINS_PROPERTY

Generated by: LCOV version 1.15-5-g462f71d