LCOV - code coverage report
Current view: top level - py - runtime.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.19.1-992-g38e7b842c.info Lines: 742 750 98.9 %
Date: 2023-03-23 12:38:05 Functions: 48 49 98.0 %
Branches: 467 545 85.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                 :            :  * Copyright (c) 2014-2018 Paul Sokolovsky
       8                 :            :  *
       9                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
      10                 :            :  * of this software and associated documentation files (the "Software"), to deal
      11                 :            :  * in the Software without restriction, including without limitation the rights
      12                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      13                 :            :  * copies of the Software, and to permit persons to whom the Software is
      14                 :            :  * furnished to do so, subject to the following conditions:
      15                 :            :  *
      16                 :            :  * The above copyright notice and this permission notice shall be included in
      17                 :            :  * all copies or substantial portions of the Software.
      18                 :            :  *
      19                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      20                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      21                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      22                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      23                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      24                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      25                 :            :  * THE SOFTWARE.
      26                 :            :  */
      27                 :            : 
      28                 :            : #include <assert.h>
      29                 :            : #include <stdarg.h>
      30                 :            : #include <stdio.h>
      31                 :            : #include <string.h>
      32                 :            : #include <unistd.h>
      33                 :            : 
      34                 :            : #include "py/parsenum.h"
      35                 :            : #include "py/compile.h"
      36                 :            : #include "py/objstr.h"
      37                 :            : #include "py/objtuple.h"
      38                 :            : #include "py/objlist.h"
      39                 :            : #include "py/objtype.h"
      40                 :            : #include "py/objmodule.h"
      41                 :            : #include "py/objgenerator.h"
      42                 :            : #include "py/smallint.h"
      43                 :            : #include "py/runtime.h"
      44                 :            : #include "py/builtin.h"
      45                 :            : #include "py/stackctrl.h"
      46                 :            : #include "py/gc.h"
      47                 :            : 
      48                 :            : #if MICROPY_DEBUG_VERBOSE // print debugging info
      49                 :            : #define DEBUG_PRINT (1)
      50                 :            : #define DEBUG_printf DEBUG_printf
      51                 :            : #define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
      52                 :            : #else // don't print debugging info
      53                 :            : #define DEBUG_printf(...) (void)0
      54                 :            : #define DEBUG_OP_printf(...) (void)0
      55                 :            : #endif
      56                 :            : 
      57                 :            : const mp_obj_module_t mp_module___main__ = {
      58                 :            :     .base = { &mp_type_module },
      59                 :            :     .globals = (mp_obj_dict_t *)&MP_STATE_VM(dict_main),
      60                 :            : };
      61                 :            : 
      62                 :            : MP_REGISTER_MODULE(MP_QSTR___main__, mp_module___main__);
      63                 :            : 
      64                 :            : #define TYPE_HAS_ITERNEXT(type) (type->flags & (MP_TYPE_FLAG_ITER_IS_ITERNEXT | MP_TYPE_FLAG_ITER_IS_CUSTOM | MP_TYPE_FLAG_ITER_IS_STREAM))
      65                 :            : 
      66                 :       3224 : void mp_init(void) {
      67                 :       3224 :     qstr_init();
      68                 :            : 
      69                 :            :     // no pending exceptions to start with
      70                 :       3224 :     MP_STATE_THREAD(mp_pending_exception) = MP_OBJ_NULL;
      71                 :            :     #if MICROPY_ENABLE_SCHEDULER
      72                 :            :     #if MICROPY_SCHEDULER_STATIC_NODES
      73                 :            :     if (MP_STATE_VM(sched_head) == NULL) {
      74                 :            :         // no pending callbacks to start with
      75                 :            :         MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
      76                 :            :     } else {
      77                 :            :         // pending callbacks are on the list, eg from before a soft reset
      78                 :            :         MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
      79                 :            :     }
      80                 :            :     #endif
      81                 :       3224 :     MP_STATE_VM(sched_idx) = 0;
      82                 :       3224 :     MP_STATE_VM(sched_len) = 0;
      83                 :            :     #endif
      84                 :            : 
      85                 :            :     #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
      86                 :       3224 :     mp_init_emergency_exception_buf();
      87                 :            :     #endif
      88                 :            : 
      89                 :            :     #if MICROPY_KBD_EXCEPTION
      90                 :            :     // initialise the exception object for raising KeyboardInterrupt
      91                 :       3224 :     MP_STATE_VM(mp_kbd_exception).base.type = &mp_type_KeyboardInterrupt;
      92                 :       3224 :     MP_STATE_VM(mp_kbd_exception).traceback_alloc = 0;
      93                 :       3224 :     MP_STATE_VM(mp_kbd_exception).traceback_len = 0;
      94                 :       3224 :     MP_STATE_VM(mp_kbd_exception).traceback_data = NULL;
      95                 :       3224 :     MP_STATE_VM(mp_kbd_exception).args = (mp_obj_tuple_t *)&mp_const_empty_tuple_obj;
      96                 :            :     #endif
      97                 :            : 
      98                 :            :     #if MICROPY_ENABLE_COMPILER
      99                 :            :     // optimization disabled by default
     100                 :       3224 :     MP_STATE_VM(mp_optimise_value) = 0;
     101                 :            :     #if MICROPY_EMIT_NATIVE
     102                 :       3224 :     MP_STATE_VM(default_emit_opt) = MP_EMIT_OPT_NONE;
     103                 :            :     #endif
     104                 :            :     #endif
     105                 :            : 
     106                 :            :     // init global module dict
     107                 :       3224 :     mp_obj_dict_init(&MP_STATE_VM(mp_loaded_modules_dict), MICROPY_LOADED_MODULES_DICT_SIZE);
     108                 :            : 
     109                 :            :     // initialise the __main__ module
     110                 :       3224 :     mp_obj_dict_init(&MP_STATE_VM(dict_main), 1);
     111                 :       3224 :     mp_obj_dict_store(MP_OBJ_FROM_PTR(&MP_STATE_VM(dict_main)), MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
     112                 :            : 
     113                 :            :     // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
     114                 :       3224 :     mp_locals_set(&MP_STATE_VM(dict_main));
     115                 :       3224 :     mp_globals_set(&MP_STATE_VM(dict_main));
     116                 :            : 
     117                 :            :     #if MICROPY_CAN_OVERRIDE_BUILTINS
     118                 :            :     // start with no extensions to builtins
     119                 :       3224 :     MP_STATE_VM(mp_module_builtins_override_dict) = NULL;
     120                 :            :     #endif
     121                 :            : 
     122                 :            :     #if MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE
     123                 :            :     MP_STATE_VM(track_reloc_code_list) = MP_OBJ_NULL;
     124                 :            :     #endif
     125                 :            : 
     126                 :            :     #if MICROPY_PY_OS_DUPTERM
     127                 :            :     for (size_t i = 0; i < MICROPY_PY_OS_DUPTERM; ++i) {
     128                 :            :         MP_STATE_VM(dupterm_objs[i]) = MP_OBJ_NULL;
     129                 :            :     }
     130                 :            :     #endif
     131                 :            : 
     132                 :            :     #if MICROPY_VFS
     133                 :            :     // initialise the VFS sub-system
     134                 :       3224 :     MP_STATE_VM(vfs_cur) = NULL;
     135                 :       3224 :     MP_STATE_VM(vfs_mount_table) = NULL;
     136                 :            :     #endif
     137                 :            : 
     138                 :            :     #if MICROPY_PY_SYS_PATH_ARGV_DEFAULTS
     139                 :            :     mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0);
     140                 :            :     mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
     141                 :            :     #if MICROPY_MODULE_FROZEN
     142                 :            :     mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
     143                 :            :     #endif
     144                 :            :     mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);
     145                 :            :     #endif
     146                 :            : 
     147                 :            :     #if MICROPY_PY_SYS_ATEXIT
     148                 :       3224 :     MP_STATE_VM(sys_exitfunc) = mp_const_none;
     149                 :            :     #endif
     150                 :            : 
     151                 :            :     #if MICROPY_PY_SYS_PS1_PS2
     152                 :       3224 :     MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_PS1]) = MP_OBJ_NEW_QSTR(MP_QSTR__gt__gt__gt__space_);
     153                 :       3224 :     MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_PS2]) = MP_OBJ_NEW_QSTR(MP_QSTR__dot__dot__dot__space_);
     154                 :            :     #endif
     155                 :            : 
     156                 :            :     #if MICROPY_PY_SYS_SETTRACE
     157                 :            :     MP_STATE_THREAD(prof_trace_callback) = MP_OBJ_NULL;
     158                 :            :     MP_STATE_THREAD(prof_callback_is_executing) = false;
     159                 :            :     MP_STATE_THREAD(current_code_state) = NULL;
     160                 :            :     #endif
     161                 :            : 
     162                 :            :     #if MICROPY_PY_SYS_TRACEBACKLIMIT
     163                 :       3224 :     MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_TRACEBACKLIMIT]) = MP_OBJ_NEW_SMALL_INT(1000);
     164                 :            :     #endif
     165                 :            : 
     166                 :            :     #if MICROPY_PY_BLUETOOTH
     167                 :            :     MP_STATE_VM(bluetooth) = MP_OBJ_NULL;
     168                 :            :     #endif
     169                 :            : 
     170                 :            :     #if MICROPY_PY_THREAD_GIL
     171                 :            :     mp_thread_mutex_init(&MP_STATE_VM(gil_mutex));
     172                 :            :     #endif
     173                 :            : 
     174                 :            :     // call port specific initialization if any
     175                 :            :     #ifdef MICROPY_PORT_INIT_FUNC
     176                 :            :     MICROPY_PORT_INIT_FUNC;
     177                 :            :     #endif
     178                 :            : 
     179                 :       3224 :     MP_THREAD_GIL_ENTER();
     180                 :       3224 : }
     181                 :            : 
     182                 :       3220 : void mp_deinit(void) {
     183                 :       3220 :     MP_THREAD_GIL_EXIT();
     184                 :            : 
     185                 :            :     // call port specific deinitialization if any
     186                 :            :     #ifdef MICROPY_PORT_DEINIT_FUNC
     187                 :            :     MICROPY_PORT_DEINIT_FUNC;
     188                 :            :     #endif
     189                 :       3220 : }
     190                 :            : 
     191                 :    1204600 : mp_obj_t MICROPY_WRAP_MP_LOAD_NAME(mp_load_name)(qstr qst) {
     192                 :            :     // logic: search locals, globals, builtins
     193                 :    1204600 :     DEBUG_OP_printf("load name %s\n", qstr_str(qst));
     194                 :            :     // If we're at the outer scope (locals == globals), dispatch to load_global right away
     195         [ +  + ]:    2409200 :     if (mp_locals_get() != mp_globals_get()) {
     196                 :       1669 :         mp_map_elem_t *elem = mp_map_lookup(&mp_locals_get()->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
     197         [ +  + ]:       1669 :         if (elem != NULL) {
     198                 :         32 :             return elem->value;
     199                 :            :         }
     200                 :            :     }
     201                 :    1204568 :     return mp_load_global(qst);
     202                 :            : }
     203                 :            : 
     204                 :   14344600 : mp_obj_t MICROPY_WRAP_MP_LOAD_GLOBAL(mp_load_global)(qstr qst) {
     205                 :            :     // logic: search globals, builtins
     206                 :   14344600 :     DEBUG_OP_printf("load global %s\n", qstr_str(qst));
     207                 :   14344600 :     mp_map_elem_t *elem = mp_map_lookup(&mp_globals_get()->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
     208         [ +  + ]:   14068469 :     if (elem == NULL) {
     209                 :            :         #if MICROPY_CAN_OVERRIDE_BUILTINS
     210         [ +  + ]:     838409 :         if (MP_STATE_VM(mp_module_builtins_override_dict) != NULL) {
     211                 :            :             // lookup in additional dynamic table of builtins first
     212                 :        210 :             elem = mp_map_lookup(&MP_STATE_VM(mp_module_builtins_override_dict)->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
     213         [ +  + ]:        210 :             if (elem != NULL) {
     214                 :          8 :                 return elem->value;
     215                 :            :             }
     216                 :            :         }
     217                 :            :         #endif
     218                 :     838401 :         elem = mp_map_lookup((mp_map_t *)&mp_module_builtins_globals.map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
     219         [ +  + ]:     838402 :         if (elem == NULL) {
     220                 :            :             #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     221                 :            :             mp_raise_msg(&mp_type_NameError, MP_ERROR_TEXT("name not defined"));
     222                 :            :             #else
     223                 :        200 :             mp_raise_msg_varg(&mp_type_NameError, MP_ERROR_TEXT("name '%q' isn't defined"), qst);
     224                 :            :             #endif
     225                 :            :         }
     226                 :            :     }
     227                 :   14068262 :     return elem->value;
     228                 :            : }
     229                 :            : 
     230                 :       1486 : mp_obj_t mp_load_build_class(void) {
     231                 :       1486 :     DEBUG_OP_printf("load_build_class\n");
     232                 :            :     #if MICROPY_CAN_OVERRIDE_BUILTINS
     233         [ +  + ]:       1486 :     if (MP_STATE_VM(mp_module_builtins_override_dict) != NULL) {
     234                 :            :         // lookup in additional dynamic table of builtins first
     235                 :         34 :         mp_map_elem_t *elem = mp_map_lookup(&MP_STATE_VM(mp_module_builtins_override_dict)->map, MP_OBJ_NEW_QSTR(MP_QSTR___build_class__), MP_MAP_LOOKUP);
     236         [ +  + ]:         34 :         if (elem != NULL) {
     237                 :         32 :             return elem->value;
     238                 :            :         }
     239                 :            :     }
     240                 :            :     #endif
     241                 :            :     return MP_OBJ_FROM_PTR(&mp_builtin___build_class___obj);
     242                 :            : }
     243                 :            : 
     244                 :     625553 : void mp_store_name(qstr qst, mp_obj_t obj) {
     245                 :     625553 :     DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qst), obj);
     246                 :     625553 :     mp_obj_dict_store(MP_OBJ_FROM_PTR(mp_locals_get()), MP_OBJ_NEW_QSTR(qst), obj);
     247                 :     625551 : }
     248                 :            : 
     249                 :        456 : void mp_delete_name(qstr qst) {
     250                 :        456 :     DEBUG_OP_printf("delete name %s\n", qstr_str(qst));
     251                 :            :     // TODO convert KeyError to NameError if qst not found
     252                 :        456 :     mp_obj_dict_delete(MP_OBJ_FROM_PTR(mp_locals_get()), MP_OBJ_NEW_QSTR(qst));
     253                 :        452 : }
     254                 :            : 
     255                 :     173907 : void mp_store_global(qstr qst, mp_obj_t obj) {
     256                 :     173907 :     DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qst), obj);
     257                 :     173907 :     mp_obj_dict_store(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(qst), obj);
     258                 :     173907 : }
     259                 :            : 
     260                 :          8 : void mp_delete_global(qstr qst) {
     261                 :          8 :     DEBUG_OP_printf("delete global %s\n", qstr_str(qst));
     262                 :            :     // TODO convert KeyError to NameError if qst not found
     263                 :          8 :     mp_obj_dict_delete(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(qst));
     264                 :          4 : }
     265                 :            : 
     266                 :     646942 : mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) {
     267                 :     646942 :     DEBUG_OP_printf("unary " UINT_FMT " %q %p\n", op, mp_unary_op_method_name[op], arg);
     268                 :            : 
     269         [ +  + ]:     646942 :     if (op == MP_UNARY_OP_NOT) {
     270                 :            :         // "not x" is the negative of whether "x" is true per Python semantics
     271         [ +  + ]:     451282 :         return mp_obj_new_bool(mp_obj_is_true(arg) == 0);
     272         [ +  + ]:     345345 :     } else if (mp_obj_is_small_int(arg)) {
     273                 :     212733 :         mp_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
     274   [ +  +  +  +  :     212733 :         switch (op) {
                      + ]
     275                 :          8 :             case MP_UNARY_OP_BOOL:
     276         [ +  - ]:         16 :                 return mp_obj_new_bool(val != 0);
     277                 :            :             case MP_UNARY_OP_HASH:
     278                 :            :                 return arg;
     279                 :            :             case MP_UNARY_OP_POSITIVE:
     280                 :            :             case MP_UNARY_OP_INT:
     281                 :            :                 return arg;
     282                 :       1720 :             case MP_UNARY_OP_NEGATIVE:
     283                 :            :                 // check for overflow
     284         [ +  + ]:       1720 :                 if (val == MP_SMALL_INT_MIN) {
     285                 :          2 :                     return mp_obj_new_int(-val);
     286                 :            :                 } else {
     287                 :       1718 :                     return MP_OBJ_NEW_SMALL_INT(-val);
     288                 :            :                 }
     289                 :         64 :             case MP_UNARY_OP_ABS:
     290         [ +  + ]:         64 :                 if (val >= 0) {
     291                 :            :                     return arg;
     292         [ +  + ]:         34 :                 } else if (val == MP_SMALL_INT_MIN) {
     293                 :            :                     // check for overflow
     294                 :          2 :                     return mp_obj_new_int(-val);
     295                 :            :                 } else {
     296                 :         32 :                     return MP_OBJ_NEW_SMALL_INT(-val);
     297                 :            :                 }
     298                 :         78 :             default:
     299         [ -  + ]:         78 :                 assert(op == MP_UNARY_OP_INVERT);
     300                 :         78 :                 return MP_OBJ_NEW_SMALL_INT(~val);
     301                 :            :         }
     302   [ +  +  +  +  :     132612 :     } else if (op == MP_UNARY_OP_HASH && mp_obj_is_str_or_bytes(arg)) {
          +  +  +  +  +  
                      + ]
     303                 :            :         // fast path for hashing str/bytes
     304         [ +  + ]:      97259 :         GET_STR_HASH(arg, h);
     305         [ +  + ]:      97259 :         if (h == 0) {
     306         [ -  + ]:          4 :             GET_STR_DATA_LEN(arg, data, len);
     307                 :          4 :             h = qstr_compute_hash(data, len);
     308                 :            :         }
     309                 :      97259 :         return MP_OBJ_NEW_SMALL_INT(h);
     310                 :            :     } else {
     311                 :      35353 :         const mp_obj_type_t *type = mp_obj_get_type(arg);
     312         [ +  + ]:      35353 :         if (MP_OBJ_TYPE_HAS_SLOT(type, unary_op)) {
     313                 :      35349 :             mp_obj_t result = MP_OBJ_TYPE_GET_SLOT(type, unary_op)(op, arg);
     314         [ +  + ]:      35341 :             if (result != MP_OBJ_NULL) {
     315                 :            :                 return result;
     316                 :            :             }
     317                 :            :         }
     318         [ +  + ]:       3028 :         if (op == MP_UNARY_OP_BOOL) {
     319                 :            :             // Type doesn't have unary_op (or didn't handle MP_UNARY_OP_BOOL),
     320                 :            :             // so is implicitly True as this code path is impossible to reach
     321                 :            :             // if arg==mp_const_none.
     322                 :            :             return mp_const_true;
     323                 :            :         }
     324                 :            :         #if MICROPY_PY_BUILTINS_FLOAT
     325                 :       3020 :         if (op == MP_UNARY_OP_FLOAT_MAYBE
     326                 :            :             #if MICROPY_PY_BUILTINS_COMPLEX
     327         [ +  + ]:       3020 :             || op == MP_UNARY_OP_COMPLEX_MAYBE
     328                 :            :             #endif
     329                 :            :             ) {
     330                 :            :             return MP_OBJ_NULL;
     331                 :            :         }
     332                 :            :         #endif
     333                 :            :         // With MP_UNARY_OP_INT, mp_unary_op() becomes a fallback for mp_obj_get_int().
     334                 :            :         // In this case provide a more focused error message to not confuse, e.g. chr(1.0)
     335                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     336                 :            :         if (op == MP_UNARY_OP_INT) {
     337                 :            :             mp_raise_TypeError(MP_ERROR_TEXT("can't convert to int"));
     338                 :            :         } else {
     339                 :            :             mp_raise_TypeError(MP_ERROR_TEXT("unsupported type for operator"));
     340                 :            :         }
     341                 :            :         #else
     342         [ +  + ]:         92 :         if (op == MP_UNARY_OP_INT) {
     343                 :         30 :             mp_raise_msg_varg(&mp_type_TypeError,
     344                 :         30 :                 MP_ERROR_TEXT("can't convert %s to int"), mp_obj_get_type_str(arg));
     345                 :            :         } else {
     346                 :         62 :             mp_raise_msg_varg(&mp_type_TypeError,
     347                 :         62 :                 MP_ERROR_TEXT("unsupported type for %q: '%s'"),
     348                 :         62 :                 mp_unary_op_method_name[op], mp_obj_get_type_str(arg));
     349                 :            :         }
     350                 :            :         #endif
     351                 :            :     }
     352                 :            : }
     353                 :            : 
     354                 :   67208761 : mp_obj_t MICROPY_WRAP_MP_BINARY_OP(mp_binary_op)(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
     355                 :   67208761 :     DEBUG_OP_printf("binary " UINT_FMT " %q %p %p\n", op, mp_binary_op_method_name[op], lhs, rhs);
     356                 :            : 
     357                 :            :     // TODO correctly distinguish inplace operators for mutable objects
     358                 :            :     // lookup logic that CPython uses for +=:
     359                 :            :     //   check for implemented +=
     360                 :            :     //   then check for implemented +
     361                 :            :     //   then check for implemented seq.inplace_concat
     362                 :            :     //   then check for implemented seq.concat
     363                 :            :     //   then fail
     364                 :            :     // note that list does not implement + or +=, so that inplace_concat is reached first for +=
     365                 :            : 
     366                 :            :     // deal with is
     367         [ +  + ]:   67208761 :     if (op == MP_BINARY_OP_IS) {
     368         [ +  + ]:     599646 :         return mp_obj_new_bool(lhs == rhs);
     369                 :            :     }
     370                 :            : 
     371                 :            :     // deal with == and != for all types
     372         [ +  + ]:   66759093 :     if (op == MP_BINARY_OP_EQUAL || op == MP_BINARY_OP_NOT_EQUAL) {
     373                 :            :         // mp_obj_equal_not_equal supports a bunch of shortcuts
     374                 :     367450 :         return mp_obj_equal_not_equal(op, lhs, rhs);
     375                 :            :     }
     376                 :            : 
     377                 :            :     // deal with exception_match for all types
     378         [ +  + ]:   66391643 :     if (op == MP_BINARY_OP_EXCEPTION_MATCH) {
     379                 :            :         // rhs must be issubclass(rhs, BaseException)
     380         [ +  + ]:       5368 :         if (mp_obj_is_exception_type(rhs)) {
     381         [ +  + ]:       4765 :             if (mp_obj_exception_match(lhs, rhs)) {
     382                 :            :                 return mp_const_true;
     383                 :            :             } else {
     384                 :        268 :                 return mp_const_false;
     385                 :            :             }
     386   [ -  +  -  +  :        603 :         } else if (mp_obj_is_type(rhs, &mp_type_tuple)) {
          -  +  -  +  +  
                +  +  - ]
     387                 :            :             mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(rhs);
     388         [ +  + ]:       1048 :             for (size_t i = 0; i < tuple->len; i++) {
     389                 :       1044 :                 rhs = tuple->items[i];
     390         [ +  + ]:       1044 :                 if (!mp_obj_is_exception_type(rhs)) {
     391                 :          4 :                     goto unsupported_op;
     392                 :            :                 }
     393         [ +  + ]:       1040 :                 if (mp_obj_exception_match(lhs, rhs)) {
     394                 :            :                     return mp_const_true;
     395                 :            :                 }
     396                 :            :             }
     397                 :            :             return mp_const_false;
     398                 :            :         }
     399                 :          4 :         goto unsupported_op;
     400                 :            :     }
     401                 :            : 
     402         [ +  + ]:   66386275 :     if (mp_obj_is_small_int(lhs)) {
     403                 :   65793605 :         mp_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
     404         [ +  + ]:   65793605 :         if (mp_obj_is_small_int(rhs)) {
     405                 :   65783036 :             mp_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
     406                 :            :             // This is a binary operation: lhs_val op rhs_val
     407                 :            :             // We need to be careful to handle overflow; see CERT INT32-C
     408                 :            :             // Operations that can overflow:
     409                 :            :             //      +       result always fits in mp_int_t, then handled by SMALL_INT check
     410                 :            :             //      -       result always fits in mp_int_t, then handled by SMALL_INT check
     411                 :            :             //      *       checked explicitly
     412                 :            :             //      /       if lhs=MIN and rhs=-1; result always fits in mp_int_t, then handled by SMALL_INT check
     413                 :            :             //      %       if lhs=MIN and rhs=-1; result always fits in mp_int_t, then handled by SMALL_INT check
     414                 :            :             //      <<      checked explicitly
     415   [ +  +  +  +  :   65783036 :             switch (op) {
          +  +  +  +  +  
          +  +  +  +  +  
             +  +  +  + ]
     416                 :        532 :                 case MP_BINARY_OP_OR:
     417                 :            :                 case MP_BINARY_OP_INPLACE_OR:
     418                 :        532 :                     lhs_val |= rhs_val;
     419                 :        532 :                     break;
     420                 :   13708632 :                 case MP_BINARY_OP_XOR:
     421                 :            :                 case MP_BINARY_OP_INPLACE_XOR:
     422                 :   13708632 :                     lhs_val ^= rhs_val;
     423                 :   13708632 :                     break;
     424                 :    6721816 :                 case MP_BINARY_OP_AND:
     425                 :            :                 case MP_BINARY_OP_INPLACE_AND:
     426                 :    6721816 :                     lhs_val &= rhs_val;
     427                 :    6721816 :                     break;
     428                 :    4638028 :                 case MP_BINARY_OP_LSHIFT:
     429                 :            :                 case MP_BINARY_OP_INPLACE_LSHIFT: {
     430         [ +  + ]:    4638028 :                     if (rhs_val < 0) {
     431                 :            :                         // negative shift not allowed
     432                 :          8 :                         mp_raise_ValueError(MP_ERROR_TEXT("negative shift count"));
     433         [ +  + ]:    4638020 :                     } else if (rhs_val >= (mp_int_t)(sizeof(lhs_val) * MP_BITS_PER_BYTE)
     434         [ +  + ]:    4637474 :                                || lhs_val > (MP_SMALL_INT_MAX >> rhs_val)
     435         [ -  + ]:    4637182 :                                || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) {
     436                 :            :                         // left-shift will overflow, so use higher precision integer
     437                 :        838 :                         lhs = mp_obj_new_int_from_ll(lhs_val);
     438                 :        838 :                         goto generic_binary_op;
     439                 :            :                     } else {
     440                 :            :                         // use standard precision
     441                 :    4637182 :                         lhs_val = (mp_uint_t)lhs_val << rhs_val;
     442                 :            :                     }
     443                 :    4637182 :                     break;
     444                 :            :                 }
     445                 :        700 :                 case MP_BINARY_OP_RSHIFT:
     446                 :            :                 case MP_BINARY_OP_INPLACE_RSHIFT:
     447         [ +  + ]:        700 :                     if (rhs_val < 0) {
     448                 :            :                         // negative shift not allowed
     449                 :          4 :                         mp_raise_ValueError(MP_ERROR_TEXT("negative shift count"));
     450                 :            :                     } else {
     451                 :            :                         // standard precision is enough for right-shift
     452         [ +  + ]:        696 :                         if (rhs_val >= (mp_int_t)(sizeof(lhs_val) * MP_BITS_PER_BYTE)) {
     453                 :            :                             // Shifting to big amounts is underfined behavior
     454                 :            :                             // in C and is CPU-dependent; propagate sign bit.
     455                 :         12 :                             rhs_val = sizeof(lhs_val) * MP_BITS_PER_BYTE - 1;
     456                 :            :                         }
     457                 :        696 :                         lhs_val >>= rhs_val;
     458                 :            :                     }
     459                 :        696 :                     break;
     460                 :   20105523 :                 case MP_BINARY_OP_ADD:
     461                 :            :                 case MP_BINARY_OP_INPLACE_ADD:
     462                 :   20105523 :                     lhs_val += rhs_val;
     463                 :   20105523 :                     break;
     464                 :      94367 :                 case MP_BINARY_OP_SUBTRACT:
     465                 :            :                 case MP_BINARY_OP_INPLACE_SUBTRACT:
     466                 :      94367 :                     lhs_val -= rhs_val;
     467                 :      94367 :                     break;
     468                 :    3385740 :                 case MP_BINARY_OP_MULTIPLY:
     469                 :            :                 case MP_BINARY_OP_INPLACE_MULTIPLY: {
     470                 :            : 
     471                 :            :                     // If long long type exists and is larger than mp_int_t, then
     472                 :            :                     // we can use the following code to perform overflow-checked multiplication.
     473                 :            :                     // Otherwise (eg in x64 case) we must use mp_small_int_mul_overflow.
     474                 :            :                     #if 0
     475                 :            :                     // compute result using long long precision
     476                 :            :                     long long res = (long long)lhs_val * (long long)rhs_val;
     477                 :            :                     if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
     478                 :            :                         // result overflowed SMALL_INT, so return higher precision integer
     479                 :            :                         return mp_obj_new_int_from_ll(res);
     480                 :            :                     } else {
     481                 :            :                         // use standard precision
     482                 :            :                         lhs_val = (mp_int_t)res;
     483                 :            :                     }
     484                 :            :                     #endif
     485                 :            : 
     486         [ +  + ]:    3385740 :                     if (mp_small_int_mul_overflow(lhs_val, rhs_val)) {
     487                 :            :                         // use higher precision
     488                 :        147 :                         lhs = mp_obj_new_int_from_ll(lhs_val);
     489                 :        147 :                         goto generic_binary_op;
     490                 :            :                     } else {
     491                 :            :                         // use standard precision
     492                 :    3368973 :                         return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
     493                 :            :                     }
     494                 :            :                 }
     495                 :       5153 :                 case MP_BINARY_OP_FLOOR_DIVIDE:
     496                 :            :                 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
     497         [ +  + ]:       5153 :                     if (rhs_val == 0) {
     498                 :          9 :                         goto zero_division;
     499                 :            :                     }
     500                 :       5144 :                     lhs_val = mp_small_int_floor_divide(lhs_val, rhs_val);
     501                 :       5144 :                     break;
     502                 :            : 
     503                 :            :                 #if MICROPY_PY_BUILTINS_FLOAT
     504                 :        244 :                 case MP_BINARY_OP_TRUE_DIVIDE:
     505                 :            :                 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
     506         [ +  + ]:        244 :                     if (rhs_val == 0) {
     507                 :         60 :                         goto zero_division;
     508                 :            :                     }
     509                 :        184 :                     return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
     510                 :            :                 #endif
     511                 :            : 
     512                 :      97388 :                 case MP_BINARY_OP_MODULO:
     513                 :            :                 case MP_BINARY_OP_INPLACE_MODULO: {
     514         [ +  + ]:      97388 :                     if (rhs_val == 0) {
     515                 :          4 :                         goto zero_division;
     516                 :            :                     }
     517                 :      97384 :                     lhs_val = mp_small_int_modulo(lhs_val, rhs_val);
     518                 :      97384 :                     break;
     519                 :            :                 }
     520                 :            : 
     521                 :       1958 :                 case MP_BINARY_OP_POWER:
     522                 :            :                 case MP_BINARY_OP_INPLACE_POWER:
     523         [ +  + ]:       1958 :                     if (rhs_val < 0) {
     524                 :            :                         #if MICROPY_PY_BUILTINS_FLOAT
     525                 :        520 :                         return mp_obj_float_binary_op(op, (mp_float_t)lhs_val, rhs);
     526                 :            :                         #else
     527                 :            :                         mp_raise_ValueError(MP_ERROR_TEXT("negative power with no float support"));
     528                 :            :                         #endif
     529                 :            :                     } else {
     530                 :            :                         mp_int_t ans = 1;
     531         [ +  + ]:       3900 :                         while (rhs_val > 0) {
     532         [ +  + ]:       3698 :                             if (rhs_val & 1) {
     533         [ +  + ]:       2026 :                                 if (mp_small_int_mul_overflow(ans, lhs_val)) {
     534                 :        160 :                                     goto power_overflow;
     535                 :            :                                 }
     536                 :       1866 :                                 ans *= lhs_val;
     537                 :            :                             }
     538         [ +  + ]:       3538 :                             if (rhs_val == 1) {
     539                 :            :                                 break;
     540                 :            :                             }
     541                 :       2664 :                             rhs_val /= 2;
     542         [ +  + ]:       2664 :                             if (mp_small_int_mul_overflow(lhs_val, lhs_val)) {
     543                 :        202 :                                 goto power_overflow;
     544                 :            :                             }
     545                 :       2462 :                             lhs_val *= lhs_val;
     546                 :            :                         }
     547                 :            :                         lhs_val = ans;
     548                 :            :                     }
     549                 :            :                     break;
     550                 :            : 
     551                 :        362 :                 power_overflow:
     552                 :            :                     // use higher precision
     553                 :        362 :                     lhs = mp_obj_new_int_from_ll(MP_OBJ_SMALL_INT_VALUE(lhs));
     554                 :        362 :                     goto generic_binary_op;
     555                 :            : 
     556                 :        178 :                 case MP_BINARY_OP_DIVMOD: {
     557         [ +  + ]:        178 :                     if (rhs_val == 0) {
     558                 :          4 :                         goto zero_division;
     559                 :            :                     }
     560                 :            :                     // to reduce stack usage we don't pass a temp array of the 2 items
     561                 :        174 :                     mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
     562                 :        174 :                     tuple->items[0] = MP_OBJ_NEW_SMALL_INT(mp_small_int_floor_divide(lhs_val, rhs_val));
     563                 :        174 :                     tuple->items[1] = MP_OBJ_NEW_SMALL_INT(mp_small_int_modulo(lhs_val, rhs_val));
     564                 :        174 :                     return MP_OBJ_FROM_PTR(tuple);
     565                 :            :                 }
     566                 :            : 
     567                 :   16286890 :                 case MP_BINARY_OP_LESS:
     568         [ +  + ]:   20789479 :                     return mp_obj_new_bool(lhs_val < rhs_val);
     569                 :     720854 :                 case MP_BINARY_OP_MORE:
     570         [ +  + ]:    1261587 :                     return mp_obj_new_bool(lhs_val > rhs_val);
     571                 :       1366 :                 case MP_BINARY_OP_LESS_EQUAL:
     572         [ +  + ]:       1614 :                     return mp_obj_new_bool(lhs_val <= rhs_val);
     573                 :      13659 :                 case MP_BINARY_OP_MORE_EQUAL:
     574         [ +  + ]:      15213 :                     return mp_obj_new_bool(lhs_val >= rhs_val);
     575                 :            : 
     576                 :          8 :                 default:
     577                 :          8 :                     goto unsupported_op;
     578                 :            :             }
     579                 :            :             // This is an inlined version of mp_obj_new_int, for speed
     580         [ +  + ]:   45372352 :             if (MP_SMALL_INT_FITS(lhs_val)) {
     581                 :   45372344 :                 return MP_OBJ_NEW_SMALL_INT(lhs_val);
     582                 :            :             } else {
     583                 :          8 :                 return mp_obj_new_int_from_ll(lhs_val);
     584                 :            :             }
     585                 :            :         #if MICROPY_PY_BUILTINS_FLOAT
     586   [ -  +  -  +  :      10569 :         } else if (mp_obj_is_float(rhs)) {
          -  +  -  +  +  
                +  +  + ]
     587                 :       6056 :             mp_obj_t res = mp_obj_float_binary_op(op, (mp_float_t)lhs_val, rhs);
     588         [ +  + ]:       6056 :             if (res == MP_OBJ_NULL) {
     589                 :          4 :                 goto unsupported_op;
     590                 :            :             } else {
     591                 :            :                 return res;
     592                 :            :             }
     593                 :            :         #endif
     594                 :            :         #if MICROPY_PY_BUILTINS_COMPLEX
     595   [ -  +  -  +  :       4513 :         } else if (mp_obj_is_type(rhs, &mp_type_complex)) {
          -  +  -  +  +  
                +  +  + ]
     596                 :         44 :             mp_obj_t res = mp_obj_complex_binary_op(op, (mp_float_t)lhs_val, 0, rhs);
     597         [ +  + ]:         44 :             if (res == MP_OBJ_NULL) {
     598                 :          4 :                 goto unsupported_op;
     599                 :            :             } else {
     600                 :            :                 return res;
     601                 :            :             }
     602                 :            :         #endif
     603                 :            :         }
     604                 :            :     }
     605                 :            : 
     606                 :            :     // Convert MP_BINARY_OP_IN to MP_BINARY_OP_CONTAINS with swapped args.
     607         [ +  + ]:     597139 :     if (op == MP_BINARY_OP_IN) {
     608                 :      23511 :         op = MP_BINARY_OP_CONTAINS;
     609                 :      23511 :         mp_obj_t temp = lhs;
     610                 :      23511 :         lhs = rhs;
     611                 :      23511 :         rhs = temp;
     612                 :            :     }
     613                 :            : 
     614                 :            :     // generic binary_op supplied by type
     615                 :     598573 :     const mp_obj_type_t *type;
     616                 :     573628 : generic_binary_op:
     617                 :     598573 :     type = mp_obj_get_type(lhs);
     618         [ +  + ]:     598573 :     if (MP_OBJ_TYPE_HAS_SLOT(type, binary_op)) {
     619                 :     598553 :         mp_obj_t result = MP_OBJ_TYPE_GET_SLOT(type, binary_op)(op, lhs, rhs);
     620         [ +  + ]:     598377 :         if (result != MP_OBJ_NULL) {
     621                 :            :             return result;
     622                 :            :         }
     623                 :            :     }
     624                 :            : 
     625                 :            :     #if MICROPY_PY_REVERSE_SPECIAL_METHODS
     626         [ +  + ]:        724 :     if (op >= MP_BINARY_OP_OR && op <= MP_BINARY_OP_POWER) {
     627                 :         87 :         mp_obj_t t = rhs;
     628                 :         87 :         rhs = lhs;
     629                 :         87 :         lhs = t;
     630                 :         87 :         op += MP_BINARY_OP_REVERSE_OR - MP_BINARY_OP_OR;
     631                 :         87 :         goto generic_binary_op;
     632         [ +  + ]:        637 :     } else if (op >= MP_BINARY_OP_REVERSE_OR) {
     633                 :            :         // Convert __rop__ back to __op__ for error message
     634                 :         74 :         mp_obj_t t = rhs;
     635                 :         74 :         rhs = lhs;
     636                 :         74 :         lhs = t;
     637                 :         74 :         op -= MP_BINARY_OP_REVERSE_OR - MP_BINARY_OP_OR;
     638                 :            :     }
     639                 :            :     #endif
     640                 :            : 
     641         [ +  + ]:        637 :     if (op == MP_BINARY_OP_CONTAINS) {
     642                 :            :         // If type didn't support containment then explicitly walk the iterator.
     643                 :            :         // mp_getiter will raise the appropriate exception if lhs is not iterable.
     644                 :        513 :         mp_obj_iter_buf_t iter_buf;
     645                 :        513 :         mp_obj_t iter = mp_getiter(lhs, &iter_buf);
     646                 :        513 :         mp_obj_t next;
     647         [ +  + ]:       1492 :         while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
     648         [ +  + ]:       1381 :             if (mp_obj_equal(next, rhs)) {
     649                 :            :                 return mp_const_true;
     650                 :            :             }
     651                 :            :         }
     652                 :            :         return mp_const_false;
     653                 :            :     }
     654                 :            : 
     655                 :        124 : unsupported_op:
     656                 :            :     #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     657                 :            :     mp_raise_TypeError(MP_ERROR_TEXT("unsupported type for operator"));
     658                 :            :     #else
     659                 :        148 :     mp_raise_msg_varg(&mp_type_TypeError,
     660                 :        148 :         MP_ERROR_TEXT("unsupported types for %q: '%s', '%s'"),
     661                 :        148 :         mp_binary_op_method_name[op], mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs));
     662                 :            :     #endif
     663                 :            : 
     664                 :         77 : zero_division:
     665                 :         77 :     mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("divide by zero"));
     666                 :            : }
     667                 :            : 
     668                 :       6053 : mp_obj_t mp_call_function_0(mp_obj_t fun) {
     669                 :       6053 :     return mp_call_function_n_kw(fun, 0, 0, NULL);
     670                 :            : }
     671                 :            : 
     672                 :      41404 : mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg) {
     673                 :      41404 :     return mp_call_function_n_kw(fun, 1, 0, &arg);
     674                 :            : }
     675                 :            : 
     676                 :          4 : mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
     677                 :          4 :     mp_obj_t args[2];
     678                 :          4 :     args[0] = arg1;
     679                 :          4 :     args[1] = arg2;
     680                 :          4 :     return mp_call_function_n_kw(fun, 2, 0, args);
     681                 :            : }
     682                 :            : 
     683                 :            : // args contains, eg: arg0  arg1  key0  value0  key1  value1
     684                 :   11142676 : mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
     685                 :            :     // TODO improve this: fun object can specify its type and we parse here the arguments,
     686                 :            :     // passing to the function arrays of fixed and keyword arguments
     687                 :            : 
     688                 :   11142676 :     DEBUG_OP_printf("calling function %p(n_args=" UINT_FMT ", n_kw=" UINT_FMT ", args=%p)\n", fun_in, n_args, n_kw, args);
     689                 :            : 
     690                 :            :     // get the type
     691                 :   11142676 :     const mp_obj_type_t *type = mp_obj_get_type(fun_in);
     692                 :            : 
     693                 :            :     // do the call
     694         [ +  + ]:   11222390 :     if (MP_OBJ_TYPE_HAS_SLOT(type, call)) {
     695                 :   11222386 :         return MP_OBJ_TYPE_GET_SLOT(type, call)(fun_in, n_args, n_kw, args);
     696                 :            :     }
     697                 :            : 
     698                 :            :     #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     699                 :            :     mp_raise_TypeError(MP_ERROR_TEXT("object not callable"));
     700                 :            :     #else
     701                 :          4 :     mp_raise_msg_varg(&mp_type_TypeError,
     702                 :          4 :         MP_ERROR_TEXT("'%s' object isn't callable"), mp_obj_get_type_str(fun_in));
     703                 :            :     #endif
     704                 :            : }
     705                 :            : 
     706                 :            : // args contains: fun  self/NULL  arg(0)  ...  arg(n_args-2)  arg(n_args-1)  kw_key(0)  kw_val(0)  ... kw_key(n_kw-1)  kw_val(n_kw-1)
     707                 :            : // if n_args==0 and n_kw==0 then there are only fun and self/NULL
     708                 :    1979796 : mp_obj_t mp_call_method_n_kw(size_t n_args, size_t n_kw, const mp_obj_t *args) {
     709                 :    1979796 :     DEBUG_OP_printf("call method (fun=%p, self=%p, n_args=" UINT_FMT ", n_kw=" UINT_FMT ", args=%p)\n", args[0], args[1], n_args, n_kw, args);
     710                 :    1979796 :     int adjust = (args[1] == MP_OBJ_NULL) ? 0 : 1;
     711                 :    1979796 :     return mp_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
     712                 :            : }
     713                 :            : 
     714                 :            : // This function only needs to be exposed externally when in stackless mode.
     715                 :            : #if !MICROPY_STACKLESS
     716                 :            : STATIC
     717                 :            : #endif
     718                 :       8478 : void mp_call_prepare_args_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args, mp_call_args_t *out_args) {
     719                 :       8478 :     mp_obj_t fun = *args++;
     720                 :       8478 :     mp_obj_t self = MP_OBJ_NULL;
     721         [ +  + ]:       8478 :     if (have_self) {
     722                 :       3774 :         self = *args++; // may be MP_OBJ_NULL
     723                 :            :     }
     724                 :       8478 :     size_t n_args = n_args_n_kw & 0xff;
     725                 :       8478 :     size_t n_kw = (n_args_n_kw >> 8) & 0xff;
     726                 :       8478 :     mp_uint_t star_args = MP_OBJ_SMALL_INT_VALUE(args[n_args + 2 * n_kw]);
     727                 :            : 
     728                 :       8478 :     DEBUG_OP_printf("call method var (fun=%p, self=%p, n_args=%u, n_kw=%u, args=%p, map=%u)\n", fun, self, n_args, n_kw, args, star_args);
     729                 :            : 
     730                 :            :     // We need to create the following array of objects:
     731                 :            :     //     args[0 .. n_args]  unpacked(pos_seq)  args[n_args .. n_args + 2 * n_kw]  unpacked(kw_dict)
     732                 :            :     // TODO: optimize one day to avoid constructing new arg array? Will be hard.
     733                 :            : 
     734                 :            :     // The new args array
     735                 :       8478 :     mp_obj_t *args2;
     736                 :       8478 :     size_t args2_alloc;
     737                 :       8478 :     size_t args2_len = 0;
     738                 :            : 
     739                 :            :     // Try to get a hint for unpacked * args length
     740                 :       8478 :     ssize_t list_len = 0;
     741                 :            : 
     742         [ +  + ]:       8478 :     if (star_args != 0) {
     743         [ +  + ]:      19846 :         for (size_t i = 0; i < n_args; i++) {
     744         [ +  + ]:      11630 :             if ((star_args >> i) & 1) {
     745                 :       8244 :                 mp_obj_t len = mp_obj_len_maybe(args[i]);
     746         [ +  + ]:       8244 :                 if (len != MP_OBJ_NULL) {
     747                 :            :                     // -1 accounts for 1 of n_args occupied by this arg
     748                 :       8148 :                     list_len += mp_obj_get_int(len) - 1;
     749                 :            :                 }
     750                 :            :             }
     751                 :            :         }
     752                 :            :     }
     753                 :            : 
     754                 :            :     // Try to get a hint for the size of the kw_dict
     755                 :       8478 :     ssize_t kw_dict_len = 0;
     756                 :            : 
     757         [ +  + ]:       8838 :     for (size_t i = 0; i < n_kw; i++) {
     758                 :        360 :         mp_obj_t key = args[n_args + i * 2];
     759                 :        360 :         mp_obj_t value = args[n_args + i * 2 + 1];
     760   [ +  +  -  +  :        360 :         if (key == MP_OBJ_NULL && value != MP_OBJ_NULL && mp_obj_is_type(value, &mp_type_dict)) {
          -  +  -  +  -  
             +  +  -  +  
                      + ]
     761                 :            :             // -1 accounts for 1 of n_kw occupied by this arg
     762                 :        324 :             kw_dict_len += mp_obj_dict_len(value) - 1;
     763                 :            :         }
     764                 :            :     }
     765                 :            : 
     766                 :            :     // Extract the pos_seq sequence to the new args array.
     767                 :            :     // Note that it can be arbitrary iterator.
     768         [ +  + ]:       8478 :     if (star_args == 0) {
     769                 :            :         // no star args to unpack
     770                 :            : 
     771                 :            :         // allocate memory for the new array of args
     772                 :        262 :         args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len);
     773                 :        262 :         args2 = mp_nonlocal_alloc(args2_alloc * sizeof(mp_obj_t));
     774                 :            : 
     775                 :            :         // copy the self
     776         [ +  + ]:        262 :         if (self != MP_OBJ_NULL) {
     777                 :         34 :             args2[args2_len++] = self;
     778                 :            :         }
     779                 :            : 
     780                 :            :         // copy the fixed pos args
     781                 :        262 :         mp_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
     782                 :        262 :         args2_len += n_args;
     783                 :            :     } else {
     784                 :            :         // at least one star arg to unpack
     785                 :            : 
     786                 :            :         // allocate memory for the new array of args
     787                 :       8216 :         args2_alloc = 1 + n_args + list_len + 2 * (n_kw + kw_dict_len);
     788                 :       8216 :         args2 = mp_nonlocal_alloc(args2_alloc * sizeof(mp_obj_t));
     789                 :            : 
     790                 :            :         // copy the self
     791         [ +  + ]:       8216 :         if (self != MP_OBJ_NULL) {
     792                 :       3728 :             args2[args2_len++] = self;
     793                 :            :         }
     794                 :            : 
     795         [ +  + ]:      19846 :         for (size_t i = 0; i < n_args; i++) {
     796                 :      11630 :             mp_obj_t arg = args[i];
     797         [ +  + ]:      11630 :             if ((star_args >> i) & 1) {
     798                 :            :                 // star arg
     799   [ -  +  -  +  :       8244 :                 if (mp_obj_is_type(arg, &mp_type_tuple) || mp_obj_is_type(arg, &mp_type_list)) {
          -  +  -  +  +  
          -  +  +  -  +  
          -  +  -  +  -  
             +  +  -  +  
                      + ]
     800                 :            :                     // optimise the case of a tuple and list
     801                 :            : 
     802                 :            :                     // get the items
     803                 :       8132 :                     size_t len;
     804                 :       8132 :                     mp_obj_t *items;
     805                 :       8132 :                     mp_obj_get_array(arg, &len, &items);
     806                 :            : 
     807                 :            :                     // copy the items
     808         [ -  + ]:       8132 :                     assert(args2_len + len <= args2_alloc);
     809                 :       8132 :                     mp_seq_copy(args2 + args2_len, items, len, mp_obj_t);
     810                 :       8132 :                     args2_len += len;
     811                 :            :                 } else {
     812                 :            :                     // generic iterator
     813                 :            : 
     814                 :            :                     // extract the variable position args from the iterator
     815                 :        112 :                     mp_obj_iter_buf_t iter_buf;
     816                 :        112 :                     mp_obj_t iterable = mp_getiter(arg, &iter_buf);
     817                 :        112 :                     mp_obj_t item;
     818         [ +  + ]:        928 :                     while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
     819         [ +  + ]:        816 :                         if (args2_len + (n_args - i) >= args2_alloc) {
     820                 :         92 :                             args2 = mp_nonlocal_realloc(args2, args2_alloc * sizeof(mp_obj_t),
     821                 :            :                                 args2_alloc * 2 * sizeof(mp_obj_t));
     822                 :         92 :                             args2_alloc *= 2;
     823                 :            :                         }
     824                 :        816 :                         args2[args2_len++] = item;
     825                 :            :                     }
     826                 :            :                 }
     827                 :            :             } else {
     828                 :            :                 // normal argument
     829         [ -  + ]:       3386 :                 assert(args2_len < args2_alloc);
     830                 :       3386 :                 args2[args2_len++] = arg;
     831                 :            :             }
     832                 :            :         }
     833                 :            :     }
     834                 :            : 
     835                 :            :     // The size of the args2 array now is the number of positional args.
     836                 :       8478 :     size_t pos_args_len = args2_len;
     837                 :            : 
     838                 :            :     // ensure there is still enough room for kw args
     839         [ +  + ]:       8478 :     if (args2_len + 2 * (n_kw + kw_dict_len) > args2_alloc) {
     840                 :          4 :         size_t new_alloc = args2_len + 2 * (n_kw + kw_dict_len);
     841                 :          4 :         args2 = mp_nonlocal_realloc(args2, args2_alloc * sizeof(mp_obj_t),
     842                 :            :             new_alloc * sizeof(mp_obj_t));
     843                 :          4 :         args2_alloc = new_alloc;
     844                 :            :     }
     845                 :            : 
     846                 :            :     // Copy the kw args.
     847         [ +  + ]:       8828 :     for (size_t i = 0; i < n_kw; i++) {
     848                 :        360 :         mp_obj_t kw_key = args[n_args + i * 2];
     849                 :        360 :         mp_obj_t kw_value = args[n_args + i * 2 + 1];
     850         [ +  + ]:        360 :         if (kw_key == MP_OBJ_NULL) {
     851                 :            :             // double-star args
     852   [ -  +  -  +  :        328 :             if (mp_obj_is_type(kw_value, &mp_type_dict)) {
          -  +  -  +  +  
                -  +  + ]
     853                 :            :                 // dictionary
     854         [ -  + ]:        324 :                 mp_map_t *map = mp_obj_dict_get_map(kw_value);
     855                 :            :                 // should have enough, since kw_dict_len is in this case hinted correctly above
     856         [ -  + ]:        324 :                 assert(args2_len + 2 * map->used <= args2_alloc);
     857         [ +  + ]:       1100 :                 for (size_t j = 0; j < map->alloc; j++) {
     858         [ +  + ]:        786 :                     if (mp_map_slot_is_filled(map, j)) {
     859                 :            :                         // the key must be a qstr, so intern it if it's a string
     860                 :        670 :                         mp_obj_t key = map->table[j].key;
     861         [ +  + ]:        670 :                         if (!mp_obj_is_qstr(key)) {
     862                 :        414 :                             key = mp_obj_str_intern_checked(key);
     863                 :            :                         }
     864                 :        660 :                         args2[args2_len++] = key;
     865                 :        660 :                         args2[args2_len++] = map->table[j].value;
     866                 :            :                     }
     867                 :            :                 }
     868                 :            :             } else {
     869                 :            :                 // generic mapping:
     870                 :            :                 // - call keys() to get an iterable of all keys in the mapping
     871                 :            :                 // - call __getitem__ for each key to get the corresponding value
     872                 :            : 
     873                 :            :                 // get the keys iterable
     874                 :          4 :                 mp_obj_t dest[3];
     875                 :          4 :                 mp_load_method(kw_value, MP_QSTR_keys, dest);
     876                 :          4 :                 mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest), NULL);
     877                 :            : 
     878                 :          4 :                 mp_obj_t key;
     879         [ +  + ]:         20 :                 while ((key = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
     880                 :            :                     // expand size of args array if needed
     881         [ +  + ]:         16 :                     if (args2_len + 1 >= args2_alloc) {
     882                 :          8 :                         size_t new_alloc = args2_alloc * 2;
     883                 :          8 :                         args2 = mp_nonlocal_realloc(args2, args2_alloc * sizeof(mp_obj_t), new_alloc * sizeof(mp_obj_t));
     884                 :          8 :                         args2_alloc = new_alloc;
     885                 :            :                     }
     886                 :            : 
     887                 :            :                     // the key must be a qstr, so intern it if it's a string
     888         [ +  + ]:         16 :                     if (!mp_obj_is_qstr(key)) {
     889                 :          4 :                         key = mp_obj_str_intern_checked(key);
     890                 :            :                     }
     891                 :            : 
     892                 :            :                     // get the value corresponding to the key
     893                 :         16 :                     mp_load_method(kw_value, MP_QSTR___getitem__, dest);
     894                 :         16 :                     dest[2] = key;
     895                 :         16 :                     mp_obj_t value = mp_call_method_n_kw(1, 0, dest);
     896                 :            : 
     897                 :            :                     // store the key/value pair in the argument array
     898                 :         16 :                     args2[args2_len++] = key;
     899                 :         16 :                     args2[args2_len++] = value;
     900                 :            :                 }
     901                 :            :             }
     902                 :            :         } else {
     903                 :            :             // normal kwarg
     904         [ -  + ]:         32 :             assert(args2_len + 2 <= args2_alloc);
     905                 :         32 :             args2[args2_len++] = kw_key;
     906                 :         32 :             args2[args2_len++] = kw_value;
     907                 :            :         }
     908                 :            :     }
     909                 :            : 
     910                 :       8468 :     out_args->fun = fun;
     911                 :       8468 :     out_args->args = args2;
     912                 :       8468 :     out_args->n_args = pos_args_len;
     913                 :       8468 :     out_args->n_kw = (args2_len - pos_args_len) / 2;
     914                 :       8468 :     out_args->n_alloc = args2_alloc;
     915                 :       8468 : }
     916                 :            : 
     917                 :       8478 : mp_obj_t mp_call_method_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args) {
     918                 :       8478 :     mp_call_args_t out_args;
     919                 :       8478 :     mp_call_prepare_args_n_kw_var(have_self, n_args_n_kw, args, &out_args);
     920                 :            : 
     921                 :       8468 :     mp_obj_t res = mp_call_function_n_kw(out_args.fun, out_args.n_args, out_args.n_kw, out_args.args);
     922                 :       8410 :     mp_nonlocal_free(out_args.args, out_args.n_alloc * sizeof(mp_obj_t));
     923                 :            : 
     924                 :       8410 :     return res;
     925                 :            : }
     926                 :            : 
     927                 :            : // unpacked items are stored in reverse order into the array pointed to by items
     928                 :      39929 : void mp_unpack_sequence(mp_obj_t seq_in, size_t num, mp_obj_t *items) {
     929                 :      39929 :     size_t seq_len;
     930   [ -  +  -  +  :      79798 :     if (mp_obj_is_type(seq_in, &mp_type_tuple) || mp_obj_is_type(seq_in, &mp_type_list)) {
          -  +  -  +  +  
          -  +  +  -  +  
          -  +  -  +  -  
             +  +  -  +  
                      + ]
     931                 :      39877 :         mp_obj_t *seq_items;
     932                 :      39877 :         mp_obj_get_array(seq_in, &seq_len, &seq_items);
     933         [ +  + ]:      39877 :         if (seq_len < num) {
     934                 :          4 :             goto too_short;
     935         [ +  + ]:      39873 :         } else if (seq_len > num) {
     936                 :          4 :             goto too_long;
     937                 :            :         }
     938         [ +  + ]:     119943 :         for (size_t i = 0; i < num; i++) {
     939                 :      80074 :             items[i] = seq_items[num - 1 - i];
     940                 :            :         }
     941                 :            :     } else {
     942                 :         52 :         mp_obj_iter_buf_t iter_buf;
     943                 :         52 :         mp_obj_t iterable = mp_getiter(seq_in, &iter_buf);
     944                 :            : 
     945         [ +  + ]:        164 :         for (seq_len = 0; seq_len < num; seq_len++) {
     946                 :        116 :             mp_obj_t el = mp_iternext(iterable);
     947         [ +  + ]:        116 :             if (el == MP_OBJ_STOP_ITERATION) {
     948                 :          4 :                 goto too_short;
     949                 :            :             }
     950                 :        112 :             items[num - 1 - seq_len] = el;
     951                 :            :         }
     952         [ +  + ]:         48 :         if (mp_iternext(iterable) != MP_OBJ_STOP_ITERATION) {
     953                 :          4 :             goto too_long;
     954                 :            :         }
     955                 :            :     }
     956                 :      39913 :     return;
     957                 :            : 
     958                 :          8 : too_short:
     959                 :            :     #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     960                 :            :     mp_raise_ValueError(MP_ERROR_TEXT("wrong number of values to unpack"));
     961                 :            :     #else
     962                 :          8 :     mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("need more than %d values to unpack"), (int)seq_len);
     963                 :            :     #endif
     964                 :          8 : too_long:
     965                 :            :     #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
     966                 :            :     mp_raise_ValueError(MP_ERROR_TEXT("wrong number of values to unpack"));
     967                 :            :     #else
     968                 :          8 :     mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("too many values to unpack (expected %d)"), (int)num);
     969                 :            :     #endif
     970                 :            : }
     971                 :            : 
     972                 :            : // unpacked items are stored in reverse order into the array pointed to by items
     973                 :        180 : void mp_unpack_ex(mp_obj_t seq_in, size_t num_in, mp_obj_t *items) {
     974                 :        180 :     size_t num_left = num_in & 0xff;
     975                 :        180 :     size_t num_right = (num_in >> 8) & 0xff;
     976                 :        180 :     DEBUG_OP_printf("unpack ex " UINT_FMT " " UINT_FMT "\n", num_left, num_right);
     977                 :        180 :     size_t seq_len;
     978   [ -  +  -  +  :        180 :     if (mp_obj_is_type(seq_in, &mp_type_tuple) || mp_obj_is_type(seq_in, &mp_type_list)) {
          -  +  -  +  +  
          -  +  +  -  +  
          -  +  -  +  -  
             +  +  -  +  
                      + ]
     979                 :            :         // Make the seq variable volatile so the compiler keeps a reference to it,
     980                 :            :         // since if it's a tuple then seq_items points to the interior of the GC cell
     981                 :            :         // and mp_obj_new_list may trigger a GC which doesn't trace this and reclaims seq.
     982                 :        136 :         volatile mp_obj_t seq = seq_in;
     983                 :        136 :         mp_obj_t *seq_items;
     984                 :        136 :         mp_obj_get_array(seq, &seq_len, &seq_items);
     985         [ +  + ]:        136 :         if (seq_len < num_left + num_right) {
     986                 :          4 :             goto too_short;
     987                 :            :         }
     988         [ +  + ]:        164 :         for (size_t i = 0; i < num_right; i++) {
     989                 :         32 :             items[i] = seq_items[seq_len - 1 - i];
     990                 :            :         }
     991                 :        132 :         items[num_right] = mp_obj_new_list(seq_len - num_left - num_right, seq_items + num_left);
     992         [ +  + ]:        224 :         for (size_t i = 0; i < num_left; i++) {
     993                 :         92 :             items[num_right + 1 + i] = seq_items[num_left - 1 - i];
     994                 :            :         }
     995                 :        132 :         seq = MP_OBJ_NULL;
     996                 :            :     } else {
     997                 :            :         // Generic iterable; this gets a bit messy: we unpack known left length to the
     998                 :            :         // items destination array, then the rest to a dynamically created list.  Once the
     999                 :            :         // iterable is exhausted, we take from this list for the right part of the items.
    1000                 :            :         // TODO Improve to waste less memory in the dynamically created list.
    1001                 :         44 :         mp_obj_t iterable = mp_getiter(seq_in, NULL);
    1002                 :         44 :         mp_obj_t item;
    1003         [ +  + ]:         84 :         for (seq_len = 0; seq_len < num_left; seq_len++) {
    1004                 :         44 :             item = mp_iternext(iterable);
    1005         [ +  + ]:         44 :             if (item == MP_OBJ_STOP_ITERATION) {
    1006                 :          4 :                 goto too_short;
    1007                 :            :             }
    1008                 :         40 :             items[num_left + num_right + 1 - 1 - seq_len] = item;
    1009                 :            :         }
    1010                 :         40 :         mp_obj_list_t *rest = MP_OBJ_TO_PTR(mp_obj_new_list(0, NULL));
    1011         [ +  + ]:        184 :         while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
    1012                 :        144 :             mp_obj_list_append(MP_OBJ_FROM_PTR(rest), item);
    1013                 :            :         }
    1014         [ +  + ]:         40 :         if (rest->len < num_right) {
    1015                 :          4 :             goto too_short;
    1016                 :            :         }
    1017                 :         36 :         items[num_right] = MP_OBJ_FROM_PTR(rest);
    1018         [ +  + ]:         72 :         for (size_t i = 0; i < num_right; i++) {
    1019                 :         36 :             items[num_right - 1 - i] = rest->items[rest->len - num_right + i];
    1020                 :            :         }
    1021                 :         36 :         mp_obj_list_set_len(MP_OBJ_FROM_PTR(rest), rest->len - num_right);
    1022                 :            :     }
    1023                 :        168 :     return;
    1024                 :            : 
    1025                 :         12 : too_short:
    1026                 :            :     #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
    1027                 :            :     mp_raise_ValueError(MP_ERROR_TEXT("wrong number of values to unpack"));
    1028                 :            :     #else
    1029                 :         12 :     mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("need more than %d values to unpack"), (int)seq_len);
    1030                 :            :     #endif
    1031                 :            : }
    1032                 :            : 
    1033                 :    2972408 : mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
    1034                 :    2972408 :     DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
    1035                 :            :     // use load_method
    1036                 :    2972408 :     mp_obj_t dest[2];
    1037                 :    2972408 :     mp_load_method(base, attr, dest);
    1038         [ +  + ]:    2972344 :     if (dest[1] == MP_OBJ_NULL) {
    1039                 :            :         // load_method returned just a normal attribute
    1040                 :    2972258 :         return dest[0];
    1041                 :            :     } else {
    1042                 :            :         // load_method returned a method, so build a bound method object
    1043                 :         86 :         return mp_obj_new_bound_meth(dest[0], dest[1]);
    1044                 :            :     }
    1045                 :            : }
    1046                 :            : 
    1047                 :            : #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
    1048                 :            : 
    1049                 :            : // The following "checked fun" type is local to the mp_convert_member_lookup
    1050                 :            : // function, and serves to check that the first argument to a builtin function
    1051                 :            : // has the correct type.
    1052                 :            : 
    1053                 :            : typedef struct _mp_obj_checked_fun_t {
    1054                 :            :     mp_obj_base_t base;
    1055                 :            :     const mp_obj_type_t *type;
    1056                 :            :     mp_obj_t fun;
    1057                 :            : } mp_obj_checked_fun_t;
    1058                 :            : 
    1059                 :         26 : STATIC mp_obj_t checked_fun_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
    1060                 :         26 :     mp_obj_checked_fun_t *self = MP_OBJ_TO_PTR(self_in);
    1061         [ +  + ]:         26 :     if (n_args > 0) {
    1062                 :         22 :         const mp_obj_type_t *arg0_type = mp_obj_get_type(args[0]);
    1063         [ +  + ]:         22 :         if (arg0_type != self->type) {
    1064                 :            :             #if MICROPY_ERROR_REPORTING != MICROPY_ERROR_REPORTING_DETAILED
    1065                 :            :             mp_raise_TypeError(MP_ERROR_TEXT("argument has wrong type"));
    1066                 :            :             #else
    1067                 :         12 :             mp_raise_msg_varg(&mp_type_TypeError,
    1068                 :         12 :                 MP_ERROR_TEXT("argument should be a '%q' not a '%q'"), self->type->name, arg0_type->name);
    1069                 :            :             #endif
    1070                 :            :         }
    1071                 :            :     }
    1072                 :         14 :     return mp_call_function_n_kw(self->fun, n_args, n_kw, args);
    1073                 :            : }
    1074                 :            : 
    1075                 :            : STATIC MP_DEFINE_CONST_OBJ_TYPE(
    1076                 :            :     mp_type_checked_fun,
    1077                 :            :     MP_QSTR_function,
    1078                 :            :     MP_TYPE_FLAG_BINDS_SELF,
    1079                 :            :     call, checked_fun_call
    1080                 :            :     );
    1081                 :            : 
    1082                 :        123 : STATIC mp_obj_t mp_obj_new_checked_fun(const mp_obj_type_t *type, mp_obj_t fun) {
    1083                 :        123 :     mp_obj_checked_fun_t *o = mp_obj_malloc(mp_obj_checked_fun_t, &mp_type_checked_fun);
    1084                 :        123 :     o->type = type;
    1085                 :        123 :     o->fun = fun;
    1086                 :        123 :     return MP_OBJ_FROM_PTR(o);
    1087                 :            : }
    1088                 :            : 
    1089                 :            : #endif // MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
    1090                 :            : 
    1091                 :            : // Given a member that was extracted from an instance, convert it correctly
    1092                 :            : // and put the result in the dest[] array for a possible method call.
    1093                 :            : // Conversion means dealing with static/class methods, callables, and values.
    1094                 :            : // see http://docs.python.org/3/howto/descriptor.html
    1095                 :            : // and also https://mail.python.org/pipermail/python-dev/2015-March/138950.html
    1096                 :    2088318 : void mp_convert_member_lookup(mp_obj_t self, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest) {
    1097         [ +  + ]:    2088318 :     if (mp_obj_is_obj(member)) {
    1098                 :    1632743 :         const mp_obj_type_t *m_type = ((mp_obj_base_t *)MP_OBJ_TO_PTR(member))->type;
    1099         [ +  + ]:    1632743 :         if (m_type->flags & MP_TYPE_FLAG_BINDS_SELF) {
    1100                 :            :             // `member` is a function that binds self as its first argument.
    1101         [ +  + ]:    1632279 :             if (m_type->flags & MP_TYPE_FLAG_BUILTIN_FUN) {
    1102                 :            :                 // `member` is a built-in function, which has special behaviour.
    1103         [ +  + ]:    1013390 :                 if (mp_obj_is_instance_type(type)) {
    1104                 :            :                     // Built-in functions on user types always behave like a staticmethod.
    1105                 :          8 :                     dest[0] = member;
    1106                 :            :                 }
    1107                 :            :                 #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
    1108   [ +  +  +  + ]:    1013382 :                 else if (self == MP_OBJ_NULL && type != &mp_type_object) {
    1109                 :            :                     // `member` is a built-in method without a first argument, so wrap
    1110                 :            :                     // it in a type checker that will check self when it's supplied.
    1111                 :            :                     // Note that object will do its own checking so shouldn't be wrapped.
    1112                 :        123 :                     dest[0] = mp_obj_new_checked_fun(type, member);
    1113                 :            :                 }
    1114                 :            :                 #endif
    1115                 :            :                 else {
    1116                 :            :                     // Return a (built-in) bound method, with self being this object.
    1117                 :    1013259 :                     dest[0] = member;
    1118                 :    1013259 :                     dest[1] = self;
    1119                 :            :                 }
    1120                 :            :             } else {
    1121                 :            :                 // Return a bound method, with self being this object.
    1122                 :     618889 :                 dest[0] = member;
    1123                 :     618889 :                 dest[1] = self;
    1124                 :            :             }
    1125         [ +  + ]:        464 :         } else if (m_type == &mp_type_staticmethod) {
    1126                 :            :             // `member` is a staticmethod, return the function that it wraps.
    1127                 :        158 :             dest[0] = ((mp_obj_static_class_method_t *)MP_OBJ_TO_PTR(member))->fun;
    1128         [ +  + ]:        306 :         } else if (m_type == &mp_type_classmethod) {
    1129                 :            :             // `member` is a classmethod, return a bound method with self being the type of
    1130                 :            :             // this object.  This type should be the type of the original instance, not the
    1131                 :            :             // base type (which is what is passed in the `type` argument to this function).
    1132         [ +  + ]:        114 :             if (self != MP_OBJ_NULL) {
    1133                 :         12 :                 type = mp_obj_get_type(self);
    1134                 :            :             }
    1135                 :        114 :             dest[0] = ((mp_obj_static_class_method_t *)MP_OBJ_TO_PTR(member))->fun;
    1136                 :        114 :             dest[1] = MP_OBJ_FROM_PTR(type);
    1137                 :            :         } else {
    1138                 :            :             // `member` is a value, so just return that value.
    1139                 :        192 :             dest[0] = member;
    1140                 :            :         }
    1141                 :            :     } else {
    1142                 :            :         // `member` is a value, so just return that value.
    1143                 :     455575 :         dest[0] = member;
    1144                 :            :     }
    1145                 :    2088318 : }
    1146                 :            : 
    1147                 :            : // no attribute found, returns:     dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
    1148                 :            : // normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
    1149                 :            : // method attribute found, returns: dest[0] == <method>,    dest[1] == <self>
    1150                 :    4850886 : void mp_load_method_maybe(mp_obj_t obj, qstr attr, mp_obj_t *dest) {
    1151                 :            :     // clear output to indicate no attribute/method found yet
    1152                 :    4850886 :     dest[0] = MP_OBJ_NULL;
    1153                 :    4850886 :     dest[1] = MP_OBJ_NULL;
    1154                 :            : 
    1155                 :            :     // Note: the specific case of obj being an instance type is fast-path'ed in the VM
    1156                 :            :     // for the MP_BC_LOAD_ATTR opcode. Instance types handle type->attr and look up directly
    1157                 :            :     // in their member's map.
    1158                 :            : 
    1159                 :            :     // get the type
    1160                 :    4850886 :     const mp_obj_type_t *type = mp_obj_get_type(obj);
    1161                 :            : 
    1162                 :            :     // look for built-in names
    1163                 :            :     #if MICROPY_CPYTHON_COMPAT
    1164         [ +  + ]:    4856177 :     if (attr == MP_QSTR___class__) {
    1165                 :            :         // a.__class__ is equivalent to type(a)
    1166                 :         43 :         dest[0] = MP_OBJ_FROM_PTR(type);
    1167                 :         43 :         return;
    1168                 :            :     }
    1169                 :            :     #endif
    1170                 :            : 
    1171   [ +  +  +  + ]:    4856134 :     if (attr == MP_QSTR___next__ && TYPE_HAS_ITERNEXT(type)) {
    1172                 :          4 :         dest[0] = MP_OBJ_FROM_PTR(&mp_builtin_next_obj);
    1173                 :          4 :         dest[1] = obj;
    1174                 :          4 :         return;
    1175                 :            :     }
    1176         [ +  + ]:    4856130 :     if (MP_OBJ_TYPE_HAS_SLOT(type, attr)) {
    1177                 :            :         // this type can do its own load, so call it
    1178                 :    3832873 :         MP_OBJ_TYPE_GET_SLOT(type, attr)(obj, attr, dest);
    1179                 :            :         // If type->attr has set dest[1] = MP_OBJ_SENTINEL, we should proceed
    1180                 :            :         // with lookups below (i.e. in locals_dict). If not, return right away.
    1181         [ +  + ]:    3832793 :         if (dest[1] != MP_OBJ_SENTINEL) {
    1182                 :            :             return;
    1183                 :            :         }
    1184                 :            :         // Clear the fail flag set by type->attr so it's like it never ran.
    1185                 :         36 :         dest[1] = MP_OBJ_NULL;
    1186                 :            :     }
    1187         [ +  + ]:    1023293 :     if (MP_OBJ_TYPE_HAS_SLOT(type, locals_dict)) {
    1188                 :            :         // generic method lookup
    1189                 :            :         // this is a lookup in the object (ie not class or type)
    1190         [ -  + ]:    1022474 :         assert(MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->base.type == &mp_type_dict); // MicroPython restriction, for now
    1191                 :    1022474 :         mp_map_t *locals_map = &MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->map;
    1192                 :    1022474 :         mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
    1193         [ +  + ]:    1022460 :         if (elem != NULL) {
    1194                 :    1013181 :             mp_convert_member_lookup(obj, type, elem->value, dest);
    1195                 :            :         }
    1196                 :    1022451 :         return;
    1197                 :            :     }
    1198                 :            : }
    1199                 :            : 
    1200                 :    4486255 : void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
    1201                 :    4486255 :     DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
    1202                 :            : 
    1203                 :    4486255 :     mp_load_method_maybe(base, attr, dest);
    1204                 :            : 
    1205         [ +  + ]:    4486153 :     if (dest[0] == MP_OBJ_NULL) {
    1206                 :            :         // no attribute/method called attr
    1207                 :            :         #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
    1208                 :            :         mp_raise_msg(&mp_type_AttributeError, MP_ERROR_TEXT("no such attribute"));
    1209                 :            :         #else
    1210                 :            :         // following CPython, we give a more detailed error message for type objects
    1211   [ -  +  -  +  :         74 :         if (mp_obj_is_type(base, &mp_type_type)) {
          -  +  -  +  +  
                +  +  + ]
    1212                 :          8 :             mp_raise_msg_varg(&mp_type_AttributeError,
    1213                 :          8 :                 MP_ERROR_TEXT("type object '%q' has no attribute '%q'"),
    1214                 :          8 :                 ((mp_obj_type_t *)MP_OBJ_TO_PTR(base))->name, attr);
    1215                 :            :         } else {
    1216                 :         66 :             mp_raise_msg_varg(&mp_type_AttributeError,
    1217                 :         66 :                 MP_ERROR_TEXT("'%s' object has no attribute '%q'"),
    1218                 :            :                 mp_obj_get_type_str(base), attr);
    1219                 :            :         }
    1220                 :            :         #endif
    1221                 :            :     }
    1222                 :    4486079 : }
    1223                 :            : 
    1224                 :            : // Acts like mp_load_method_maybe but catches AttributeError, and all other exceptions if requested
    1225                 :      45491 : void mp_load_method_protected(mp_obj_t obj, qstr attr, mp_obj_t *dest, bool catch_all_exc) {
    1226                 :      45491 :     nlr_buf_t nlr;
    1227         [ +  + ]:      45491 :     if (nlr_push(&nlr) == 0) {
    1228                 :      45491 :         mp_load_method_maybe(obj, attr, dest);
    1229                 :      45479 :         nlr_pop();
    1230                 :            :     } else {
    1231         [ +  - ]:         12 :         if (!catch_all_exc
    1232         [ +  + ]:         12 :             && !mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t *)nlr.ret_val)->type),
    1233                 :            :                 MP_OBJ_FROM_PTR(&mp_type_AttributeError))) {
    1234                 :            :             // Re-raise the exception
    1235                 :          4 :             nlr_raise(MP_OBJ_FROM_PTR(nlr.ret_val));
    1236                 :            :         }
    1237                 :            :     }
    1238                 :      45487 : }
    1239                 :            : 
    1240                 :     478889 : void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
    1241                 :     478889 :     DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
    1242                 :     478889 :     const mp_obj_type_t *type = mp_obj_get_type(base);
    1243         [ +  - ]:     478889 :     if (MP_OBJ_TYPE_HAS_SLOT(type, attr)) {
    1244                 :     478889 :         mp_obj_t dest[2] = {MP_OBJ_SENTINEL, value};
    1245                 :     478889 :         MP_OBJ_TYPE_GET_SLOT(type, attr)(base, attr, dest);
    1246         [ +  + ]:     478882 :         if (dest[0] == MP_OBJ_NULL) {
    1247                 :            :             // success
    1248                 :     478806 :             return;
    1249                 :            :         }
    1250                 :            :     }
    1251                 :            :     #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
    1252                 :            :     mp_raise_msg(&mp_type_AttributeError, MP_ERROR_TEXT("no such attribute"));
    1253                 :            :     #else
    1254                 :         76 :     mp_raise_msg_varg(&mp_type_AttributeError,
    1255                 :         76 :         MP_ERROR_TEXT("'%s' object has no attribute '%q'"),
    1256                 :            :         mp_obj_get_type_str(base), attr);
    1257                 :            :     #endif
    1258                 :            : }
    1259                 :            : 
    1260                 :     352139 : mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
    1261         [ -  + ]:     352139 :     assert(o_in);
    1262                 :     352139 :     const mp_obj_type_t *type = mp_obj_get_type(o_in);
    1263                 :            : 
    1264                 :            :     // Most types that use iternext just use the identity getiter. We handle this case explicitly
    1265                 :            :     // so we don't unnecessarily allocate any RAM for the iter_buf, which won't be used.
    1266   [ +  +  +  - ]:     352139 :     if ((type->flags & MP_TYPE_FLAG_ITER_IS_ITERNEXT) == MP_TYPE_FLAG_ITER_IS_ITERNEXT || (type->flags & MP_TYPE_FLAG_ITER_IS_STREAM) == MP_TYPE_FLAG_ITER_IS_STREAM) {
    1267                 :            :         return o_in;
    1268                 :            :     }
    1269                 :            : 
    1270         [ +  + ]:     186857 :     if (MP_OBJ_TYPE_HAS_SLOT(type, iter)) {
    1271                 :            :         // check for native getiter (corresponds to __iter__)
    1272   [ +  +  +  + ]:     186837 :         if (iter_buf == NULL && MP_OBJ_TYPE_GET_SLOT(type, iter) != mp_obj_instance_getiter) {
    1273                 :            :             // if caller did not provide a buffer then allocate one on the heap
    1274                 :            :             // mp_obj_instance_getiter is special, it will allocate only if needed
    1275                 :      32195 :             iter_buf = m_new_obj(mp_obj_iter_buf_t);
    1276                 :            :         }
    1277                 :     186837 :         mp_getiter_fun_t getiter;
    1278         [ +  + ]:     186837 :         if (type->flags & MP_TYPE_FLAG_ITER_IS_CUSTOM) {
    1279                 :        113 :             getiter = ((mp_getiter_iternext_custom_t *)MP_OBJ_TYPE_GET_SLOT(type, iter))->getiter;
    1280                 :            :         } else {
    1281                 :     186724 :             getiter = (mp_getiter_fun_t)MP_OBJ_TYPE_GET_SLOT(type, iter);
    1282                 :            :         }
    1283                 :     186837 :         mp_obj_t iter = getiter(o_in, iter_buf);
    1284         [ +  + ]:     186835 :         if (iter != MP_OBJ_NULL) {
    1285                 :            :             return iter;
    1286                 :            :         }
    1287                 :            :     }
    1288                 :            : 
    1289                 :            :     // check for __getitem__
    1290                 :         48 :     mp_obj_t dest[2];
    1291                 :         48 :     mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
    1292         [ +  + ]:         48 :     if (dest[0] != MP_OBJ_NULL) {
    1293                 :            :         // __getitem__ exists, create and return an iterator
    1294         [ +  + ]:         20 :         if (iter_buf == NULL) {
    1295                 :            :             // if caller did not provide a buffer then allocate one on the heap
    1296                 :         12 :             iter_buf = m_new_obj(mp_obj_iter_buf_t);
    1297                 :            :         }
    1298                 :         20 :         return mp_obj_new_getitem_iter(dest, iter_buf);
    1299                 :            :     }
    1300                 :            : 
    1301                 :            :     // object not iterable
    1302                 :            :     #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
    1303                 :            :     mp_raise_TypeError(MP_ERROR_TEXT("object not iterable"));
    1304                 :            :     #else
    1305                 :         28 :     mp_raise_msg_varg(&mp_type_TypeError,
    1306                 :         28 :         MP_ERROR_TEXT("'%s' object isn't iterable"), mp_obj_get_type_str(o_in));
    1307                 :            :     #endif
    1308                 :            : 
    1309                 :            : }
    1310                 :            : 
    1311                 :     575575 : STATIC mp_fun_1_t type_get_iternext(const mp_obj_type_t *type) {
    1312         [ +  + ]:     575575 :     if ((type->flags & MP_TYPE_FLAG_ITER_IS_STREAM) == MP_TYPE_FLAG_ITER_IS_STREAM) {
    1313                 :            :         mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self);
    1314                 :            :         return mp_stream_unbuffered_iter;
    1315         [ +  + ]:     575527 :     } else if (type->flags & MP_TYPE_FLAG_ITER_IS_ITERNEXT) {
    1316                 :     575295 :         return (mp_fun_1_t)MP_OBJ_TYPE_GET_SLOT(type, iter);
    1317         [ +  - ]:        232 :     } else if (type->flags & MP_TYPE_FLAG_ITER_IS_CUSTOM) {
    1318                 :        232 :         return ((mp_getiter_iternext_custom_t *)MP_OBJ_TYPE_GET_SLOT(type, iter))->iternext;
    1319                 :            :     } else {
    1320                 :            :         return NULL;
    1321                 :            :     }
    1322                 :            : }
    1323                 :            : 
    1324                 :            : // may return MP_OBJ_STOP_ITERATION as an optimisation instead of raise StopIteration()
    1325                 :            : // may also raise StopIteration()
    1326                 :     113753 : mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
    1327                 :     113753 :     const mp_obj_type_t *type = mp_obj_get_type(o_in);
    1328         [ +  + ]:     113773 :     if (TYPE_HAS_ITERNEXT(type)) {
    1329                 :     113733 :         MP_STATE_THREAD(stop_iteration_arg) = MP_OBJ_NULL;
    1330                 :     113724 :         return type_get_iternext(type)(o_in);
    1331                 :            :     } else {
    1332                 :            :         // check for __next__ method
    1333                 :         40 :         mp_obj_t dest[2];
    1334                 :         40 :         mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
    1335         [ +  + ]:         40 :         if (dest[0] != MP_OBJ_NULL) {
    1336                 :            :             // __next__ exists, call it and return its result
    1337                 :         36 :             return mp_call_method_n_kw(0, 0, dest);
    1338                 :            :         } else {
    1339                 :            :             #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
    1340                 :            :             mp_raise_TypeError(MP_ERROR_TEXT("object not an iterator"));
    1341                 :            :             #else
    1342                 :          4 :             mp_raise_msg_varg(&mp_type_TypeError,
    1343                 :          4 :                 MP_ERROR_TEXT("'%s' object isn't an iterator"), mp_obj_get_type_str(o_in));
    1344                 :            :             #endif
    1345                 :            :         }
    1346                 :            :     }
    1347                 :            : }
    1348                 :            : 
    1349                 :            : // will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration() (or any subclass thereof)
    1350                 :            : // may raise other exceptions
    1351                 :     461791 : mp_obj_t mp_iternext(mp_obj_t o_in) {
    1352                 :     461791 :     MP_STACK_CHECK(); // enumerate, filter, map and zip can recursively call mp_iternext
    1353                 :     461782 :     const mp_obj_type_t *type = mp_obj_get_type(o_in);
    1354         [ +  + ]:     461780 :     if (TYPE_HAS_ITERNEXT(type)) {
    1355                 :     461652 :         MP_STATE_THREAD(stop_iteration_arg) = MP_OBJ_NULL;
    1356                 :     461644 :         return type_get_iternext(type)(o_in);
    1357                 :            :     } else {
    1358                 :            :         // check for __next__ method
    1359                 :        128 :         mp_obj_t dest[2];
    1360                 :        128 :         mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
    1361         [ +  + ]:        128 :         if (dest[0] != MP_OBJ_NULL) {
    1362                 :            :             // __next__ exists, call it and return its result
    1363                 :        124 :             nlr_buf_t nlr;
    1364         [ +  + ]:        124 :             if (nlr_push(&nlr) == 0) {
    1365                 :        124 :                 mp_obj_t ret = mp_call_method_n_kw(0, 0, dest);
    1366                 :         72 :                 nlr_pop();
    1367                 :         72 :                 return ret;
    1368                 :            :             } else {
    1369         [ +  + ]:         52 :                 if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t *)nlr.ret_val)->type), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
    1370                 :         46 :                     return mp_make_stop_iteration(mp_obj_exception_get_value(MP_OBJ_FROM_PTR(nlr.ret_val)));
    1371                 :            :                 } else {
    1372                 :          6 :                     nlr_jump(nlr.ret_val);
    1373                 :            :                 }
    1374                 :            :             }
    1375                 :            :         } else {
    1376                 :            :             #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
    1377                 :            :             mp_raise_TypeError(MP_ERROR_TEXT("object not an iterator"));
    1378                 :            :             #else
    1379                 :          4 :             mp_raise_msg_varg(&mp_type_TypeError,
    1380                 :          4 :                 MP_ERROR_TEXT("'%s' object isn't an iterator"), mp_obj_get_type_str(o_in));
    1381                 :            :             #endif
    1382                 :            :         }
    1383                 :            :     }
    1384                 :            : }
    1385                 :            : 
    1386                 :     300360 : mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
    1387         [ -  + ]:     300360 :     assert((send_value != MP_OBJ_NULL) ^ (throw_value != MP_OBJ_NULL));
    1388                 :     300360 :     const mp_obj_type_t *type = mp_obj_get_type(self_in);
    1389                 :            : 
    1390         [ +  + ]:     300360 :     if (type == &mp_type_gen_instance) {
    1391                 :       1185 :         return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val);
    1392                 :            :     }
    1393                 :            : 
    1394   [ +  +  +  + ]:     299175 :     if (TYPE_HAS_ITERNEXT(type) && send_value == mp_const_none) {
    1395                 :        248 :         MP_STATE_THREAD(stop_iteration_arg) = MP_OBJ_NULL;
    1396                 :        248 :         mp_obj_t ret = type_get_iternext(type)(self_in);
    1397                 :        158 :         *ret_val = ret;
    1398         [ +  + ]:        158 :         if (ret != MP_OBJ_STOP_ITERATION) {
    1399                 :            :             return MP_VM_RETURN_YIELD;
    1400                 :            :         } else {
    1401                 :            :             // The generator is finished.
    1402                 :            :             // This is an optimised "raise StopIteration(*ret_val)".
    1403                 :         20 :             *ret_val = MP_STATE_THREAD(stop_iteration_arg);
    1404         [ +  - ]:         20 :             if (*ret_val == MP_OBJ_NULL) {
    1405                 :         20 :                 *ret_val = mp_const_none;
    1406                 :            :             }
    1407                 :         20 :             return MP_VM_RETURN_NORMAL;
    1408                 :            :         }
    1409                 :            :     }
    1410                 :            : 
    1411                 :     298918 :     mp_obj_t dest[3]; // Reserve slot for send() arg
    1412                 :            : 
    1413                 :            :     // Python instance iterator protocol
    1414         [ +  + ]:     298918 :     if (send_value == mp_const_none) {
    1415                 :     298823 :         mp_load_method_maybe(self_in, MP_QSTR___next__, dest);
    1416         [ +  - ]:     298823 :         if (dest[0] != MP_OBJ_NULL) {
    1417                 :     298823 :             *ret_val = mp_call_method_n_kw(0, 0, dest);
    1418                 :     149482 :             return MP_VM_RETURN_YIELD;
    1419                 :            :         }
    1420                 :            :     }
    1421                 :            : 
    1422                 :            :     // Either python instance generator protocol, or native object
    1423                 :            :     // generator protocol.
    1424         [ +  + ]:        104 :     if (send_value != MP_OBJ_NULL) {
    1425                 :          8 :         mp_load_method(self_in, MP_QSTR_send, dest);
    1426                 :          8 :         dest[2] = send_value;
    1427                 :          8 :         *ret_val = mp_call_method_n_kw(1, 0, dest);
    1428                 :          8 :         return MP_VM_RETURN_YIELD;
    1429                 :            :     }
    1430                 :            : 
    1431         [ -  + ]:         96 :     assert(throw_value != MP_OBJ_NULL);
    1432                 :            :     {
    1433         [ +  + ]:         96 :         if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(throw_value)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) {
    1434                 :          8 :             mp_load_method_maybe(self_in, MP_QSTR_close, dest);
    1435         [ +  + ]:          8 :             if (dest[0] != MP_OBJ_NULL) {
    1436                 :            :                 // TODO: Exceptions raised in close() are not propagated,
    1437                 :            :                 // printed to sys.stderr
    1438                 :          2 :                 *ret_val = mp_call_method_n_kw(0, 0, dest);
    1439                 :            :                 // We assume one can't "yield" from close()
    1440                 :          2 :                 return MP_VM_RETURN_NORMAL;
    1441                 :            :             }
    1442                 :            :         } else {
    1443                 :         88 :             mp_load_method_maybe(self_in, MP_QSTR_throw, dest);
    1444         [ +  + ]:         88 :             if (dest[0] != MP_OBJ_NULL) {
    1445                 :          8 :                 dest[2] = throw_value;
    1446                 :          8 :                 *ret_val = mp_call_method_n_kw(1, 0, dest);
    1447                 :            :                 // If .throw() method returned, we assume it's value to yield
    1448                 :            :                 // - any exception would be thrown with nlr_raise().
    1449                 :          8 :                 return MP_VM_RETURN_YIELD;
    1450                 :            :             }
    1451                 :            :         }
    1452                 :            :         // If there's nowhere to throw exception into, then we assume that object
    1453                 :            :         // is just incapable to handle it, so any exception thrown into it
    1454                 :            :         // will be propagated up. This behavior is approved by test_pep380.py
    1455                 :            :         // test_delegation_of_close_to_non_generator(),
    1456                 :            :         //  test_delegating_throw_to_non_generator()
    1457         [ +  + ]:         86 :         if (mp_obj_exception_match(throw_value, MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
    1458                 :            :             // PEP479: if StopIteration is raised inside a generator it is replaced with RuntimeError
    1459                 :          4 :             *ret_val = mp_obj_new_exception_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("generator raised StopIteration"));
    1460                 :            :         } else {
    1461                 :         82 :             *ret_val = mp_make_raise_obj(throw_value);
    1462                 :            :         }
    1463                 :            :         return MP_VM_RETURN_EXCEPTION;
    1464                 :            :     }
    1465                 :            : }
    1466                 :            : 
    1467                 :     238527 : mp_obj_t mp_make_raise_obj(mp_obj_t o) {
    1468                 :     238527 :     DEBUG_printf("raise %p\n", o);
    1469         [ +  + ]:     238527 :     if (mp_obj_is_exception_type(o)) {
    1470                 :            :         // o is an exception type (it is derived from BaseException (or is BaseException))
    1471                 :            :         // create and return a new exception instance by calling o
    1472                 :            :         // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
    1473                 :            :         // could have const instances in ROM which we return here instead
    1474                 :        758 :         o = mp_call_function_n_kw(o, 0, 0, NULL);
    1475                 :            :     }
    1476                 :            : 
    1477         [ +  + ]:     238527 :     if (mp_obj_is_exception_instance(o)) {
    1478                 :            :         // o is an instance of an exception, so use it as the exception
    1479                 :            :         return o;
    1480                 :            :     } else {
    1481                 :            :         // o cannot be used as an exception, so return a type error (which will be raised by the caller)
    1482                 :         36 :         return mp_obj_new_exception_msg(&mp_type_TypeError, MP_ERROR_TEXT("exceptions must derive from BaseException"));
    1483                 :            :     }
    1484                 :            : }
    1485                 :            : 
    1486                 :       1642 : mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
    1487                 :       1642 :     DEBUG_printf("import name '%s' level=%d\n", qstr_str(name), MP_OBJ_SMALL_INT_VALUE(level));
    1488                 :            : 
    1489                 :            :     // build args array
    1490                 :       1642 :     mp_obj_t args[5];
    1491                 :       1642 :     args[0] = MP_OBJ_NEW_QSTR(name);
    1492                 :       1642 :     args[1] = mp_const_none; // TODO should be globals
    1493                 :       1642 :     args[2] = mp_const_none; // TODO should be locals
    1494                 :       1642 :     args[3] = fromlist;
    1495                 :       1642 :     args[4] = level;
    1496                 :            : 
    1497                 :            :     #if MICROPY_CAN_OVERRIDE_BUILTINS
    1498                 :            :     // Lookup __import__ and call that if it exists
    1499                 :       1642 :     mp_obj_dict_t *bo_dict = MP_STATE_VM(mp_module_builtins_override_dict);
    1500         [ +  + ]:       1642 :     if (bo_dict != NULL) {
    1501                 :         26 :         mp_map_elem_t *import = mp_map_lookup(&bo_dict->map, MP_OBJ_NEW_QSTR(MP_QSTR___import__), MP_MAP_LOOKUP);
    1502         [ +  - ]:         26 :         if (import != NULL) {
    1503                 :         26 :             return mp_call_function_n_kw(import->value, 5, 0, args);
    1504                 :            :         }
    1505                 :            :     }
    1506                 :            :     #endif
    1507                 :            : 
    1508                 :       1616 :     return mp_builtin___import__(5, args);
    1509                 :            : }
    1510                 :            : 
    1511                 :        551 : mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
    1512                 :        551 :     DEBUG_printf("import from %p %s\n", module, qstr_str(name));
    1513                 :            : 
    1514                 :        551 :     mp_obj_t dest[2];
    1515                 :            : 
    1516                 :        551 :     mp_load_method_maybe(module, name, dest);
    1517                 :            : 
    1518         [ -  + ]:        551 :     if (dest[1] != MP_OBJ_NULL) {
    1519                 :            :         // Hopefully we can't import bound method from an object
    1520                 :          0 :     import_error:
    1521                 :          6 :         mp_raise_msg_varg(&mp_type_ImportError, MP_ERROR_TEXT("can't import name %q"), name);
    1522                 :            :     }
    1523                 :            : 
    1524         [ +  + ]:        551 :     if (dest[0] != MP_OBJ_NULL) {
    1525                 :            :         return dest[0];
    1526                 :            :     }
    1527                 :            : 
    1528                 :            :     #if MICROPY_ENABLE_EXTERNAL_IMPORT
    1529                 :            : 
    1530                 :            :     // See if it's a package, then can try FS import
    1531         [ +  + ]:         16 :     if (!mp_obj_is_package(module)) {
    1532                 :          6 :         goto import_error;
    1533                 :            :     }
    1534                 :            : 
    1535                 :         10 :     mp_load_method_maybe(module, MP_QSTR___name__, dest);
    1536                 :         10 :     size_t pkg_name_len;
    1537                 :         10 :     const char *pkg_name = mp_obj_str_get_data(dest[0], &pkg_name_len);
    1538                 :            : 
    1539                 :         10 :     const uint dot_name_len = pkg_name_len + 1 + qstr_len(name);
    1540                 :         10 :     char *dot_name = mp_local_alloc(dot_name_len);
    1541                 :         10 :     memcpy(dot_name, pkg_name, pkg_name_len);
    1542                 :         10 :     dot_name[pkg_name_len] = '.';
    1543                 :         10 :     memcpy(dot_name + pkg_name_len + 1, qstr_str(name), qstr_len(name));
    1544                 :         10 :     qstr dot_name_q = qstr_from_strn(dot_name, dot_name_len);
    1545                 :         10 :     mp_local_free(dot_name);
    1546                 :            : 
    1547                 :            :     // For fromlist, pass sentinel "non empty" value to force returning of leaf module
    1548                 :         10 :     return mp_import_name(dot_name_q, mp_const_true, MP_OBJ_NEW_SMALL_INT(0));
    1549                 :            : 
    1550                 :            :     #else
    1551                 :            : 
    1552                 :            :     // Package import not supported with external imports disabled
    1553                 :            :     goto import_error;
    1554                 :            : 
    1555                 :            :     #endif
    1556                 :            : }
    1557                 :            : 
    1558                 :         78 : void mp_import_all(mp_obj_t module) {
    1559                 :         78 :     DEBUG_printf("import all %p\n", module);
    1560                 :            : 
    1561                 :            :     // TODO: Support __all__
    1562                 :         78 :     mp_map_t *map = &mp_obj_module_get_globals(module)->map;
    1563         [ +  + ]:       2118 :     for (size_t i = 0; i < map->alloc; i++) {
    1564         [ +  + ]:       2040 :         if (mp_map_slot_is_filled(map, i)) {
    1565                 :            :             // Entry in module global scope may be generated programmatically
    1566                 :            :             // (and thus be not a qstr for longer names). Avoid turning it in
    1567                 :            :             // qstr if it has '_' and was used exactly to save memory.
    1568                 :       1986 :             const char *name = mp_obj_str_get_str(map->table[i].key);
    1569         [ +  + ]:       1986 :             if (*name != '_') {
    1570                 :       1534 :                 qstr qname = mp_obj_str_get_qstr(map->table[i].key);
    1571                 :       1534 :                 mp_store_name(qname, map->table[i].value);
    1572                 :            :             }
    1573                 :            :         }
    1574                 :            :     }
    1575                 :         78 : }
    1576                 :            : 
    1577                 :            : #if MICROPY_ENABLE_COMPILER
    1578                 :            : 
    1579                 :       1608 : mp_obj_t mp_parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t parse_input_kind, mp_obj_dict_t *globals, mp_obj_dict_t *locals) {
    1580                 :            :     // save context
    1581                 :       1608 :     mp_obj_dict_t *volatile old_globals = mp_globals_get();
    1582                 :       1608 :     mp_obj_dict_t *volatile old_locals = mp_locals_get();
    1583                 :            : 
    1584                 :            :     // set new context
    1585                 :       1608 :     mp_globals_set(globals);
    1586                 :       1608 :     mp_locals_set(locals);
    1587                 :            : 
    1588                 :       1608 :     nlr_buf_t nlr;
    1589         [ +  + ]:       1608 :     if (nlr_push(&nlr) == 0) {
    1590                 :       1608 :         qstr source_name = lex->source_name;
    1591                 :       1608 :         mp_parse_tree_t parse_tree = mp_parse(lex, parse_input_kind);
    1592                 :       1466 :         mp_obj_t module_fun = mp_compile(&parse_tree, source_name, parse_input_kind == MP_PARSE_SINGLE_INPUT);
    1593                 :            : 
    1594                 :       1128 :         mp_obj_t ret;
    1595         [ +  + ]:       1128 :         if (MICROPY_PY_BUILTINS_COMPILE && globals == NULL) {
    1596                 :            :             // for compile only, return value is the module function
    1597                 :            :             ret = module_fun;
    1598                 :            :         } else {
    1599                 :            :             // execute module function and get return value
    1600                 :       1100 :             ret = mp_call_function_0(module_fun);
    1601                 :            :         }
    1602                 :            : 
    1603                 :            :         // finish nlr block, restore context and return value
    1604                 :       1061 :         nlr_pop();
    1605                 :       1061 :         mp_globals_set(old_globals);
    1606                 :       1061 :         mp_locals_set(old_locals);
    1607                 :       1061 :         return ret;
    1608                 :            :     } else {
    1609                 :            :         // exception; restore context and re-raise same exception
    1610                 :        546 :         mp_globals_set(old_globals);
    1611                 :        546 :         mp_locals_set(old_locals);
    1612                 :        546 :         nlr_jump(nlr.ret_val);
    1613                 :            :     }
    1614                 :            : }
    1615                 :            : 
    1616                 :            : #endif // MICROPY_ENABLE_COMPILER
    1617                 :            : 
    1618                 :        110 : NORETURN void m_malloc_fail(size_t num_bytes) {
    1619                 :        110 :     DEBUG_printf("memory allocation failed, allocating %u bytes\n", (uint)num_bytes);
    1620                 :            :     #if MICROPY_ENABLE_GC
    1621         [ +  + ]:        110 :     if (gc_is_locked()) {
    1622                 :         94 :         mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("memory allocation failed, heap is locked"));
    1623                 :            :     }
    1624                 :            :     #endif
    1625                 :         16 :     mp_raise_msg_varg(&mp_type_MemoryError,
    1626                 :         16 :         MP_ERROR_TEXT("memory allocation failed, allocating %u bytes"), (uint)num_bytes);
    1627                 :            : }
    1628                 :            : 
    1629                 :            : #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
    1630                 :            : 
    1631                 :            : NORETURN void mp_raise_type(const mp_obj_type_t *exc_type) {
    1632                 :            :     nlr_raise(mp_obj_new_exception(exc_type));
    1633                 :            : }
    1634                 :            : 
    1635                 :            : NORETURN void mp_raise_ValueError_no_msg(void) {
    1636                 :            :     mp_raise_type(&mp_type_ValueError);
    1637                 :            : }
    1638                 :            : 
    1639                 :            : NORETURN void mp_raise_TypeError_no_msg(void) {
    1640                 :            :     mp_raise_type(&mp_type_TypeError);
    1641                 :            : }
    1642                 :            : 
    1643                 :            : NORETURN void mp_raise_NotImplementedError_no_msg(void) {
    1644                 :            :     mp_raise_type(&mp_type_NotImplementedError);
    1645                 :            : }
    1646                 :            : 
    1647                 :            : #else
    1648                 :            : 
    1649                 :       2421 : NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg) {
    1650         [ +  + ]:       2421 :     if (msg == NULL) {
    1651                 :        876 :         nlr_raise(mp_obj_new_exception(exc_type));
    1652                 :            :     } else {
    1653                 :       1545 :         nlr_raise(mp_obj_new_exception_msg(exc_type, msg));
    1654                 :            :     }
    1655                 :            : }
    1656                 :            : 
    1657                 :       1132 : NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, ...) {
    1658                 :       1132 :     va_list args;
    1659                 :       1132 :     va_start(args, fmt);
    1660                 :       1132 :     mp_obj_t exc = mp_obj_new_exception_msg_vlist(exc_type, fmt, args);
    1661                 :       1132 :     va_end(args);
    1662                 :       1132 :     nlr_raise(exc);
    1663                 :            : }
    1664                 :            : 
    1665                 :        812 : NORETURN void mp_raise_ValueError(mp_rom_error_text_t msg) {
    1666                 :        812 :     mp_raise_msg(&mp_type_ValueError, msg);
    1667                 :            : }
    1668                 :            : 
    1669                 :        257 : NORETURN void mp_raise_TypeError(mp_rom_error_text_t msg) {
    1670                 :        257 :     mp_raise_msg(&mp_type_TypeError, msg);
    1671                 :            : }
    1672                 :            : 
    1673                 :        414 : NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg) {
    1674                 :        414 :     mp_raise_msg(&mp_type_NotImplementedError, msg);
    1675                 :            : }
    1676                 :            : 
    1677                 :            : #endif
    1678                 :            : 
    1679                 :        480 : NORETURN void mp_raise_type_arg(const mp_obj_type_t *exc_type, mp_obj_t arg) {
    1680                 :        480 :     nlr_raise(mp_obj_new_exception_arg1(exc_type, arg));
    1681                 :            : }
    1682                 :            : 
    1683                 :        454 : NORETURN void mp_raise_StopIteration(mp_obj_t arg) {
    1684         [ +  + ]:        454 :     if (arg == MP_OBJ_NULL) {
    1685                 :        373 :         mp_raise_type(&mp_type_StopIteration);
    1686                 :            :     } else {
    1687                 :         81 :         mp_raise_type_arg(&mp_type_StopIteration, arg);
    1688                 :            :     }
    1689                 :            : }
    1690                 :            : 
    1691                 :        230 : NORETURN void mp_raise_OSError(int errno_) {
    1692                 :        230 :     mp_raise_type_arg(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_));
    1693                 :            : }
    1694                 :            : 
    1695                 :          0 : NORETURN void mp_raise_OSError_with_filename(int errno_, const char *filename) {
    1696                 :          0 :     vstr_t vstr;
    1697                 :          0 :     vstr_init(&vstr, 32);
    1698                 :          0 :     vstr_printf(&vstr, "can't open %s", filename);
    1699                 :          0 :     mp_obj_t o_str = mp_obj_new_str_from_vstr(&vstr);
    1700                 :          0 :     mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(errno_), MP_OBJ_FROM_PTR(o_str)};
    1701                 :          0 :     nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args));
    1702                 :            : }
    1703                 :            : 
    1704                 :            : #if MICROPY_STACK_CHECK || MICROPY_ENABLE_PYSTACK
    1705                 :         23 : NORETURN void mp_raise_recursion_depth(void) {
    1706                 :         23 :     mp_raise_type_arg(&mp_type_RuntimeError, MP_OBJ_NEW_QSTR(MP_QSTR_maximum_space_recursion_space_depth_space_exceeded));
    1707                 :            : }
    1708                 :            : #endif

Generated by: LCOV version 1.15-5-g462f71d