LCOV - code coverage report
Current view: top level - py - builtinevex.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-343-g7b050b366.info Lines: 59 59 100.0 %
Date: 2024-04-25 17:00:48 Functions: 6 6 100.0 %
Branches: 36 52 69.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                 :         36 : static mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_dict_t *globals, mp_obj_dict_t *locals) {
      48                 :            :     // save context
      49                 :         36 :     nlr_jump_callback_node_globals_locals_t ctx;
      50                 :         36 :     ctx.globals = mp_globals_get();
      51                 :         36 :     ctx.locals = mp_locals_get();
      52                 :            : 
      53                 :            :     // set new context
      54                 :         36 :     mp_globals_set(globals);
      55                 :         36 :     mp_locals_set(locals);
      56                 :            : 
      57                 :            :     // set exception handler to restore context if an exception is raised
      58                 :         36 :     nlr_push_jump_callback(&ctx.callback, mp_globals_locals_set_from_nlr_jump_callback);
      59                 :            : 
      60                 :            :     // The call to mp_parse_compile_execute() in mp_builtin_compile() below passes
      61                 :            :     // NULL for the globals, so repopulate that entry now with the correct globals.
      62   [ -  +  -  +  :         36 :     if (mp_obj_is_type(self->module_fun, &mp_type_fun_bc)
          -  +  -  +  +  
                -  +  + ]
      63                 :            :         #if MICROPY_EMIT_NATIVE
      64   [ -  +  -  +  :         18 :         || mp_obj_is_type(self->module_fun, &mp_type_fun_native)
          -  +  -  +  +  
                -  +  - ]
      65                 :            :         #endif
      66                 :            :         ) {
      67                 :         36 :         mp_obj_fun_bc_t *fun_bc = MP_OBJ_TO_PTR(self->module_fun);
      68                 :         36 :         ((mp_module_context_t *)fun_bc->context)->module.globals = globals;
      69                 :            :     }
      70                 :            : 
      71                 :            :     // execute code
      72                 :         36 :     mp_obj_t ret = mp_call_function_0(self->module_fun);
      73                 :            : 
      74                 :            :     // deregister exception handler and restore context
      75                 :         28 :     nlr_pop_jump_callback(true);
      76                 :            : 
      77                 :            :     // return value
      78                 :         28 :     return ret;
      79                 :            : }
      80                 :            : 
      81                 :         52 : static mp_obj_t mp_builtin_compile(size_t n_args, const mp_obj_t *args) {
      82                 :         52 :     (void)n_args;
      83                 :            : 
      84                 :            :     // get the source
      85                 :         52 :     size_t str_len;
      86                 :         52 :     const char *str = mp_obj_str_get_data(args[0], &str_len);
      87                 :            : 
      88                 :            :     // get the filename
      89                 :         52 :     qstr filename = mp_obj_str_get_qstr(args[1]);
      90                 :            : 
      91                 :            :     // create the lexer
      92                 :         52 :     mp_lexer_t *lex = mp_lexer_new_from_str_len(filename, str, str_len, 0);
      93                 :            : 
      94                 :            :     // get the compile mode
      95                 :         52 :     qstr mode = mp_obj_str_get_qstr(args[2]);
      96                 :         52 :     mp_parse_input_kind_t parse_input_kind;
      97   [ +  +  +  + ]:         52 :     switch (mode) {
      98                 :            :         case MP_QSTR_single:
      99                 :            :             parse_input_kind = MP_PARSE_SINGLE_INPUT;
     100                 :            :             break;
     101                 :         24 :         case MP_QSTR_exec:
     102                 :         24 :             parse_input_kind = MP_PARSE_FILE_INPUT;
     103                 :         24 :             break;
     104                 :         12 :         case MP_QSTR_eval:
     105                 :         12 :             parse_input_kind = MP_PARSE_EVAL_INPUT;
     106                 :         12 :             break;
     107                 :            :         default:
     108                 :          4 :             mp_raise_ValueError(MP_ERROR_TEXT("bad compile mode"));
     109                 :            :     }
     110                 :            : 
     111                 :         48 :     mp_obj_code_t *code = mp_obj_malloc(mp_obj_code_t, &mp_type_code);
     112                 :         48 :     code->module_fun = mp_parse_compile_execute(lex, parse_input_kind, NULL, NULL);
     113                 :         36 :     return MP_OBJ_FROM_PTR(code);
     114                 :            : }
     115                 :            : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_compile_obj, 3, 6, mp_builtin_compile);
     116                 :            : 
     117                 :            : #endif // MICROPY_PY_BUILTINS_COMPILE
     118                 :            : 
     119                 :            : #if MICROPY_PY_BUILTINS_EVAL_EXEC
     120                 :            : 
     121                 :       1509 : static mp_obj_t eval_exec_helper(size_t n_args, const mp_obj_t *args, mp_parse_input_kind_t parse_input_kind) {
     122                 :            :     // work out the context
     123                 :       1509 :     mp_obj_dict_t *globals = mp_globals_get();
     124                 :       1509 :     mp_obj_dict_t *locals = mp_locals_get();
     125         [ +  + ]:       1588 :     for (size_t i = 1; i < 3 && i < n_args; ++i) {
     126         [ +  + ]:         87 :         if (args[i] != mp_const_none) {
     127   [ -  +  -  +  :         63 :             if (!mp_obj_is_type(args[i], &mp_type_dict)) {
          -  +  -  +  +  
                +  -  + ]
     128                 :          8 :                 mp_raise_TypeError(NULL);
     129                 :            :             }
     130                 :         55 :             locals = MP_OBJ_TO_PTR(args[i]);
     131         [ +  + ]:         55 :             if (i == 1) {
     132                 :         39 :                 globals = locals;
     133                 :            :             }
     134                 :            :         }
     135                 :            :     }
     136                 :            : 
     137                 :            :     #if MICROPY_PY_BUILTINS_COMPILE
     138   [ +  +  +  + ]:       1501 :     if (mp_obj_is_type(args[0], &mp_type_code)) {
     139                 :         36 :         return code_execute(MP_OBJ_TO_PTR(args[0]), globals, locals);
     140                 :            :     }
     141                 :            :     #endif
     142                 :            : 
     143                 :            : 
     144                 :            :     // create the lexer
     145                 :            :     // MP_PARSE_SINGLE_INPUT is used to indicate a file input
     146                 :       1465 :     mp_lexer_t *lex;
     147         [ +  + ]:       1465 :     if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) {
     148                 :         12 :         lex = mp_lexer_new_from_file(mp_obj_str_get_qstr(args[0]));
     149                 :          8 :         parse_input_kind = MP_PARSE_FILE_INPUT;
     150                 :            :     } else {
     151                 :            :         // Extract the source code.
     152                 :       1453 :         mp_buffer_info_t bufinfo;
     153                 :       1453 :         mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
     154                 :            : 
     155                 :       1453 :         lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, bufinfo.buf, bufinfo.len, 0);
     156                 :            :     }
     157                 :            : 
     158                 :       1455 :     return mp_parse_compile_execute(lex, parse_input_kind, globals, locals);
     159                 :            : }
     160                 :            : 
     161                 :        370 : static mp_obj_t mp_builtin_eval(size_t n_args, const mp_obj_t *args) {
     162                 :        370 :     return eval_exec_helper(n_args, args, MP_PARSE_EVAL_INPUT);
     163                 :            : }
     164                 :            : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_eval_obj, 1, 3, mp_builtin_eval);
     165                 :            : 
     166                 :       1127 : static mp_obj_t mp_builtin_exec(size_t n_args, const mp_obj_t *args) {
     167                 :       1127 :     return eval_exec_helper(n_args, args, MP_PARSE_FILE_INPUT);
     168                 :            : }
     169                 :            : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_exec_obj, 1, 3, mp_builtin_exec);
     170                 :            : 
     171                 :            : #endif // MICROPY_PY_BUILTINS_EVAL_EXEC
     172                 :            : 
     173                 :            : #if MICROPY_PY_BUILTINS_EXECFILE
     174                 :         12 : static mp_obj_t mp_builtin_execfile(size_t n_args, const mp_obj_t *args) {
     175                 :            :     // MP_PARSE_SINGLE_INPUT is used to indicate a file input
     176                 :         12 :     return eval_exec_helper(n_args, args, MP_PARSE_SINGLE_INPUT);
     177                 :            : }
     178                 :            : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_execfile_obj, 1, 3, mp_builtin_execfile);
     179                 :            : #endif

Generated by: LCOV version 1.15-5-g462f71d