LCOV - code coverage report
Current view: top level - py - builtinevex.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.19.1-994-ga4672149b.info Lines: 63 63 100.0 %
Date: 2023-03-29 17:08:22 Functions: 6 6 100.0 %
Branches: 32 42 76.2 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython project, http://micropython.org/
       3                 :            :  *
       4                 :            :  * The MIT License (MIT)
       5                 :            :  *
       6                 :            :  * Copyright (c) 2013, 2014 Damien P. George
       7                 :            :  *
       8                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       9                 :            :  * of this software and associated documentation files (the "Software"), to deal
      10                 :            :  * in the Software without restriction, including without limitation the rights
      11                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      12                 :            :  * copies of the Software, and to permit persons to whom the Software is
      13                 :            :  * furnished to do so, subject to the following conditions:
      14                 :            :  *
      15                 :            :  * The above copyright notice and this permission notice shall be included in
      16                 :            :  * all copies or substantial portions of the Software.
      17                 :            :  *
      18                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      19                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      20                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      21                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      22                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      23                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      24                 :            :  * THE SOFTWARE.
      25                 :            :  */
      26                 :            : 
      27                 :            : #include <stdint.h>
      28                 :            : 
      29                 :            : #include "py/objfun.h"
      30                 :            : #include "py/compile.h"
      31                 :            : #include "py/runtime.h"
      32                 :            : #include "py/builtin.h"
      33                 :            : 
      34                 :            : #if MICROPY_PY_BUILTINS_COMPILE
      35                 :            : 
      36                 :            : typedef struct _mp_obj_code_t {
      37                 :            :     mp_obj_base_t base;
      38                 :            :     mp_obj_t module_fun;
      39                 :            : } mp_obj_code_t;
      40                 :            : 
      41                 :            : STATIC MP_DEFINE_CONST_OBJ_TYPE(
      42                 :            :     mp_type_code,
      43                 :            :     MP_QSTR_code,
      44                 :            :     MP_TYPE_FLAG_NONE
      45                 :            :     );
      46                 :            : 
      47                 :         32 : STATIC mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_dict_t *globals, mp_obj_dict_t *locals) {
      48                 :            :     // save context and set new context
      49                 :         32 :     mp_obj_dict_t *old_globals = mp_globals_get();
      50                 :         32 :     mp_obj_dict_t *old_locals = mp_locals_get();
      51                 :         32 :     mp_globals_set(globals);
      52                 :         32 :     mp_locals_set(locals);
      53                 :            : 
      54                 :            :     // a bit of a hack: fun_bc will re-set globals, so need to make sure it's
      55                 :            :     // the correct one
      56   [ -  +  -  +  :         32 :     if (mp_obj_is_type(self->module_fun, &mp_type_fun_bc)) {
          -  +  -  +  +  
                -  +  + ]
      57                 :         16 :         mp_obj_fun_bc_t *fun_bc = MP_OBJ_TO_PTR(self->module_fun);
      58                 :         16 :         ((mp_module_context_t *)fun_bc->context)->module.globals = globals;
      59                 :            :     }
      60                 :            : 
      61                 :            :     // execute code
      62                 :         32 :     nlr_buf_t nlr;
      63         [ +  + ]:         32 :     if (nlr_push(&nlr) == 0) {
      64                 :         32 :         mp_obj_t ret = mp_call_function_0(self->module_fun);
      65                 :         24 :         nlr_pop();
      66                 :         24 :         mp_globals_set(old_globals);
      67                 :         24 :         mp_locals_set(old_locals);
      68                 :         24 :         return ret;
      69                 :            :     } else {
      70                 :            :         // exception; restore context and re-raise same exception
      71                 :          8 :         mp_globals_set(old_globals);
      72                 :          8 :         mp_locals_set(old_locals);
      73                 :          8 :         nlr_jump(nlr.ret_val);
      74                 :            :     }
      75                 :            : }
      76                 :            : 
      77                 :         44 : STATIC mp_obj_t mp_builtin_compile(size_t n_args, const mp_obj_t *args) {
      78                 :         44 :     (void)n_args;
      79                 :            : 
      80                 :            :     // get the source
      81                 :         44 :     size_t str_len;
      82                 :         44 :     const char *str = mp_obj_str_get_data(args[0], &str_len);
      83                 :            : 
      84                 :            :     // get the filename
      85                 :         44 :     qstr filename = mp_obj_str_get_qstr(args[1]);
      86                 :            : 
      87                 :            :     // create the lexer
      88                 :         44 :     mp_lexer_t *lex = mp_lexer_new_from_str_len(filename, str, str_len, 0);
      89                 :            : 
      90                 :            :     // get the compile mode
      91                 :         44 :     qstr mode = mp_obj_str_get_qstr(args[2]);
      92                 :         44 :     mp_parse_input_kind_t parse_input_kind;
      93   [ +  +  +  + ]:         44 :     switch (mode) {
      94                 :            :         case MP_QSTR_single:
      95                 :            :             parse_input_kind = MP_PARSE_SINGLE_INPUT;
      96                 :            :             break;
      97                 :         16 :         case MP_QSTR_exec:
      98                 :         16 :             parse_input_kind = MP_PARSE_FILE_INPUT;
      99                 :         16 :             break;
     100                 :         12 :         case MP_QSTR_eval:
     101                 :         12 :             parse_input_kind = MP_PARSE_EVAL_INPUT;
     102                 :         12 :             break;
     103                 :            :         default:
     104                 :          4 :             mp_raise_ValueError(MP_ERROR_TEXT("bad compile mode"));
     105                 :            :     }
     106                 :            : 
     107                 :         40 :     mp_obj_code_t *code = mp_obj_malloc(mp_obj_code_t, &mp_type_code);
     108                 :         40 :     code->module_fun = mp_parse_compile_execute(lex, parse_input_kind, NULL, NULL);
     109                 :         28 :     return MP_OBJ_FROM_PTR(code);
     110                 :            : }
     111                 :            : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_compile_obj, 3, 6, mp_builtin_compile);
     112                 :            : 
     113                 :            : #endif // MICROPY_PY_BUILTINS_COMPILE
     114                 :            : 
     115                 :            : #if MICROPY_PY_BUILTINS_EVAL_EXEC
     116                 :            : 
     117                 :       1401 : STATIC mp_obj_t eval_exec_helper(size_t n_args, const mp_obj_t *args, mp_parse_input_kind_t parse_input_kind) {
     118                 :            :     // work out the context
     119                 :       1401 :     mp_obj_dict_t *globals = mp_globals_get();
     120                 :       1401 :     mp_obj_dict_t *locals = mp_locals_get();
     121         [ +  + ]:       1480 :     for (size_t i = 1; i < 3 && i < n_args; ++i) {
     122         [ +  + ]:         87 :         if (args[i] != mp_const_none) {
     123   [ -  +  -  +  :         63 :             if (!mp_obj_is_type(args[i], &mp_type_dict)) {
          -  +  -  +  +  
                +  -  + ]
     124                 :          8 :                 mp_raise_TypeError(NULL);
     125                 :            :             }
     126                 :         55 :             locals = MP_OBJ_TO_PTR(args[i]);
     127         [ +  + ]:         55 :             if (i == 1) {
     128                 :         39 :                 globals = locals;
     129                 :            :             }
     130                 :            :         }
     131                 :            :     }
     132                 :            : 
     133                 :            :     #if MICROPY_PY_BUILTINS_COMPILE
     134   [ +  +  +  + ]:       1393 :     if (mp_obj_is_type(args[0], &mp_type_code)) {
     135                 :         32 :         return code_execute(MP_OBJ_TO_PTR(args[0]), globals, locals);
     136                 :            :     }
     137                 :            :     #endif
     138                 :            : 
     139                 :            :     // Extract the source code.
     140                 :       1361 :     mp_buffer_info_t bufinfo;
     141                 :       1361 :     mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
     142                 :            : 
     143                 :            :     // create the lexer
     144                 :            :     // MP_PARSE_SINGLE_INPUT is used to indicate a file input
     145                 :       1361 :     mp_lexer_t *lex;
     146         [ +  + ]:       1361 :     if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) {
     147                 :          8 :         lex = mp_lexer_new_from_file(bufinfo.buf);
     148                 :          8 :         parse_input_kind = MP_PARSE_FILE_INPUT;
     149                 :            :     } else {
     150                 :       1353 :         lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, bufinfo.buf, bufinfo.len, 0);
     151                 :            :     }
     152                 :            : 
     153                 :       1355 :     return mp_parse_compile_execute(lex, parse_input_kind, globals, locals);
     154                 :            : }
     155                 :            : 
     156                 :        366 : STATIC mp_obj_t mp_builtin_eval(size_t n_args, const mp_obj_t *args) {
     157                 :        366 :     return eval_exec_helper(n_args, args, MP_PARSE_EVAL_INPUT);
     158                 :            : }
     159                 :            : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_eval_obj, 1, 3, mp_builtin_eval);
     160                 :            : 
     161                 :       1027 : STATIC mp_obj_t mp_builtin_exec(size_t n_args, const mp_obj_t *args) {
     162                 :       1027 :     return eval_exec_helper(n_args, args, MP_PARSE_FILE_INPUT);
     163                 :            : }
     164                 :            : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
     165                 :            : 
     166                 :            : #endif // MICROPY_PY_BUILTINS_EVAL_EXEC
     167                 :            : 
     168                 :            : #if MICROPY_PY_BUILTINS_EXECFILE
     169                 :          8 : STATIC mp_obj_t mp_builtin_execfile(size_t n_args, const mp_obj_t *args) {
     170                 :            :     // MP_PARSE_SINGLE_INPUT is used to indicate a file input
     171                 :          8 :     return eval_exec_helper(n_args, args, MP_PARSE_SINGLE_INPUT);
     172                 :            : }
     173                 :            : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_execfile_obj, 1, 3, mp_builtin_execfile);
     174                 :            : #endif

Generated by: LCOV version 1.15-5-g462f71d