summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/builtin.c15
-rw-r--r--py/objtype.c79
-rw-r--r--py/runtime.c7
3 files changed, 46 insertions, 55 deletions
diff --git a/py/builtin.c b/py/builtin.c
index f9775a55a7..e8746c55c2 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -157,11 +157,18 @@ STATIC mp_obj_t mp_builtin_dir(uint n_args, const mp_obj_t *args) {
map = rt_locals_get();
} else { // n_args == 1
// make a list of names in the given object
- mp_obj_type_t *type = mp_obj_get_type(args[0]);
- if (type == &mp_type_module) {
+ if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) {
map = mp_obj_module_get_globals(args[0]);
- } else if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &dict_type)) {
- map = mp_obj_dict_get_map(type->locals_dict);
+ } else {
+ mp_obj_type_t *type;
+ if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) {
+ type = args[0];
+ } else {
+ type = mp_obj_get_type(args[0]);
+ }
+ if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &dict_type)) {
+ map = mp_obj_dict_get_map(type->locals_dict);
+ }
}
}
diff --git a/py/objtype.c b/py/objtype.c
index f0ded1aff9..4c709c7ab4 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -30,24 +30,13 @@ STATIC mp_obj_t mp_obj_new_class(mp_obj_t class) {
STATIC mp_obj_t mp_obj_class_lookup(const mp_obj_type_t *type, qstr attr) {
for (;;) {
if (type->locals_dict != NULL) {
- // search locals_dict (the dynamically created set of methods/attributes)
-
+ // search locals_dict (the set of methods/attributes)
assert(MP_OBJ_IS_TYPE(type->locals_dict, &dict_type)); // Micro Python restriction, for now
mp_map_t *locals_map = mp_obj_dict_get_map(type->locals_dict);
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
if (elem != NULL) {
return elem->value;
}
- /*
- } else if (type->methods != NULL) {
- // search methods (the const set of methods)
-
- for (const mp_method_t *meth = type->methods; meth->name != MP_QSTR_NULL; meth++) {
- if (meth->name == attr) {
- return (mp_obj_t)meth->fun;
- }
- }
- */
}
// attribute not found, keep searching base classes
@@ -220,24 +209,20 @@ STATIC void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
}
mp_obj_t member = mp_obj_class_lookup(self->base.type, attr);
if (member != MP_OBJ_NULL) {
- if (mp_obj_is_callable(member)) {
- // class member is callable so build a bound method
- // check if the methods are functions, static or class methods
- // see http://docs.python.org/3.3/howto/descriptor.html
- // TODO check that this is the correct place to have this logic
- if (MP_OBJ_IS_TYPE(member, &mp_type_staticmethod)) {
- // return just the function
- dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
- } else if (MP_OBJ_IS_TYPE(member, &mp_type_classmethod)) {
- // return a bound method, with self being the type of this object
- dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
- dest[1] = mp_obj_get_type(self_in);
- } else {
- // return a bound method, with self being this object
- dest[0] = member;
- dest[1] = self_in;
- }
- return;
+ // check if the methods are functions, static or class methods
+ // see http://docs.python.org/3.3/howto/descriptor.html
+ // TODO check that this is the correct place to have this logic
+ if (MP_OBJ_IS_TYPE(member, &mp_type_staticmethod)) {
+ // return just the function
+ dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
+ } else if (MP_OBJ_IS_TYPE(member, &mp_type_classmethod)) {
+ // return a bound method, with self being the type of this object
+ dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
+ dest[1] = mp_obj_get_type(self_in);
+ } else if (mp_obj_is_callable(member)) {
+ // return a bound method, with self being this object
+ dest[0] = member;
+ dest[1] = self_in;
} else {
// class member is a value, so just return that value
dest[0] = member;
@@ -425,29 +410,25 @@ STATIC void super_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
mp_obj_t member = mp_obj_class_lookup((mp_obj_type_t*)items[i], attr);
if (member != MP_OBJ_NULL) {
// XXX this and the code in class_load_attr need to be factored out
- if (mp_obj_is_callable(member)) {
- // class member is callable so build a bound method
- // check if the methods are functions, static or class methods
- // see http://docs.python.org/3.3/howto/descriptor.html
- // TODO check that this is the correct place to have this logic
- if (MP_OBJ_IS_TYPE(member, &mp_type_staticmethod)) {
- // return just the function
- dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
- } else if (MP_OBJ_IS_TYPE(member, &mp_type_classmethod)) {
- // return a bound method, with self being the type of this object
- dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
- dest[1] = mp_obj_get_type(self->obj);
- } else {
- // return a bound method, with self being this object
- dest[0] = member;
- dest[1] = self->obj;
- }
- return;
+ // check if the methods are functions, static or class methods
+ // see http://docs.python.org/3.3/howto/descriptor.html
+ // TODO check that this is the correct place to have this logic
+ if (MP_OBJ_IS_TYPE(member, &mp_type_staticmethod)) {
+ // return just the function
+ dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
+ } else if (MP_OBJ_IS_TYPE(member, &mp_type_classmethod)) {
+ // return a bound method, with self being the type of this object
+ dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
+ dest[1] = mp_obj_get_type(self->obj);
+ } if (mp_obj_is_callable(member)) {
+ // return a bound method, with self being this object
+ dest[0] = member;
+ dest[1] = self->obj;
} else {
// class member is a value, so just return that value
dest[0] = member;
- return;
}
+ return;
}
}
}
diff --git a/py/runtime.c b/py/runtime.c
index 1f4a524b70..6590e8e0d0 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -859,10 +859,13 @@ STATIC void rt_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
// return a bound method, with self being the type of this object
dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
dest[1] = mp_obj_get_type(base);
- } else {
+ } else if (mp_obj_is_callable(elem->value)) {
// return a bound method, with self being this object
- dest[0] = (mp_obj_t)elem->value;
+ dest[0] = elem->value;
dest[1] = base;
+ } else {
+ // class member is a value, so just return that value
+ dest[0] = elem->value;
}
}
}