LCOV - code coverage report
Current view: top level - py - argcheck.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-325-gd11ca092f.info Lines: 53 55 96.4 %
Date: 2024-04-17 08:48:33 Functions: 4 5 80.0 %
Branches: 29 30 96.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) 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                 :    2635910 : void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig) {
      33                 :            :     // TODO maybe take the function name as an argument so we can print nicer error messages
      34                 :            : 
      35                 :            :     // The reverse of MP_OBJ_FUN_MAKE_SIG
      36                 :    2635910 :     bool takes_kw = sig & 1;
      37                 :    2635910 :     size_t n_args_min = sig >> 17;
      38                 :    2635910 :     size_t n_args_max = (sig >> 1) & 0xffff;
      39                 :            : 
      40         [ +  + ]:    2635910 :     if (n_kw && !takes_kw) {
      41                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
      42                 :            :         mp_arg_error_terse_mismatch();
      43                 :            :         #else
      44                 :         32 :         mp_raise_TypeError(MP_ERROR_TEXT("function doesn't take keyword arguments"));
      45                 :            :         #endif
      46                 :            :     }
      47                 :            : 
      48         [ +  + ]:    2635878 :     if (n_args_min == n_args_max) {
      49         [ +  + ]:    1393051 :         if (n_args != n_args_min) {
      50                 :            :             #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
      51                 :            :             mp_arg_error_terse_mismatch();
      52                 :            :             #else
      53                 :         12 :             mp_raise_msg_varg(&mp_type_TypeError,
      54                 :         12 :                 MP_ERROR_TEXT("function takes %d positional arguments but %d were given"),
      55                 :            :                 n_args_min, n_args);
      56                 :            :             #endif
      57                 :            :         }
      58                 :            :     } else {
      59         [ +  + ]:    1242827 :         if (n_args < n_args_min) {
      60                 :            :             #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
      61                 :            :             mp_arg_error_terse_mismatch();
      62                 :            :             #else
      63                 :         12 :             mp_raise_msg_varg(&mp_type_TypeError,
      64                 :         12 :                 MP_ERROR_TEXT("function missing %d required positional arguments"),
      65                 :            :                 n_args_min - n_args);
      66                 :            :             #endif
      67         [ +  + ]:    1242815 :         } else if (n_args > n_args_max) {
      68                 :            :             #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
      69                 :            :             mp_arg_error_terse_mismatch();
      70                 :            :             #else
      71                 :          8 :             mp_raise_msg_varg(&mp_type_TypeError,
      72                 :          8 :                 MP_ERROR_TEXT("function expected at most %d arguments, got %d"),
      73                 :            :                 n_args_max, n_args);
      74                 :            :             #endif
      75                 :            :         }
      76                 :            :     }
      77                 :    2635846 : }
      78                 :            : 
      79                 :     216916 : void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals) {
      80                 :     216916 :     size_t pos_found = 0, kws_found = 0;
      81         [ +  + ]:     840965 :     for (size_t i = 0; i < n_allowed; i++) {
      82                 :     624057 :         mp_obj_t given_arg;
      83         [ +  + ]:     624057 :         if (i < n_pos) {
      84         [ +  + ]:      14858 :             if (allowed[i].flags & MP_ARG_KW_ONLY) {
      85                 :          4 :                 goto extra_positional;
      86                 :            :             }
      87                 :      14854 :             pos_found++;
      88                 :      14854 :             given_arg = pos[i];
      89                 :            :         } else {
      90                 :     609199 :             mp_map_elem_t *kw = mp_map_lookup(kws, MP_OBJ_NEW_QSTR(allowed[i].qst), MP_MAP_LOOKUP);
      91         [ +  + ]:     609199 :             if (kw == NULL) {
      92         [ +  + ]:     522444 :                 if (allowed[i].flags & MP_ARG_REQUIRED) {
      93                 :            :                     #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
      94                 :            :                     mp_arg_error_terse_mismatch();
      95                 :            :                     #else
      96                 :          4 :                     mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("'%q' argument required"), allowed[i].qst);
      97                 :            :                     #endif
      98                 :            :                 }
      99                 :     522440 :                 out_vals[i] = allowed[i].defval;
     100                 :     522440 :                 continue;
     101                 :            :             } else {
     102                 :      86755 :                 kws_found++;
     103                 :      86755 :                 given_arg = kw->value;
     104                 :            :             }
     105                 :            :         }
     106         [ +  + ]:     101609 :         if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_BOOL) {
     107                 :        124 :             out_vals[i].u_bool = mp_obj_is_true(given_arg);
     108         [ +  + ]:     101485 :         } else if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_INT) {
     109                 :         30 :             out_vals[i].u_int = mp_obj_get_int(given_arg);
     110                 :            :         } else {
     111         [ -  + ]:     101455 :             assert((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_OBJ);
     112                 :     101455 :             out_vals[i].u_obj = given_arg;
     113                 :            :         }
     114                 :            :     }
     115         [ +  + ]:     216908 :     if (pos_found < n_pos) {
     116                 :          4 :     extra_positional:
     117                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     118                 :            :         mp_arg_error_terse_mismatch();
     119                 :            :         #else
     120                 :            :         // TODO better error message
     121                 :          8 :         mp_raise_TypeError(MP_ERROR_TEXT("extra positional arguments given"));
     122                 :            :         #endif
     123                 :            :     }
     124         [ +  + ]:     216904 :     if (kws_found < kws->used) {
     125                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     126                 :            :         mp_arg_error_terse_mismatch();
     127                 :            :         #else
     128                 :            :         // TODO better error message
     129                 :         20 :         mp_raise_TypeError(MP_ERROR_TEXT("extra keyword arguments given"));
     130                 :            :         #endif
     131                 :            :     }
     132                 :     216884 : }
     133                 :            : 
     134                 :       7402 : void mp_arg_parse_all_kw_array(size_t n_pos, size_t n_kw, const mp_obj_t *args, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals) {
     135                 :       7402 :     mp_map_t kw_args;
     136                 :       7402 :     mp_map_init_fixed_table(&kw_args, n_kw, args + n_pos);
     137                 :       7402 :     mp_arg_parse_all(n_pos, args, &kw_args, n_allowed, allowed, out_vals);
     138                 :       7394 : }
     139                 :            : 
     140                 :          0 : NORETURN void mp_arg_error_terse_mismatch(void) {
     141                 :          0 :     mp_raise_TypeError(MP_ERROR_TEXT("argument num/types mismatch"));
     142                 :            : }
     143                 :            : 
     144                 :            : #if MICROPY_CPYTHON_COMPAT
     145                 :          4 : NORETURN void mp_arg_error_unimpl_kw(void) {
     146                 :          4 :     mp_raise_NotImplementedError(MP_ERROR_TEXT("keyword argument(s) not implemented - use normal args instead"));
     147                 :            : }
     148                 :            : #endif

Generated by: LCOV version 1.15-5-g462f71d