py: Make dir report instance members

This commit is contained in:
Dave Hylands 2015-12-28 20:03:15 -08:00 committed by Paul Sokolovsky
parent b50030b1d0
commit 7281d95aee
2 changed files with 20 additions and 1 deletions

View File

@ -31,6 +31,7 @@
#include "py/smallint.h"
#include "py/objint.h"
#include "py/objstr.h"
#include "py/objtype.h"
#include "py/runtime0.h"
#include "py/runtime.h"
#include "py/builtin.h"
@ -196,6 +197,7 @@ STATIC mp_obj_t mp_builtin_dir(mp_uint_t n_args, const mp_obj_t *args) {
// TODO make this function more general and less of a hack
mp_obj_dict_t *dict = NULL;
mp_map_t *members = NULL;
if (n_args == 0) {
// make a list of names in the local name space
dict = mp_locals_get();
@ -214,6 +216,10 @@ STATIC mp_obj_t mp_builtin_dir(mp_uint_t n_args, const mp_obj_t *args) {
dict = type->locals_dict;
}
}
if (mp_obj_is_instance_type(mp_obj_get_type(args[0]))) {
mp_obj_instance_t *inst = args[0];
members = &inst->members;
}
}
mp_obj_t dir = mp_obj_new_list(0, NULL);
@ -224,7 +230,13 @@ STATIC mp_obj_t mp_builtin_dir(mp_uint_t n_args, const mp_obj_t *args) {
}
}
}
if (members != NULL) {
for (mp_uint_t i = 0; i < members->alloc; i++) {
if (MP_MAP_SLOT_IS_FILLED(members, i)) {
mp_obj_list_append(dir, members->table[i].key);
}
}
}
return dir;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir);

View File

@ -7,3 +7,10 @@ print('__name__' in dir())
import sys
print('platform' in dir(sys))
class Foo:
def __init__(self):
self.x = 1
foo = Foo()
print('__init__' in dir(foo))
print('x' in dir(foo))