Skip to content

Commit badc9d4

Browse files
committed
py: Improve dir(): extract names from type->methods table.
1 parent 5e756c9 commit badc9d4

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

py/builtin.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
151151
STATIC mp_obj_t mp_builtin_dir(uint n_args, const mp_obj_t *args) {
152152
// TODO make this function more general and less of a hack
153153

154-
mp_map_t *map;
154+
mp_map_t *map = NULL;
155+
const mp_method_t *meth = NULL;
155156
if (n_args == 0) {
156157
// make a list of names in the local name space
157158
map = rt_locals_get();
@@ -162,15 +163,23 @@ STATIC mp_obj_t mp_builtin_dir(uint n_args, const mp_obj_t *args) {
162163
map = mp_obj_module_get_globals(args[0]);
163164
} else if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &dict_type)) {
164165
map = mp_obj_dict_get_map(type->locals_dict);
165-
} else {
166-
return mp_obj_new_list(0, NULL);
166+
}
167+
if (type->methods != NULL) {
168+
meth = type->methods;
167169
}
168170
}
169171

170172
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);
173+
if (map != NULL) {
174+
for (uint i = 0; i < map->alloc; i++) {
175+
if (map->table[i].key != MP_OBJ_NULL) {
176+
mp_obj_list_append(dir, map->table[i].key);
177+
}
178+
}
179+
}
180+
if (meth != NULL) {
181+
for (; meth->name != NULL; meth++) {
182+
mp_obj_list_append(dir, MP_OBJ_NEW_QSTR(qstr_from_str(meth->name)));
174183
}
175184
}
176185
return dir;

0 commit comments

Comments
 (0)