Skip to content

Commit 4acb245

Browse files
committed
py: Add very basic implementation of dir() builtin.
Only works on modules and class instances.
1 parent 0473e27 commit 4acb245

5 files changed

Lines changed: 34 additions & 1 deletion

File tree

py/builtin.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,36 @@ static mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
148148

149149
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
150150

151+
static mp_obj_t mp_builtin_dir(uint n_args, const mp_obj_t *args) {
152+
// TODO make this function more general and less of a hack
153+
154+
mp_map_t *map;
155+
if (n_args == 0) {
156+
// make a list of names in the local name space
157+
map = rt_locals_get();
158+
} else { // n_args == 1
159+
// make a list of names in the given object
160+
mp_obj_type_t *type = mp_obj_get_type(args[0]);
161+
if (type == &module_type) {
162+
map = mp_obj_module_get_globals(args[0]);
163+
} else if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &dict_type)) {
164+
map = mp_obj_dict_get_map(type->locals_dict);
165+
} else {
166+
return mp_obj_new_list(0, NULL);
167+
}
168+
}
169+
170+
mp_obj_t dir = mp_obj_new_list(0, NULL);
171+
for (uint i = 0; i < map->alloc; i++) {
172+
if (map->table[i].key != MP_OBJ_NULL) {
173+
mp_obj_list_append(dir, map->table[i].key);
174+
}
175+
}
176+
return dir;
177+
}
178+
179+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir);
180+
151181
static mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
152182
if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
153183
mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_any_obj);
88
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_bytes_obj); // Temporary hack
99
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_callable_obj);
1010
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj);
11+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_dir_obj);
1112
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_divmod_obj);
1213
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_eval_obj);
1314
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj);

py/objtype.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static mp_obj_t mp_obj_class_lookup(const mp_obj_type_t *type, qstr attr) {
3535
// search locals_dict (the dynamically created set of methods/attributes)
3636

3737
assert(MP_OBJ_IS_TYPE(type->locals_dict, &dict_type)); // Micro Python restriction, for now
38-
mp_map_t *locals_map = ((void*)type->locals_dict + sizeof(mp_obj_base_t)); // XXX hack to get map object from dict object
38+
mp_map_t *locals_map = mp_obj_dict_get_map(type->locals_dict);
3939
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
4040
if (elem != NULL) {
4141
return elem->value;

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Q(callable)
5151
Q(chr)
5252
Q(complex)
5353
Q(dict)
54+
Q(dir)
5455
Q(divmod)
5556
Q(enumerate)
5657
Q(eval)

py/runtime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ void rt_init(void) {
141141
mp_map_add_qstr(&map_builtins, MP_QSTR_bytes, (mp_obj_t)&mp_builtin_bytes_obj);
142142
mp_map_add_qstr(&map_builtins, MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj);
143143
mp_map_add_qstr(&map_builtins, MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj);
144+
mp_map_add_qstr(&map_builtins, MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj);
144145
mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj);
145146
mp_map_add_qstr(&map_builtins, MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj);
146147
mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj);

0 commit comments

Comments
 (0)