LCOV - code coverage report
Current view: top level - py - objgenerator.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-343-g7b050b366.info Lines: 109 109 100.0 %
Date: 2024-04-25 17:00:48 Functions: 10 10 100.0 %
Branches: 43 43 100.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython project, http://micropython.org/
       3                 :            :  *
       4                 :            :  * The MIT License (MIT)
       5                 :            :  *
       6                 :            :  * Copyright (c) 2013-2019 Damien P. George
       7                 :            :  * Copyright (c) 2014-2017 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 <stdlib.h>
      29                 :            : #include <assert.h>
      30                 :            : 
      31                 :            : #include "py/runtime.h"
      32                 :            : #include "py/bc.h"
      33                 :            : #include "py/objstr.h"
      34                 :            : #include "py/objgenerator.h"
      35                 :            : #include "py/objfun.h"
      36                 :            : #include "py/stackctrl.h"
      37                 :            : 
      38                 :            : // Instance of GeneratorExit exception - needed by generator.close()
      39                 :            : const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit}, 0, 0, NULL, (mp_obj_tuple_t *)&mp_const_empty_tuple_obj};
      40                 :            : 
      41                 :            : /******************************************************************************/
      42                 :            : /* generator wrapper                                                          */
      43                 :            : 
      44                 :            : typedef struct _mp_obj_gen_instance_t {
      45                 :            :     mp_obj_base_t base;
      46                 :            :     // mp_const_none: Not-running, no exception.
      47                 :            :     // MP_OBJ_NULL: Running, no exception.
      48                 :            :     // other: Not running, pending exception.
      49                 :            :     mp_obj_t pend_exc;
      50                 :            :     mp_code_state_t code_state;
      51                 :            : } mp_obj_gen_instance_t;
      52                 :            : 
      53                 :       1364 : static mp_obj_t gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
      54                 :            :     // A generating function is just a bytecode function with type mp_type_gen_wrap
      55                 :       1364 :     mp_obj_fun_bc_t *self_fun = MP_OBJ_TO_PTR(self_in);
      56                 :            : 
      57                 :            :     // bytecode prelude: get state size and exception stack size
      58                 :       1364 :     const uint8_t *ip = self_fun->bytecode;
      59         [ +  + ]:       2792 :     MP_BC_PRELUDE_SIG_DECODE(ip);
      60                 :            : 
      61                 :            :     // allocate the generator object, with room for local stack and exception stack
      62                 :       1364 :     mp_obj_gen_instance_t *o = mp_obj_malloc_var(mp_obj_gen_instance_t, code_state.state, byte,
      63                 :            :         n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t),
      64                 :            :         &mp_type_gen_instance);
      65                 :            : 
      66                 :       1364 :     o->pend_exc = mp_const_none;
      67                 :       1364 :     o->code_state.fun_bc = self_fun;
      68                 :       1364 :     o->code_state.n_state = n_state;
      69                 :       1364 :     mp_setup_code_state(&o->code_state, n_args, n_kw, args);
      70                 :       1364 :     return MP_OBJ_FROM_PTR(o);
      71                 :            : }
      72                 :            : 
      73                 :            : #if MICROPY_PY_FUNCTION_ATTRS
      74                 :            : #define GEN_WRAP_TYPE_ATTR attr, mp_obj_fun_bc_attr,
      75                 :            : #else
      76                 :            : #define GEN_WRAP_TYPE_ATTR
      77                 :            : #endif
      78                 :            : 
      79                 :            : MP_DEFINE_CONST_OBJ_TYPE(
      80                 :            :     mp_type_gen_wrap,
      81                 :            :     MP_QSTR_generator,
      82                 :            :     MP_TYPE_FLAG_BINDS_SELF,
      83                 :            :     GEN_WRAP_TYPE_ATTR
      84                 :            :     call, gen_wrap_call
      85                 :            :     );
      86                 :            : 
      87                 :            : /******************************************************************************/
      88                 :            : // native generator wrapper
      89                 :            : 
      90                 :            : #if MICROPY_EMIT_NATIVE
      91                 :            : 
      92                 :            : // Based on mp_obj_gen_instance_t.
      93                 :            : typedef struct _mp_obj_gen_instance_native_t {
      94                 :            :     mp_obj_base_t base;
      95                 :            :     mp_obj_t pend_exc;
      96                 :            :     mp_code_state_native_t code_state;
      97                 :            : } mp_obj_gen_instance_native_t;
      98                 :            : 
      99                 :       1146 : static mp_obj_t native_gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
     100                 :            :     // The state for a native generating function is held in the same struct as a bytecode function
     101                 :       1146 :     mp_obj_fun_bc_t *self_fun = MP_OBJ_TO_PTR(self_in);
     102                 :            : 
     103                 :            :     // Determine start of prelude.
     104         [ +  + ]:       1146 :     const uint8_t *prelude_ptr = mp_obj_fun_native_get_prelude_ptr(self_fun);
     105                 :            : 
     106                 :            :     // Extract n_state from the prelude.
     107                 :       1146 :     const uint8_t *ip = prelude_ptr;
     108         [ +  + ]:       2356 :     MP_BC_PRELUDE_SIG_DECODE(ip);
     109                 :            : 
     110                 :            :     // Allocate the generator object, with room for local stack (exception stack not needed).
     111                 :       1146 :     mp_obj_gen_instance_native_t *o = mp_obj_malloc_var(mp_obj_gen_instance_native_t, code_state.state, byte, n_state * sizeof(mp_obj_t), &mp_type_gen_instance);
     112                 :            : 
     113                 :            :     // Parse the input arguments and set up the code state
     114                 :       1146 :     o->pend_exc = mp_const_none;
     115                 :       1146 :     o->code_state.fun_bc = self_fun;
     116                 :       1146 :     o->code_state.n_state = n_state;
     117                 :       1146 :     mp_setup_code_state_native(&o->code_state, n_args, n_kw, args);
     118                 :            : 
     119                 :            :     // Indicate we are a native function, which doesn't use this variable
     120                 :       1146 :     o->code_state.exc_sp_idx = MP_CODE_STATE_EXC_SP_IDX_SENTINEL;
     121                 :            : 
     122                 :            :     // Prepare the generator instance for execution
     123                 :       1146 :     o->code_state.ip = mp_obj_fun_native_get_generator_start(self_fun);
     124                 :            : 
     125                 :       1146 :     return MP_OBJ_FROM_PTR(o);
     126                 :            : }
     127                 :            : 
     128                 :            : #if MICROPY_PY_FUNCTION_ATTRS
     129                 :            : #define NATIVE_GEN_WRAP_TYPE_ATTR , attr, mp_obj_fun_bc_attr
     130                 :            : #else
     131                 :            : #define NATIVE_GEN_WRAP_TYPE_ATTR
     132                 :            : #endif
     133                 :            : 
     134                 :            : MP_DEFINE_CONST_OBJ_TYPE(
     135                 :            :     mp_type_native_gen_wrap,
     136                 :            :     MP_QSTR_generator,
     137                 :            :     MP_TYPE_FLAG_BINDS_SELF,
     138                 :            :     call, native_gen_wrap_call
     139                 :            :     NATIVE_GEN_WRAP_TYPE_ATTR
     140                 :            :     );
     141                 :            : 
     142                 :            : #endif // MICROPY_EMIT_NATIVE
     143                 :            : 
     144                 :            : /******************************************************************************/
     145                 :            : /* generator instance                                                         */
     146                 :            : 
     147                 :          4 : static void gen_instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
     148                 :          4 :     (void)kind;
     149                 :          4 :     mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
     150                 :          4 :     mp_printf(print, "<generator object '%q' at %p>", mp_obj_fun_get_name(MP_OBJ_FROM_PTR(self->code_state.fun_bc)), self);
     151                 :          4 : }
     152                 :            : 
     153                 :     174218 : mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
     154                 :     174218 :     MP_STACK_CHECK();
     155                 :     174207 :     mp_check_self(mp_obj_is_type(self_in, &mp_type_gen_instance));
     156                 :     174207 :     mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
     157         [ +  + ]:     174207 :     if (self->code_state.ip == 0) {
     158                 :            :         // Trying to resume an already stopped generator.
     159                 :            :         // This is an optimised "raise StopIteration(None)".
     160                 :         83 :         *ret_val = mp_const_none;
     161                 :         83 :         return MP_VM_RETURN_NORMAL;
     162                 :            :     }
     163                 :            : 
     164                 :            :     // Ensure the generator cannot be reentered during execution
     165         [ +  + ]:     174124 :     if (self->pend_exc == MP_OBJ_NULL) {
     166                 :          8 :         mp_raise_ValueError(MP_ERROR_TEXT("generator already executing"));
     167                 :            :     }
     168                 :            : 
     169                 :            :     #if MICROPY_PY_GENERATOR_PEND_THROW
     170                 :            :     // If exception is pending (set using .pend_throw()), process it now.
     171         [ +  + ]:     174116 :     if (self->pend_exc != mp_const_none) {
     172                 :         16 :         throw_value = self->pend_exc;
     173                 :            :     }
     174                 :            :     #endif
     175                 :            : 
     176                 :            :     // If the generator is started, allow sending a value.
     177                 :     174116 :     void *state_start = self->code_state.state - 1;
     178                 :            :     #if MICROPY_EMIT_NATIVE
     179                 :     174116 :     if (self->code_state.exc_sp_idx == MP_CODE_STATE_EXC_SP_IDX_SENTINEL) {
     180                 :     174116 :         state_start = ((mp_obj_gen_instance_native_t *)self)->code_state.state - 1;
     181                 :            :     }
     182                 :            :     #endif
     183         [ +  + ]:     174116 :     if (self->code_state.sp == state_start) {
     184         [ +  + ]:       2484 :         if (send_value != mp_const_none) {
     185                 :          4 :             mp_raise_TypeError(MP_ERROR_TEXT("can't send non-None value to a just-started generator"));
     186                 :            :         }
     187                 :            :     } else {
     188                 :     171632 :         *self->code_state.sp = send_value;
     189                 :            :     }
     190                 :            : 
     191                 :            :     // Mark as running
     192                 :     174112 :     self->pend_exc = MP_OBJ_NULL;
     193                 :            : 
     194                 :            :     // Set up the correct globals context for the generator and execute it
     195                 :     174112 :     self->code_state.old_globals = mp_globals_get();
     196                 :     174113 :     mp_globals_set(self->code_state.fun_bc->context->module.globals);
     197                 :            : 
     198                 :     174109 :     mp_vm_return_kind_t ret_kind;
     199                 :            : 
     200                 :            :     #if MICROPY_EMIT_NATIVE
     201         [ +  + ]:     174109 :     if (self->code_state.exc_sp_idx == MP_CODE_STATE_EXC_SP_IDX_SENTINEL) {
     202                 :            :         // A native generator.
     203                 :      99719 :         typedef uintptr_t (*mp_fun_native_gen_t)(void *, mp_obj_t);
     204                 :      99719 :         mp_fun_native_gen_t fun = mp_obj_fun_native_get_generator_resume(self->code_state.fun_bc);
     205                 :      99719 :         ret_kind = fun((void *)&self->code_state, throw_value);
     206                 :            :     } else
     207                 :            :     #endif
     208                 :            :     {
     209                 :            :         // A bytecode generator
     210                 :      74390 :         ret_kind = mp_execute_bytecode(&self->code_state, throw_value);
     211                 :            :     }
     212                 :            : 
     213                 :     174084 :     mp_globals_set(self->code_state.old_globals);
     214                 :            : 
     215                 :            :     // Mark as not running
     216                 :     174085 :     self->pend_exc = mp_const_none;
     217                 :            : 
     218      [ +  +  + ]:     174085 :     switch (ret_kind) {
     219                 :       1224 :         case MP_VM_RETURN_NORMAL:
     220                 :            :         default:
     221                 :            :             // Explicitly mark generator as completed. If we don't do this,
     222                 :            :             // subsequent next() may re-execute statements after last yield
     223                 :            :             // again and again, leading to side effects.
     224                 :       1224 :             self->code_state.ip = 0;
     225                 :            :             // This is an optimised "raise StopIteration(*ret_val)".
     226                 :       1224 :             *ret_val = *self->code_state.sp;
     227                 :       1224 :             break;
     228                 :            : 
     229                 :     171744 :         case MP_VM_RETURN_YIELD:
     230                 :     171744 :             *ret_val = *self->code_state.sp;
     231                 :            :             #if MICROPY_PY_GENERATOR_PEND_THROW
     232                 :     171744 :             *self->code_state.sp = mp_const_none;
     233                 :            :             #endif
     234                 :     171744 :             break;
     235                 :            : 
     236                 :       1117 :         case MP_VM_RETURN_EXCEPTION: {
     237                 :       1117 :             self->code_state.ip = 0;
     238                 :            :             #if MICROPY_EMIT_NATIVE
     239         [ +  + ]:       1117 :             if (self->code_state.exc_sp_idx == MP_CODE_STATE_EXC_SP_IDX_SENTINEL) {
     240                 :        539 :                 *ret_val = ((mp_obj_gen_instance_native_t *)self)->code_state.state[0];
     241                 :            :             } else
     242                 :            :             #endif
     243                 :            :             {
     244                 :        578 :                 *ret_val = self->code_state.state[0];
     245                 :            :             }
     246                 :            :             // PEP479: if StopIteration is raised inside a generator it is replaced with RuntimeError
     247         [ +  + ]:       1117 :             if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(*ret_val)), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
     248                 :          8 :                 *ret_val = mp_obj_new_exception_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("generator raised StopIteration"));
     249                 :            :             }
     250                 :            :             break;
     251                 :            :         }
     252                 :            :     }
     253                 :            : 
     254                 :            :     return ret_kind;
     255                 :            : }
     256                 :            : 
     257                 :     172859 : static mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, bool raise_stop_iteration) {
     258                 :     172859 :     mp_obj_t ret;
     259      [ +  +  + ]:     172859 :     switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
     260                 :        851 :         case MP_VM_RETURN_NORMAL:
     261                 :            :         default:
     262                 :            :             // A normal return is a StopIteration, either raise it or return
     263                 :            :             // MP_OBJ_STOP_ITERATION as an optimisation.
     264         [ +  + ]:        851 :             if (ret == mp_const_none) {
     265                 :        782 :                 ret = MP_OBJ_NULL;
     266                 :            :             }
     267         [ +  + ]:        851 :             if (raise_stop_iteration) {
     268                 :        364 :                 mp_raise_StopIteration(ret);
     269                 :            :             } else {
     270                 :        487 :                 return mp_make_stop_iteration(ret);
     271                 :            :             }
     272                 :            : 
     273                 :     171373 :         case MP_VM_RETURN_YIELD:
     274                 :     171373 :             return ret;
     275                 :            : 
     276                 :        591 :         case MP_VM_RETURN_EXCEPTION:
     277                 :        591 :             nlr_raise(ret);
     278                 :            :     }
     279                 :            : }
     280                 :            : 
     281                 :      29869 : static mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
     282                 :      29869 :     return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL, false);
     283                 :            : }
     284                 :            : 
     285                 :     142752 : static mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
     286                 :     142752 :     return gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL, true);
     287                 :            : }
     288                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
     289                 :            : 
     290                 :        238 : static mp_obj_t gen_instance_throw(size_t n_args, const mp_obj_t *args) {
     291                 :            :     // The signature of this function is: throw(type[, value[, traceback]])
     292                 :            :     // CPython will pass all given arguments through the call chain and process them
     293                 :            :     // at the point they are used (native generators will handle them differently to
     294                 :            :     // user-defined generators with a throw() method).  To save passing multiple
     295                 :            :     // values, MicroPython instead does partial processing here to reduce it down to
     296                 :            :     // one argument and passes that through:
     297                 :            :     // - if only args[1] is given, or args[2] is given but is None, args[1] is
     298                 :            :     //   passed through (in the standard case it is an exception class or instance)
     299                 :            :     // - if args[2] is given and not None it is passed through (in the standard
     300                 :            :     //   case it would be an exception instance and args[1] its corresponding class)
     301                 :            :     // - args[3] is always ignored
     302                 :            : 
     303                 :        238 :     mp_obj_t exc = args[1];
     304   [ +  +  +  + ]:        238 :     if (n_args > 2 && args[2] != mp_const_none) {
     305                 :          8 :         exc = args[2];
     306                 :            :     }
     307                 :            : 
     308                 :        238 :     return gen_resume_and_raise(args[0], mp_const_none, exc, true);
     309                 :            : }
     310                 :            : static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);
     311                 :            : 
     312                 :         40 : static mp_obj_t gen_instance_close(mp_obj_t self_in) {
     313                 :         40 :     mp_obj_t ret;
     314      [ +  +  + ]:         40 :     switch (mp_obj_gen_resume(self_in, mp_const_none, MP_OBJ_FROM_PTR(&mp_const_GeneratorExit_obj), &ret)) {
     315                 :            :         case MP_VM_RETURN_YIELD:
     316                 :          2 :             mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("generator ignored GeneratorExit"));
     317                 :            : 
     318                 :            :         // Swallow GeneratorExit (== successful close), and re-raise any other
     319                 :         34 :         case MP_VM_RETURN_EXCEPTION:
     320                 :            :             // ret should always be an instance of an exception class
     321         [ +  + ]:         34 :             if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) {
     322                 :            :                 return mp_const_none;
     323                 :            :             }
     324                 :          4 :             nlr_raise(ret);
     325                 :            : 
     326                 :            :         default:
     327                 :            :             // The only choice left is MP_VM_RETURN_NORMAL which is successful close
     328                 :            :             return mp_const_none;
     329                 :            :     }
     330                 :            : }
     331                 :            : static MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
     332                 :            : 
     333                 :            : #if MICROPY_PY_GENERATOR_PEND_THROW
     334                 :         36 : static mp_obj_t gen_instance_pend_throw(mp_obj_t self_in, mp_obj_t exc_in) {
     335                 :         36 :     mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
     336         [ +  + ]:         36 :     if (self->pend_exc == MP_OBJ_NULL) {
     337                 :          4 :         mp_raise_ValueError(MP_ERROR_TEXT("generator already executing"));
     338                 :            :     }
     339                 :         32 :     mp_obj_t prev = self->pend_exc;
     340                 :         32 :     self->pend_exc = exc_in;
     341                 :         32 :     return prev;
     342                 :            : }
     343                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_pend_throw_obj, gen_instance_pend_throw);
     344                 :            : #endif
     345                 :            : 
     346                 :            : static const mp_rom_map_elem_t gen_instance_locals_dict_table[] = {
     347                 :            :     { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&gen_instance_close_obj) },
     348                 :            :     { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&gen_instance_send_obj) },
     349                 :            :     { MP_ROM_QSTR(MP_QSTR_throw), MP_ROM_PTR(&gen_instance_throw_obj) },
     350                 :            :     #if MICROPY_PY_GENERATOR_PEND_THROW
     351                 :            :     { MP_ROM_QSTR(MP_QSTR_pend_throw), MP_ROM_PTR(&gen_instance_pend_throw_obj) },
     352                 :            :     #endif
     353                 :            : };
     354                 :            : 
     355                 :            : static MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table);
     356                 :            : 
     357                 :            : MP_DEFINE_CONST_OBJ_TYPE(
     358                 :            :     mp_type_gen_instance,
     359                 :            :     MP_QSTR_generator,
     360                 :            :     MP_TYPE_FLAG_ITER_IS_ITERNEXT,
     361                 :            :     print, gen_instance_print,
     362                 :            :     iter, gen_instance_iternext,
     363                 :            :     locals_dict, &gen_instance_locals_dict
     364                 :            :     );

Generated by: LCOV version 1.15-5-g462f71d