LCOV - code coverage report
Current view: top level - py - obj.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-325-gd11ca092f.info Lines: 229 233 98.3 %
Date: 2024-04-17 08:48:33 Functions: 27 27 100.0 %
Branches: 224 264 84.8 %

           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 <stdint.h>
      28                 :            : #include <stdio.h>
      29                 :            : #include <stdarg.h>
      30                 :            : #include <assert.h>
      31                 :            : 
      32                 :            : #include "py/obj.h"
      33                 :            : #include "py/objtype.h"
      34                 :            : #include "py/objint.h"
      35                 :            : #include "py/objstr.h"
      36                 :            : #include "py/runtime.h"
      37                 :            : #include "py/stackctrl.h"
      38                 :            : #include "py/stream.h" // for mp_obj_print
      39                 :            : 
      40                 :            : // Allocates an object and also sets type, for mp_obj_malloc{,_var} macros.
      41                 :     554237 : MP_NOINLINE void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type) {
      42                 :     554237 :     mp_obj_base_t *base = (mp_obj_base_t *)m_malloc(num_bytes);
      43                 :     554221 :     base->type = type;
      44                 :     554221 :     return base;
      45                 :            : }
      46                 :            : 
      47                 :            : #if MICROPY_ENABLE_FINALISER
      48                 :            : // Allocates an object and also sets type, for mp_obj_malloc{,_var}_with_finaliser macros.
      49                 :       4270 : MP_NOINLINE void *mp_obj_malloc_with_finaliser_helper(size_t num_bytes, const mp_obj_type_t *type) {
      50                 :       4270 :     mp_obj_base_t *base = (mp_obj_base_t *)m_malloc_with_finaliser(num_bytes);
      51                 :       4268 :     base->type = type;
      52                 :       4268 :     return base;
      53                 :            : }
      54                 :            : #endif
      55                 :            : 
      56                 :   46814666 : const mp_obj_type_t *MICROPY_WRAP_MP_OBJ_GET_TYPE(mp_obj_get_type)(mp_const_obj_t o_in) {
      57                 :            :     #if MICROPY_OBJ_IMMEDIATE_OBJS && MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
      58                 :            : 
      59         [ +  + ]:   46814666 :     if (mp_obj_is_obj(o_in)) {
      60                 :   46274927 :         const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
      61                 :   46274927 :         return o->type;
      62                 :            :     } else {
      63                 :     539739 :         static const mp_obj_type_t *const types[] = {
      64                 :            :             NULL, &mp_type_int, &mp_type_str, &mp_type_int,
      65                 :            :             NULL, &mp_type_int, &mp_type_NoneType, &mp_type_int,
      66                 :            :             NULL, &mp_type_int, &mp_type_str, &mp_type_int,
      67                 :            :             NULL, &mp_type_int, &mp_type_bool, &mp_type_int,
      68                 :            :         };
      69                 :     539739 :         return types[(uintptr_t)o_in & 0xf];
      70                 :            :     }
      71                 :            : 
      72                 :            :     #elif MICROPY_OBJ_IMMEDIATE_OBJS && MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
      73                 :            : 
      74                 :            :     if (mp_obj_is_small_int(o_in)) {
      75                 :            :         return &mp_type_int;
      76                 :            :     } else if (mp_obj_is_obj(o_in)) {
      77                 :            :         const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
      78                 :            :         return o->type;
      79                 :            :     #if MICROPY_PY_BUILTINS_FLOAT
      80                 :            :     } else if ((((mp_uint_t)(o_in)) & 0xff800007) != 0x00000006) {
      81                 :            :         return &mp_type_float;
      82                 :            :     #endif
      83                 :            :     } else {
      84                 :            :         static const mp_obj_type_t *const types[] = {
      85                 :            :             &mp_type_str, &mp_type_NoneType, &mp_type_str, &mp_type_bool,
      86                 :            :         };
      87                 :            :         return types[((uintptr_t)o_in >> 3) & 3];
      88                 :            :     }
      89                 :            : 
      90                 :            :     #else
      91                 :            : 
      92                 :            :     if (mp_obj_is_small_int(o_in)) {
      93                 :            :         return &mp_type_int;
      94                 :            :     } else if (mp_obj_is_qstr(o_in)) {
      95                 :            :         return &mp_type_str;
      96                 :            :         #if MICROPY_PY_BUILTINS_FLOAT && ( \
      97                 :            :             MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D)
      98                 :            :     } else if (mp_obj_is_float(o_in)) {
      99                 :            :         return &mp_type_float;
     100                 :            :         #endif
     101                 :            :     #if MICROPY_OBJ_IMMEDIATE_OBJS
     102                 :            :     } else if (mp_obj_is_immediate_obj(o_in)) {
     103                 :            :         static const mp_obj_type_t *const types[2] = {&mp_type_NoneType, &mp_type_bool};
     104                 :            :         return types[MP_OBJ_IMMEDIATE_OBJ_VALUE(o_in) & 1];
     105                 :            :     #endif
     106                 :            :     } else {
     107                 :            :         const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in);
     108                 :            :         return o->type;
     109                 :            :     }
     110                 :            : 
     111                 :            :     #endif
     112                 :            : }
     113                 :            : 
     114                 :        806 : const char *mp_obj_get_type_str(mp_const_obj_t o_in) {
     115                 :        806 :     return qstr_str(mp_obj_get_type(o_in)->name);
     116                 :            : }
     117                 :            : 
     118                 :     417249 : void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
     119                 :            :     // There can be data structures nested too deep, or just recursive
     120                 :     417249 :     MP_STACK_CHECK();
     121                 :            :     #ifndef NDEBUG
     122         [ -  + ]:     417247 :     if (o_in == MP_OBJ_NULL) {
     123                 :          0 :         mp_print_str(print, "(nil)");
     124                 :          0 :         return;
     125                 :            :     }
     126                 :            :     #endif
     127                 :     417247 :     const mp_obj_type_t *type = mp_obj_get_type(o_in);
     128         [ +  + ]:     417247 :     if (MP_OBJ_TYPE_HAS_SLOT(type, print)) {
     129                 :     417153 :         MP_OBJ_TYPE_GET_SLOT(type, print)((mp_print_t *)print, o_in, kind);
     130                 :            :     } else {
     131                 :         94 :         mp_printf(print, "<%q>", type->name);
     132                 :            :     }
     133                 :            : }
     134                 :            : 
     135                 :        160 : void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
     136                 :        160 :     mp_obj_print_helper(MP_PYTHON_PRINTER, o_in, kind);
     137                 :        160 : }
     138                 :            : 
     139                 :            : // helper function to print an exception with traceback
     140                 :         50 : void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
     141         [ +  - ]:         50 :     if (mp_obj_is_exception_instance(exc)) {
     142                 :         50 :         size_t n, *values;
     143                 :         50 :         mp_obj_exception_get_traceback(exc, &n, &values);
     144         [ +  + ]:         50 :         if (n > 0) {
     145         [ -  + ]:         28 :             assert(n % 3 == 0);
     146                 :         28 :             mp_print_str(print, "Traceback (most recent call last):\n");
     147         [ +  + ]:         85 :             for (int i = n - 3; i >= 0; i -= 3) {
     148                 :            :                 #if MICROPY_ENABLE_SOURCE_LINE
     149                 :         57 :                 mp_printf(print, "  File \"%q\", line %d", values[i], (int)values[i + 1]);
     150                 :            :                 #else
     151                 :            :                 mp_printf(print, "  File \"%q\"", values[i]);
     152                 :            :                 #endif
     153                 :            :                 // the block name can be NULL if it's unknown
     154                 :         57 :                 qstr block = values[i + 2];
     155         [ -  + ]:         57 :                 if (block == MP_QSTRnull) {
     156                 :          0 :                     mp_print_str(print, "\n");
     157                 :            :                 } else {
     158                 :         57 :                     mp_printf(print, ", in %q\n", block);
     159                 :            :                 }
     160                 :            :             }
     161                 :            :         }
     162                 :            :     }
     163                 :         50 :     mp_obj_print_helper(print, exc, PRINT_EXC);
     164                 :         50 :     mp_print_str(print, "\n");
     165                 :         50 : }
     166                 :            : 
     167                 :   14747913 : bool mp_obj_is_true(mp_obj_t arg) {
     168         [ +  + ]:   14747913 :     if (arg == mp_const_false) {
     169                 :            :         return 0;
     170         [ +  + ]:   12611707 :     } else if (arg == mp_const_true) {
     171                 :            :         return 1;
     172         [ +  + ]:    5186130 :     } else if (arg == mp_const_none) {
     173                 :            :         return 0;
     174         [ +  + ]:    5032437 :     } else if (mp_obj_is_small_int(arg)) {
     175         [ +  + ]:    4737405 :         if (arg == MP_OBJ_NEW_SMALL_INT(0)) {
     176                 :            :             return 0;
     177                 :            :         } else {
     178                 :    2364177 :             return 1;
     179                 :            :         }
     180                 :            :     } else {
     181                 :     295032 :         const mp_obj_type_t *type = mp_obj_get_type(arg);
     182         [ +  + ]:     295032 :         if (MP_OBJ_TYPE_HAS_SLOT(type, unary_op)) {
     183                 :     140831 :             mp_obj_t result = MP_OBJ_TYPE_GET_SLOT(type, unary_op)(MP_UNARY_OP_BOOL, arg);
     184         [ +  + ]:     140831 :             if (result != MP_OBJ_NULL) {
     185                 :     140802 :                 return result == mp_const_true;
     186                 :            :             }
     187                 :            :         }
     188                 :            : 
     189                 :     154230 :         mp_obj_t len = mp_obj_len_maybe(arg);
     190         [ +  + ]:     154230 :         if (len != MP_OBJ_NULL) {
     191                 :            :             // obj has a length, truth determined if len != 0
     192                 :         36 :             return len != MP_OBJ_NEW_SMALL_INT(0);
     193                 :            :         } else {
     194                 :            :             // any other obj is true per Python semantics
     195                 :            :             return 1;
     196                 :            :         }
     197                 :            :     }
     198                 :            : }
     199                 :            : 
     200                 :       3534 : bool mp_obj_is_callable(mp_obj_t o_in) {
     201         [ +  + ]:       3534 :     const mp_call_fun_t call = MP_OBJ_TYPE_GET_SLOT_OR_NULL(mp_obj_get_type(o_in), call);
     202         [ +  + ]:         88 :     if (call != mp_obj_instance_call) {
     203                 :       3522 :         return call != NULL;
     204                 :            :     }
     205                 :         12 :     return mp_obj_instance_is_callable(o_in);
     206                 :            : }
     207                 :            : 
     208                 :            : // This function implements the '==' and '!=' operators.
     209                 :            : //
     210                 :            : // From the Python language reference:
     211                 :            : // (https://docs.python.org/3/reference/expressions.html#not-in)
     212                 :            : // "The objects need not have the same type. If both are numbers, they are converted
     213                 :            : // to a common type. Otherwise, the == and != operators always consider objects of
     214                 :            : // different types to be unequal."
     215                 :            : //
     216                 :            : // This means that False==0 and True==1 are true expressions.
     217                 :            : //
     218                 :            : // Furthermore, from the v3.4.2 code for object.c: "Practical amendments: If rich
     219                 :            : // comparison returns NotImplemented, == and != are decided by comparing the object
     220                 :            : // pointer."
     221                 :     940462 : mp_obj_t mp_obj_equal_not_equal(mp_binary_op_t op, mp_obj_t o1, mp_obj_t o2) {
     222         [ +  + ]:     940462 :     mp_obj_t local_true = (op == MP_BINARY_OP_NOT_EQUAL) ? mp_const_false : mp_const_true;
     223                 :     732034 :     mp_obj_t local_false = (op == MP_BINARY_OP_NOT_EQUAL) ? mp_const_true : mp_const_false;
     224                 :     940462 :     int pass_number = 0;
     225                 :            : 
     226                 :            :     // Shortcut for very common cases
     227         [ +  + ]:     940462 :     if (o1 == o2 &&
     228   [ +  +  +  + ]:     236730 :         (mp_obj_is_small_int(o1) || !(mp_obj_get_type(o1)->flags & MP_TYPE_FLAG_EQ_NOT_REFLEXIVE))) {
     229                 :            :         return local_true;
     230                 :            :     }
     231                 :            : 
     232                 :            :     // fast path for strings
     233   [ +  +  +  +  :     703812 :     if (mp_obj_is_str(o1)) {
                   +  + ]
     234   [ +  +  +  +  :     271566 :         if (mp_obj_is_str(o2)) {
                   +  + ]
     235                 :            :             // both strings, use special function
     236         [ +  + ]:     521759 :             return mp_obj_str_equal(o1, o2) ? local_true : local_false;
     237                 :            :         #if MICROPY_PY_STR_BYTES_CMP_WARN
     238   [ -  +  -  +  :         48 :         } else if (mp_obj_is_type(o2, &mp_type_bytes)) {
          -  +  -  +  +  
                +  +  + ]
     239                 :          4 :         str_bytes_cmp:
     240                 :          8 :             mp_warning(MP_WARN_CAT(BytesWarning), "Comparison between bytes and str");
     241                 :          8 :             return local_false;
     242                 :            :         #endif
     243                 :            :         } else {
     244                 :         44 :             goto skip_one_pass;
     245                 :            :         }
     246                 :            :     #if MICROPY_PY_STR_BYTES_CMP_WARN
     247   [ +  +  +  +  :     432246 :     } else if (mp_obj_is_str(o2) && mp_obj_is_type(o1, &mp_type_bytes)) {
          +  +  -  +  -  
          +  -  +  -  +  
             +  +  +  + ]
     248                 :            :         // o1 is not a string (else caught above), so the objects are not equal
     249                 :          4 :         goto str_bytes_cmp;
     250                 :            :     #endif
     251                 :            :     }
     252                 :            : 
     253                 :            :     // fast path for small ints
     254         [ +  + ]:     432242 :     if (mp_obj_is_small_int(o1)) {
     255         [ +  + ]:     332729 :         if (mp_obj_is_small_int(o2)) {
     256                 :            :             // both SMALL_INT, and not equal if we get here
     257                 :            :             return local_false;
     258                 :            :         } else {
     259                 :       1056 :             goto skip_one_pass;
     260                 :            :         }
     261                 :            :     }
     262                 :            : 
     263                 :            :     // generic type, call binary_op(MP_BINARY_OP_EQUAL)
     264         [ +  + ]:     109444 :     while (pass_number < 2) {
     265                 :     105284 :         const mp_obj_type_t *type = mp_obj_get_type(o1);
     266                 :            :         // If a full equality test is not needed and the other object is a different
     267                 :            :         // type then we don't need to bother trying the comparison.
     268         [ +  + ]:     105284 :         if (MP_OBJ_TYPE_HAS_SLOT(type, binary_op) &&
     269   [ +  +  +  + ]:      97921 :             ((type->flags & MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE) || mp_obj_get_type(o2) == type)) {
     270                 :            :             // CPython is asymmetric: it will try __eq__ if there's no __ne__ but not the
     271                 :            :             // other way around.  If the class doesn't need a full test we can skip __ne__.
     272   [ +  +  +  + ]:      96597 :             if (op == MP_BINARY_OP_NOT_EQUAL && (type->flags & MP_TYPE_FLAG_EQ_HAS_NEQ_TEST)) {
     273                 :        152 :                 mp_obj_t r = MP_OBJ_TYPE_GET_SLOT(type, binary_op)(MP_BINARY_OP_NOT_EQUAL, o1, o2);
     274         [ +  + ]:        152 :                 if (r != MP_OBJ_NULL) {
     275                 :            :                     return r;
     276                 :            :                 }
     277                 :            :             }
     278                 :            : 
     279                 :            :             // Try calling __eq__.
     280                 :      96513 :             mp_obj_t r = MP_OBJ_TYPE_GET_SLOT(type, binary_op)(MP_BINARY_OP_EQUAL, o1, o2);
     281         [ +  + ]:      96145 :             if (r != MP_OBJ_NULL) {
     282         [ +  + ]:      96001 :                 if (op == MP_BINARY_OP_EQUAL) {
     283                 :            :                     return r;
     284                 :            :                 } else {
     285         [ +  + ]:       3806 :                     return mp_obj_is_true(r) ? local_true : local_false;
     286                 :            :                 }
     287                 :            :             }
     288                 :            :         }
     289                 :            : 
     290                 :       8831 :     skip_one_pass:
     291                 :            :         // Try the other way around if none of the above worked
     292                 :       9931 :         ++pass_number;
     293                 :       9931 :         mp_obj_t temp = o1;
     294                 :       9931 :         o1 = o2;
     295                 :       9931 :         o2 = temp;
     296                 :            :     }
     297                 :            : 
     298                 :            :     // equality not implemented, so fall back to pointer comparison
     299         [ +  + ]:       4160 :     return (o1 == o2) ? local_true : local_false;
     300                 :            : }
     301                 :            : 
     302                 :     609661 : bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
     303                 :     609661 :     return mp_obj_is_true(mp_obj_equal_not_equal(MP_BINARY_OP_EQUAL, o1, o2));
     304                 :            : }
     305                 :            : 
     306                 :    8619779 : mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
     307                 :            :     // This function essentially performs implicit type conversion to int
     308                 :            :     // Note that Python does NOT provide implicit type conversion from
     309                 :            :     // float to int in the core expression language, try some_list[1.0].
     310                 :    8619779 :     mp_int_t val;
     311         [ +  + ]:    8619779 :     if (!mp_obj_get_int_maybe(arg, &val)) {
     312                 :         30 :         mp_raise_TypeError_int_conversion(arg);
     313                 :            :     }
     314                 :    8562643 :     return val;
     315                 :            : }
     316                 :            : 
     317                 :       1722 : mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg) {
     318   [ +  +  +  +  :       1722 :     if (mp_obj_is_int(arg)) {
                   +  + ]
     319                 :       1704 :         return mp_obj_int_get_truncated(arg);
     320                 :            :     } else {
     321                 :         18 :         return mp_obj_get_int(arg);
     322                 :            :     }
     323                 :            : }
     324                 :            : 
     325                 :            : // returns false if arg is not of integral type
     326                 :            : // returns true and sets *value if it is of integral type
     327                 :            : // can throw OverflowError if arg is of integral type, but doesn't fit in a mp_int_t
     328                 :    8532523 : bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) {
     329         [ +  + ]:    8532523 :     if (arg == mp_const_false) {
     330                 :         36 :         *value = 0;
     331         [ +  + ]:    8532487 :     } else if (arg == mp_const_true) {
     332                 :         80 :         *value = 1;
     333         [ +  + ]:    8532407 :     } else if (mp_obj_is_small_int(arg)) {
     334                 :    8532319 :         *value = MP_OBJ_SMALL_INT_VALUE(arg);
     335   [ +  +  +  + ]:         88 :     } else if (mp_obj_is_exact_type(arg, &mp_type_int)) {
     336                 :         18 :         *value = mp_obj_int_get_checked(arg);
     337                 :            :     } else {
     338                 :         70 :         arg = mp_unary_op(MP_UNARY_OP_INT_MAYBE, (mp_obj_t)arg);
     339         [ +  + ]:         70 :         if (arg != MP_OBJ_NULL) {
     340                 :          4 :             *value = mp_obj_int_get_checked(arg);
     341                 :            :         } else {
     342                 :            :             return false;
     343                 :            :         }
     344                 :            :     }
     345                 :            :     return true;
     346                 :            : }
     347                 :            : 
     348                 :            : #if MICROPY_PY_BUILTINS_FLOAT
     349                 :     538734 : bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value) {
     350                 :     538734 :     mp_float_t val;
     351                 :            : 
     352         [ +  + ]:     538734 :     if (arg == mp_const_false) {
     353                 :            :         val = 0;
     354         [ +  + ]:     538706 :     } else if (arg == mp_const_true) {
     355                 :            :         val = 1;
     356         [ +  + ]:     538686 :     } else if (mp_obj_is_small_int(arg)) {
     357                 :     260760 :         val = (mp_float_t)MP_OBJ_SMALL_INT_VALUE(arg);
     358                 :            :     #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
     359   [ +  -  +  + ]:     277926 :     } else if (mp_obj_is_exact_type(arg, &mp_type_int)) {
     360                 :      21624 :         val = mp_obj_int_as_float_impl(arg);
     361                 :            :     #endif
     362   [ -  +  -  +  :     256302 :     } else if (mp_obj_is_float(arg)) {
          -  +  -  +  +  
                -  +  + ]
     363                 :     253362 :         val = mp_obj_float_get(arg);
     364                 :            :     } else {
     365                 :       2940 :         arg = mp_unary_op(MP_UNARY_OP_FLOAT_MAYBE, (mp_obj_t)arg);
     366   [ +  +  +  +  :       2940 :         if (arg != MP_OBJ_NULL && mp_obj_is_float(arg)) {
                   +  - ]
     367                 :         20 :             val = mp_obj_float_get(arg);
     368                 :            :         } else {
     369                 :            :             return false;
     370                 :            :         }
     371                 :            :     }
     372                 :     535814 :     *value = val;
     373                 :     535814 :     return true;
     374                 :            : }
     375                 :            : 
     376                 :     108898 : mp_float_t mp_obj_get_float(mp_obj_t arg) {
     377                 :     108898 :     mp_float_t val;
     378                 :            : 
     379         [ +  + ]:     108898 :     if (!mp_obj_get_float_maybe(arg, &val)) {
     380                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     381                 :            :         mp_raise_TypeError(MP_ERROR_TEXT("can't convert to float"));
     382                 :            :         #else
     383                 :         16 :         mp_raise_msg_varg(&mp_type_TypeError,
     384                 :         16 :             MP_ERROR_TEXT("can't convert %s to float"), mp_obj_get_type_str(arg));
     385                 :            :         #endif
     386                 :            :     }
     387                 :            : 
     388                 :     108882 :     return val;
     389                 :            : }
     390                 :            : 
     391                 :            : #if MICROPY_PY_BUILTINS_COMPLEX
     392                 :       2984 : bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
     393         [ +  + ]:       2984 :     if (mp_obj_get_float_maybe(arg, real)) {
     394                 :        104 :         *imag = 0;
     395   [ -  +  -  +  :       2880 :     } else if (mp_obj_is_type(arg, &mp_type_complex)) {
          -  +  -  +  +  
                -  +  + ]
     396                 :       2848 :         mp_obj_complex_get(arg, real, imag);
     397                 :            :     } else {
     398                 :         32 :         arg = mp_unary_op(MP_UNARY_OP_COMPLEX_MAYBE, (mp_obj_t)arg);
     399   [ +  +  +  +  :         32 :         if (arg != MP_OBJ_NULL && mp_obj_is_type(arg, &mp_type_complex)) {
                   +  - ]
     400                 :          8 :             mp_obj_complex_get(arg, real, imag);
     401                 :            :         } else {
     402                 :            :             return false;
     403                 :            :         }
     404                 :            :     }
     405                 :            :     return true;
     406                 :            : }
     407                 :            : 
     408                 :       1600 : void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
     409         [ +  + ]:       1600 :     if (!mp_obj_get_complex_maybe(arg, real, imag)) {
     410                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     411                 :            :         mp_raise_TypeError(MP_ERROR_TEXT("can't convert to complex"));
     412                 :            :         #else
     413                 :         16 :         mp_raise_msg_varg(&mp_type_TypeError,
     414                 :         16 :             MP_ERROR_TEXT("can't convert %s to complex"), mp_obj_get_type_str(arg));
     415                 :            :         #endif
     416                 :            :     }
     417                 :       1584 : }
     418                 :            : #endif
     419                 :            : #endif
     420                 :            : 
     421                 :            : // note: returned value in *items may point to the interior of a GC block
     422                 :     155689 : void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items) {
     423   [ -  +  -  +  :     155689 :     if (mp_obj_is_type(o, &mp_type_tuple)) {
          -  +  -  +  +  
                +  +  + ]
     424                 :     145431 :         mp_obj_tuple_get(o, len, items);
     425   [ -  +  -  +  :      10258 :     } else if (mp_obj_is_type(o, &mp_type_list)) {
          -  +  -  +  +  
                +  +  + ]
     426                 :      10236 :         mp_obj_list_get(o, len, items);
     427                 :            :     } else {
     428                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     429                 :            :         mp_raise_TypeError(MP_ERROR_TEXT("expected tuple/list"));
     430                 :            :         #else
     431                 :         22 :         mp_raise_msg_varg(&mp_type_TypeError,
     432                 :         22 :             MP_ERROR_TEXT("object '%s' isn't a tuple or list"), mp_obj_get_type_str(o));
     433                 :            :         #endif
     434                 :            :     }
     435                 :     155667 : }
     436                 :            : 
     437                 :            : // note: returned value in *items may point to the interior of a GC block
     438                 :        392 : void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items) {
     439                 :        392 :     size_t seq_len;
     440                 :        392 :     mp_obj_get_array(o, &seq_len, items);
     441         [ +  + ]:        380 :     if (seq_len != len) {
     442                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     443                 :            :         mp_raise_ValueError(MP_ERROR_TEXT("tuple/list has wrong length"));
     444                 :            :         #else
     445                 :         16 :         mp_raise_msg_varg(&mp_type_ValueError,
     446                 :         16 :             MP_ERROR_TEXT("requested length %d but object has length %d"), (int)len, (int)seq_len);
     447                 :            :         #endif
     448                 :            :     }
     449                 :        364 : }
     450                 :            : 
     451                 :            : // is_slice determines whether the index is a slice index
     452                 :   19348996 : size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice) {
     453                 :   19348996 :     mp_int_t i;
     454         [ +  + ]:   19348996 :     if (mp_obj_is_small_int(index)) {
     455                 :   19348940 :         i = MP_OBJ_SMALL_INT_VALUE(index);
     456         [ +  + ]:         56 :     } else if (!mp_obj_get_int_maybe(index, &i)) {
     457                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     458                 :            :         mp_raise_TypeError(MP_ERROR_TEXT("indices must be integers"));
     459                 :            :         #else
     460                 :         12 :         mp_raise_msg_varg(&mp_type_TypeError,
     461                 :         12 :             MP_ERROR_TEXT("%q indices must be integers, not %s"),
     462                 :         12 :             type->name, mp_obj_get_type_str(index));
     463                 :            :         #endif
     464                 :            :     }
     465                 :            : 
     466         [ +  + ]:   19348984 :     if (i < 0) {
     467                 :     100127 :         i += len;
     468                 :            :     }
     469         [ +  + ]:   19348984 :     if (is_slice) {
     470         [ +  + ]:        280 :         if (i < 0) {
     471                 :         12 :             i = 0;
     472         [ +  + ]:        268 :         } else if ((mp_uint_t)i > len) {
     473                 :         24 :             i = len;
     474                 :            :         }
     475                 :            :     } else {
     476   [ +  -  +  + ]:   19348704 :         if (i < 0 || (mp_uint_t)i >= len) {
     477                 :            :             #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     478                 :            :             mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("index out of range"));
     479                 :            :             #else
     480                 :         16 :             mp_raise_msg_varg(&mp_type_IndexError, MP_ERROR_TEXT("%q index out of range"), type->name);
     481                 :            :             #endif
     482                 :            :         }
     483                 :            :     }
     484                 :            : 
     485                 :            :     // By this point 0 <= i <= len and so fits in a size_t
     486                 :   19348968 :     return (size_t)i;
     487                 :            : }
     488                 :            : 
     489                 :      16166 : mp_obj_t mp_obj_id(mp_obj_t o_in) {
     490                 :      16166 :     mp_int_t id = (mp_int_t)o_in;
     491         [ +  + ]:      16166 :     if (!mp_obj_is_obj(o_in)) {
     492                 :      16028 :         return mp_obj_new_int(id);
     493         [ +  - ]:        138 :     } else if (id >= 0) {
     494                 :            :         // Many OSes and CPUs have affinity for putting "user" memories
     495                 :            :         // into low half of address space, and "system" into upper half.
     496                 :            :         // We're going to take advantage of that and return small int
     497                 :            :         // (signed) for such "user" addresses.
     498                 :        138 :         return MP_OBJ_NEW_SMALL_INT(id);
     499                 :            :     } else {
     500                 :            :         // If that didn't work, well, let's return long int, just as
     501                 :            :         // a (big) positive value, so it will never clash with the range
     502                 :            :         // of small int returned in previous case.
     503                 :          0 :         return mp_obj_new_int_from_uint((mp_uint_t)id);
     504                 :            :     }
     505                 :            : }
     506                 :            : 
     507                 :            : // will raise a TypeError if object has no length
     508                 :      36185 : mp_obj_t mp_obj_len(mp_obj_t o_in) {
     509                 :      36185 :     mp_obj_t len = mp_obj_len_maybe(o_in);
     510         [ +  + ]:      36185 :     if (len == MP_OBJ_NULL) {
     511                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     512                 :            :         mp_raise_TypeError(MP_ERROR_TEXT("object has no len"));
     513                 :            :         #else
     514                 :          8 :         mp_raise_msg_varg(&mp_type_TypeError,
     515                 :          8 :             MP_ERROR_TEXT("object of type '%s' has no len()"), mp_obj_get_type_str(o_in));
     516                 :            :         #endif
     517                 :            :     } else {
     518                 :      36177 :         return len;
     519                 :            :     }
     520                 :            : }
     521                 :            : 
     522                 :            : // may return MP_OBJ_NULL
     523                 :     200279 : mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
     524                 :     200279 :     if (
     525                 :            :         #if !MICROPY_PY_BUILTINS_STR_UNICODE
     526                 :            :         // It's simple - unicode is slow, non-unicode is fast
     527                 :            :         mp_obj_is_str(o_in) ||
     528                 :            :         #endif
     529   [ -  +  -  +  :     200279 :         mp_obj_is_type(o_in, &mp_type_bytes)) {
          -  +  -  +  +  
                +  +  + ]
     530         [ -  + ]:        404 :         GET_STR_LEN(o_in, l);
     531                 :        404 :         return MP_OBJ_NEW_SMALL_INT(l);
     532                 :            :     } else {
     533                 :     199875 :         const mp_obj_type_t *type = mp_obj_get_type(o_in);
     534         [ +  + ]:     199875 :         if (MP_OBJ_TYPE_HAS_SLOT(type, unary_op)) {
     535                 :      45575 :             return MP_OBJ_TYPE_GET_SLOT(type, unary_op)(MP_UNARY_OP_LEN, o_in);
     536                 :            :         } else {
     537                 :            :             return MP_OBJ_NULL;
     538                 :            :         }
     539                 :            :     }
     540                 :            : }
     541                 :            : 
     542                 :   20529110 : mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
     543                 :   20529110 :     const mp_obj_type_t *type = mp_obj_get_type(base);
     544         [ +  + ]:   20529110 :     if (MP_OBJ_TYPE_HAS_SLOT(type, subscr)) {
     545                 :   20529098 :         mp_obj_t ret = MP_OBJ_TYPE_GET_SLOT(type, subscr)(base, index, value);
     546         [ +  + ]:   20290214 :         if (ret != MP_OBJ_NULL) {
     547                 :   20290162 :             return ret;
     548                 :            :         }
     549                 :            :         // TODO: call base classes here?
     550                 :            :     }
     551         [ +  + ]:         64 :     if (value == MP_OBJ_NULL) {
     552                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     553                 :            :         mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item deletion"));
     554                 :            :         #else
     555                 :         22 :         mp_raise_msg_varg(&mp_type_TypeError,
     556                 :         22 :             MP_ERROR_TEXT("'%s' object doesn't support item deletion"), mp_obj_get_type_str(base));
     557                 :            :         #endif
     558         [ +  + ]:         42 :     } else if (value == MP_OBJ_SENTINEL) {
     559                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     560                 :            :         mp_raise_TypeError(MP_ERROR_TEXT("object isn't subscriptable"));
     561                 :            :         #else
     562                 :          8 :         mp_raise_msg_varg(&mp_type_TypeError,
     563                 :          8 :             MP_ERROR_TEXT("'%s' object isn't subscriptable"), mp_obj_get_type_str(base));
     564                 :            :         #endif
     565                 :            :     } else {
     566                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     567                 :            :         mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item assignment"));
     568                 :            :         #else
     569                 :         34 :         mp_raise_msg_varg(&mp_type_TypeError,
     570                 :         34 :             MP_ERROR_TEXT("'%s' object doesn't support item assignment"), mp_obj_get_type_str(base));
     571                 :            :         #endif
     572                 :            :     }
     573                 :            : }
     574                 :            : 
     575                 :            : // Return input argument. Useful as .getiter for objects which are
     576                 :            : // their own iterators, etc.
     577                 :        322 : mp_obj_t mp_identity(mp_obj_t self) {
     578                 :        322 :     return self;
     579                 :            : }
     580                 :            : MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
     581                 :            : 
     582                 :            : // mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) {
     583                 :            : //     (void)iter_buf;
     584                 :            : //     return self;
     585                 :            : // }
     586                 :            : 
     587                 :     114904 : bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
     588                 :     114904 :     const mp_obj_type_t *type = mp_obj_get_type(obj);
     589         [ +  + ]:     114904 :     if (MP_OBJ_TYPE_HAS_SLOT(type, buffer)
     590         [ +  + ]:      32914 :         && MP_OBJ_TYPE_GET_SLOT(type, buffer)(obj, bufinfo, flags & MP_BUFFER_RW) == 0) {
     591                 :            :         return true;
     592                 :            :     }
     593         [ +  + ]:      82230 :     if (flags & MP_BUFFER_RAISE_IF_UNSUPPORTED) {
     594                 :          4 :         mp_raise_TypeError(MP_ERROR_TEXT("object with buffer protocol required"));
     595                 :            :     }
     596                 :            :     return false;
     597                 :            : }

Generated by: LCOV version 1.15-5-g462f71d