Skip to content

Commit 9f04dfb

Browse files
committed
py: Add builtin help function to core, with default help msg.
This builtin is configured using MICROPY_PY_BUILTINS_HELP, and is disabled by default.
1 parent bd3dd92 commit 9f04dfb

5 files changed

Lines changed: 116 additions & 1 deletion

File tree

py/builtin.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ MP_DECLARE_CONST_FUN_OBJ_3(mp_builtin_setattr_obj);
5353
MP_DECLARE_CONST_FUN_OBJ_0(mp_builtin_globals_obj);
5454
MP_DECLARE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj);
5555
MP_DECLARE_CONST_FUN_OBJ_1(mp_builtin_hash_obj);
56+
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj);
5657
MP_DECLARE_CONST_FUN_OBJ_1(mp_builtin_hex_obj);
5758
MP_DECLARE_CONST_FUN_OBJ_1(mp_builtin_id_obj);
5859
MP_DECLARE_CONST_FUN_OBJ_2(mp_builtin_isinstance_obj);
@@ -72,7 +73,6 @@ MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_round_obj);
7273
MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj);
7374
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj);
7475
// Defined by a port, but declared here for simplicity
75-
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj);
7676
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj);
7777
MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj);
7878

@@ -118,4 +118,6 @@ extern const mp_obj_module_t mp_module_webrepl;
118118
extern const mp_obj_module_t mp_module_framebuf;
119119
extern const mp_obj_module_t mp_module_btree;
120120

121+
extern const char *MICROPY_PY_BUILTINS_HELP_TEXT;
122+
121123
#endif // __MICROPY_INCLUDED_PY_BUILTIN_H__

py/builtinhelp.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
29+
#include "py/builtin.h"
30+
31+
#if MICROPY_PY_BUILTINS_HELP
32+
33+
const char *mp_help_default_text =
34+
"Welcome to MicroPython!\n"
35+
"\n"
36+
"For online docs please visit http://docs.micropython.org/\n"
37+
"\n"
38+
"Control commands:\n"
39+
" CTRL-A -- on a blank line, enter raw REPL mode\n"
40+
" CTRL-B -- on a blank line, enter normal REPL mode\n"
41+
" CTRL-C -- interrupt a running program\n"
42+
" CTRL-D -- on a blank line, exit or do a soft reset\n"
43+
" CTRL-E -- on a blank line, enter paste mode\n"
44+
"\n"
45+
"For further help on a specific object, type help(obj)\n"
46+
;
47+
48+
STATIC void mp_help_print_info_about_object(mp_obj_t name_o, mp_obj_t value) {
49+
mp_print_str(MP_PYTHON_PRINTER, " ");
50+
mp_obj_print(name_o, PRINT_STR);
51+
mp_print_str(MP_PYTHON_PRINTER, " -- ");
52+
mp_obj_print(value, PRINT_STR);
53+
mp_print_str(MP_PYTHON_PRINTER, "\n");
54+
}
55+
56+
STATIC void mp_help_print_obj(const mp_obj_t obj) {
57+
// try to print something sensible about the given object
58+
mp_print_str(MP_PYTHON_PRINTER, "object ");
59+
mp_obj_print(obj, PRINT_STR);
60+
mp_printf(MP_PYTHON_PRINTER, " is of type %s\n", mp_obj_get_type_str(obj));
61+
62+
mp_map_t *map = NULL;
63+
if (MP_OBJ_IS_TYPE(obj, &mp_type_module)) {
64+
map = mp_obj_dict_get_map(mp_obj_module_get_globals(obj));
65+
} else {
66+
mp_obj_type_t *type;
67+
if (MP_OBJ_IS_TYPE(obj, &mp_type_type)) {
68+
type = obj;
69+
} else {
70+
type = mp_obj_get_type(obj);
71+
}
72+
if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) {
73+
map = mp_obj_dict_get_map(type->locals_dict);
74+
}
75+
}
76+
if (map != NULL) {
77+
for (uint i = 0; i < map->alloc; i++) {
78+
if (map->table[i].key != MP_OBJ_NULL) {
79+
mp_help_print_info_about_object(map->table[i].key, map->table[i].value);
80+
}
81+
}
82+
}
83+
}
84+
85+
STATIC mp_obj_t mp_builtin_help(size_t n_args, const mp_obj_t *args) {
86+
if (n_args == 0) {
87+
// print a general help message
88+
mp_print_str(MP_PYTHON_PRINTER, MICROPY_PY_BUILTINS_HELP_TEXT);
89+
} else {
90+
// try to print something sensible about the given object
91+
mp_help_print_obj(args[0]);
92+
}
93+
94+
return mp_const_none;
95+
}
96+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj, 0, 1, mp_builtin_help);
97+
98+
#endif // MICROPY_PY_BUILTINS_HELP

py/modbuiltins.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,9 @@ STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = {
666666
{ MP_ROM_QSTR(MP_QSTR_globals), MP_ROM_PTR(&mp_builtin_globals_obj) },
667667
{ MP_ROM_QSTR(MP_QSTR_hasattr), MP_ROM_PTR(&mp_builtin_hasattr_obj) },
668668
{ MP_ROM_QSTR(MP_QSTR_hash), MP_ROM_PTR(&mp_builtin_hash_obj) },
669+
#if MICROPY_PY_BUILTINS_HELP
670+
{ MP_ROM_QSTR(MP_QSTR_help), MP_ROM_PTR(&mp_builtin_help_obj) },
671+
#endif
669672
{ MP_ROM_QSTR(MP_QSTR_hex), MP_ROM_PTR(&mp_builtin_hex_obj) },
670673
{ MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&mp_builtin_id_obj) },
671674
{ MP_ROM_QSTR(MP_QSTR_isinstance), MP_ROM_PTR(&mp_builtin_isinstance_obj) },

py/mpconfig.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,17 @@ typedef double mp_float_t;
759759
#define MICROPY_PY_BUILTINS_MIN_MAX (1)
760760
#endif
761761

762+
// Whether to provide the help function
763+
#ifndef MICROPY_PY_BUILTINS_HELP
764+
#define MICROPY_PY_BUILTINS_HELP (0)
765+
#endif
766+
767+
// Use this to configure the help text shown for help(). It should be a
768+
// variable with the type "const char*". A sensible default is provided.
769+
#ifndef MICROPY_PY_BUILTINS_HELP_TEXT
770+
#define MICROPY_PY_BUILTINS_HELP_TEXT mp_help_default_text
771+
#endif
772+
762773
// Whether to set __file__ for imported modules
763774
#ifndef MICROPY_PY___FILE__
764775
#define MICROPY_PY___FILE__ (1)

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ PY_O_BASENAME = \
189189
binary.o \
190190
builtinimport.o \
191191
builtinevex.o \
192+
builtinhelp.o \
192193
modarray.o \
193194
modbuiltins.o \
194195
modcollections.o \

0 commit comments

Comments
 (0)