Skip to content

Commit 2b46da2

Browse files
Colin Hogbenpfalcon
authored andcommitted
lib/utils/pyhelp: Extract implementation of help(obj) to a library function.
Several ports use identical code for the 1-argument form of the builtin help function. Move this code to a library function to allow easier re-use by ports.
1 parent 4296a8d commit 2b46da2

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

lib/utils/pyhelp.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 "lib/utils/pyhelp.h"
30+
31+
STATIC void pyhelp_print_info_about_object(mp_obj_t name_o, mp_obj_t value) {
32+
printf(" ");
33+
mp_obj_print(name_o, PRINT_STR);
34+
printf(" -- ");
35+
mp_obj_print(value, PRINT_STR);
36+
printf("\n");
37+
}
38+
39+
// Helper for 1-argument form of builtin help
40+
//
41+
// Typically a port will define a help function thus:
42+
//
43+
// STATIC const char *const myport_help_text =
44+
// "Welcome to MicroPython!\n"
45+
// "\n"
46+
// "...information specific to this port e.g. modules available...";
47+
//
48+
// STATIC mp_obj_t myport_help(mp_uint_t n_args, const mp_obj_t *args) {
49+
// if (n_args == 0) {
50+
// printf("%s", myport_help_text);
51+
// } else {
52+
// pyhelp_print_obj(args[0]);
53+
// }
54+
// return mp_const_none;
55+
// }
56+
// MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj, 0, 1, myport_help);
57+
//
58+
void pyhelp_print_obj(const mp_obj_t obj) {
59+
// try to print something sensible about the given object
60+
printf("object ");
61+
mp_obj_print(obj, PRINT_STR);
62+
printf(" is of type %s\n", mp_obj_get_type_str(obj));
63+
64+
mp_map_t *map = NULL;
65+
if (MP_OBJ_IS_TYPE(obj, &mp_type_module)) {
66+
map = mp_obj_dict_get_map(mp_obj_module_get_globals(obj));
67+
} else {
68+
mp_obj_type_t *type;
69+
if (MP_OBJ_IS_TYPE(obj, &mp_type_type)) {
70+
type = obj;
71+
} else {
72+
type = mp_obj_get_type(obj);
73+
}
74+
if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) {
75+
map = mp_obj_dict_get_map(type->locals_dict);
76+
}
77+
}
78+
if (map != NULL) {
79+
for (uint i = 0; i < map->alloc; i++) {
80+
if (map->table[i].key != MP_OBJ_NULL) {
81+
pyhelp_print_info_about_object(map->table[i].key, map->table[i].value);
82+
}
83+
}
84+
}
85+
}

lib/utils/pyhelp.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
#ifndef __MICROPY_INCLUDED_LIB_UTILS_PYHELP_H__
27+
#define __MICROPY_INCLUDED_LIB_UTILS_PYHELP_H__
28+
29+
#include "py/obj.h"
30+
31+
void pyhelp_print_obj(const mp_obj_t obj);
32+
33+
#endif // __MICROPY_INCLUDED_LIB_UTILS_PYHELP_H__

0 commit comments

Comments
 (0)