LCOV - code coverage report
Current view: top level - py - builtinhelp.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-335-g9c7f0659e.info Lines: 67 67 100.0 %
Date: 2024-04-24 08:31:58 Functions: 6 6 100.0 %
Branches: 27 30 90.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-2016 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 <stdio.h>
      28                 :            : #include <string.h>
      29                 :            : 
      30                 :            : #include "py/builtin.h"
      31                 :            : #include "py/objmodule.h"
      32                 :            : 
      33                 :            : #if MICROPY_PY_BUILTINS_HELP
      34                 :            : 
      35                 :            : const char mp_help_default_text[] =
      36                 :            :     "Welcome to MicroPython!\n"
      37                 :            :     "\n"
      38                 :            :     "For online docs please visit http://docs.micropython.org/\n"
      39                 :            :     "\n"
      40                 :            :     "Control commands:\n"
      41                 :            :     "  CTRL-A        -- on a blank line, enter raw REPL mode\n"
      42                 :            :     "  CTRL-B        -- on a blank line, enter normal REPL mode\n"
      43                 :            :     "  CTRL-C        -- interrupt a running program\n"
      44                 :            :     "  CTRL-D        -- on a blank line, exit or do a soft reset\n"
      45                 :            :     "  CTRL-E        -- on a blank line, enter paste mode\n"
      46                 :            :     "\n"
      47                 :            :     "For further help on a specific object, type help(obj)\n"
      48                 :            : ;
      49                 :            : 
      50                 :         72 : static void mp_help_print_info_about_object(mp_obj_t name_o, mp_obj_t value) {
      51                 :         72 :     mp_print_str(MP_PYTHON_PRINTER, "  ");
      52                 :         72 :     mp_obj_print(name_o, PRINT_STR);
      53                 :         72 :     mp_print_str(MP_PYTHON_PRINTER, " -- ");
      54                 :         72 :     mp_obj_print(value, PRINT_STR);
      55                 :         72 :     mp_print_str(MP_PYTHON_PRINTER, "\n");
      56                 :         72 : }
      57                 :            : 
      58                 :            : #if MICROPY_PY_BUILTINS_HELP_MODULES
      59                 :          8 : static void mp_help_add_from_map(mp_obj_t list, const mp_map_t *map) {
      60         [ +  + ]:        164 :     for (size_t i = 0; i < map->alloc; i++) {
      61         [ +  - ]:        156 :         if (mp_map_slot_is_filled(map, i)) {
      62                 :        156 :             mp_obj_list_append(list, map->table[i].key);
      63                 :            :         }
      64                 :            :     }
      65                 :          8 : }
      66                 :            : 
      67                 :            : #if MICROPY_MODULE_FROZEN
      68                 :          4 : static void mp_help_add_from_names(mp_obj_t list, const char *name) {
      69         [ +  + ]:         52 :     while (*name) {
      70                 :         48 :         size_t len = strlen(name);
      71                 :            :         // name should end in '.py' and we strip it off
      72                 :         48 :         mp_obj_list_append(list, mp_obj_new_str(name, len - 3));
      73                 :         48 :         name += len + 1;
      74                 :            :     }
      75                 :          4 : }
      76                 :            : #endif
      77                 :            : 
      78                 :          4 : static void mp_help_print_modules(void) {
      79                 :          4 :     mp_obj_t list = mp_obj_new_list(0, NULL);
      80                 :            : 
      81                 :          4 :     mp_help_add_from_map(list, &mp_builtin_module_map);
      82                 :          4 :     mp_help_add_from_map(list, &mp_builtin_extensible_module_map);
      83                 :            : 
      84                 :            :     #if MICROPY_MODULE_FROZEN
      85                 :          4 :     extern const char mp_frozen_names[];
      86                 :          4 :     mp_help_add_from_names(list, mp_frozen_names);
      87                 :            :     #endif
      88                 :            : 
      89                 :            :     // sort the list so it's printed in alphabetical order
      90                 :          4 :     mp_obj_list_sort(1, &list, (mp_map_t *)&mp_const_empty_map);
      91                 :            : 
      92                 :            :     // print the list of modules in a column-first order
      93                 :            :     #define NUM_COLUMNS (4)
      94                 :            :     #define COLUMN_WIDTH (18)
      95                 :          4 :     size_t len;
      96                 :          4 :     mp_obj_t *items;
      97                 :          4 :     mp_obj_list_get(list, &len, &items);
      98                 :          4 :     unsigned int num_rows = (len + NUM_COLUMNS - 1) / NUM_COLUMNS;
      99         [ +  + ]:         56 :     for (unsigned int i = 0; i < num_rows; ++i) {
     100                 :            :         unsigned int j = i;
     101                 :        204 :         for (;;) {
     102                 :        204 :             int l = mp_print_str(MP_PYTHON_PRINTER, mp_obj_str_get_str(items[j]));
     103                 :        204 :             j += num_rows;
     104         [ +  + ]:        204 :             if (j >= len) {
     105                 :            :                 break;
     106                 :            :             }
     107                 :        152 :             int gap = COLUMN_WIDTH - l;
     108         [ +  + ]:        160 :             while (gap < 1) {
     109                 :          8 :                 gap += COLUMN_WIDTH;
     110                 :            :             }
     111         [ +  + ]:       1816 :             while (gap--) {
     112                 :       1664 :                 mp_print_str(MP_PYTHON_PRINTER, " ");
     113                 :            :             }
     114                 :            :         }
     115                 :         52 :         mp_print_str(MP_PYTHON_PRINTER, "\n");
     116                 :            :     }
     117                 :            : 
     118                 :            :     #if MICROPY_ENABLE_EXTERNAL_IMPORT
     119                 :            :     // let the user know there may be other modules available from the filesystem
     120                 :          4 :     mp_print_str(MP_PYTHON_PRINTER, "Plus any modules on the filesystem\n");
     121                 :            :     #endif
     122                 :          4 : }
     123                 :            : #endif
     124                 :            : 
     125                 :         20 : static void mp_help_print_obj(const mp_obj_t obj) {
     126                 :            :     #if MICROPY_PY_BUILTINS_HELP_MODULES
     127         [ +  + ]:         20 :     if (obj == MP_OBJ_NEW_QSTR(MP_QSTR_modules)) {
     128                 :          4 :         mp_help_print_modules();
     129                 :          4 :         return;
     130                 :            :     }
     131                 :            :     #endif
     132                 :            : 
     133                 :         16 :     const mp_obj_type_t *type = mp_obj_get_type(obj);
     134                 :            : 
     135                 :            :     // try to print something sensible about the given object
     136                 :         16 :     mp_print_str(MP_PYTHON_PRINTER, "object ");
     137                 :         16 :     mp_obj_print(obj, PRINT_STR);
     138                 :         16 :     mp_printf(MP_PYTHON_PRINTER, " is of type %q\n", type->name);
     139                 :            : 
     140                 :         16 :     mp_map_t *map = NULL;
     141         [ +  + ]:         16 :     if (type == &mp_type_module) {
     142                 :          4 :         map = &mp_obj_module_get_globals(obj)->map;
     143                 :            :     } else {
     144         [ +  + ]:         12 :         if (type == &mp_type_type) {
     145                 :          4 :             type = MP_OBJ_TO_PTR(obj);
     146                 :            :         }
     147         [ +  + ]:         12 :         if (MP_OBJ_TYPE_HAS_SLOT(type, locals_dict)) {
     148                 :          8 :             map = &MP_OBJ_TYPE_GET_SLOT(type, locals_dict)->map;
     149                 :            :         }
     150                 :            :     }
     151         [ +  - ]:         12 :     if (map != NULL) {
     152         [ +  + ]:         84 :         for (uint i = 0; i < map->alloc; i++) {
     153                 :         72 :             mp_obj_t key = map->table[i].key;
     154         [ +  - ]:         72 :             if (key != MP_OBJ_NULL) {
     155                 :         72 :                 mp_help_print_info_about_object(key, map->table[i].value);
     156                 :            :             }
     157                 :            :         }
     158                 :            :     }
     159                 :            : }
     160                 :            : 
     161                 :         24 : static mp_obj_t mp_builtin_help(size_t n_args, const mp_obj_t *args) {
     162         [ +  + ]:         24 :     if (n_args == 0) {
     163                 :            :         // print a general help message
     164                 :          4 :         mp_print_str(MP_PYTHON_PRINTER, MICROPY_PY_BUILTINS_HELP_TEXT);
     165                 :            :     } else {
     166                 :            :         // try to print something sensible about the given object
     167                 :         20 :         mp_help_print_obj(args[0]);
     168                 :            :     }
     169                 :            : 
     170                 :         24 :     return mp_const_none;
     171                 :            : }
     172                 :            : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj, 0, 1, mp_builtin_help);
     173                 :            : 
     174                 :            : #endif // MICROPY_PY_BUILTINS_HELP

Generated by: LCOV version 1.15-5-g462f71d