py/objtype: Add type.__bases__ attribute.

Enabled as part of MICROPY_CPYTHON_COMPAT.
This commit is contained in:
Josh Lloyd 2019-09-24 17:01:54 +12:00 committed by Damien George
parent 59c1b7d4b8
commit 8f9e2e325a
2 changed files with 60 additions and 0 deletions

View File

@ -1014,6 +1014,21 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
dest[0] = MP_OBJ_NEW_QSTR(self->name);
return;
}
if (attr == MP_QSTR___bases__) {
if (self == &mp_type_object) {
dest[0] = mp_const_empty_tuple;
return;
}
mp_obj_t parent_obj = self->parent ? MP_OBJ_FROM_PTR(self->parent) : MP_OBJ_FROM_PTR(&mp_type_object);
#if MICROPY_MULTIPLE_INHERITANCE
if (mp_obj_is_type(parent_obj, &mp_type_tuple)) {
dest[0] = parent_obj;
return;
}
#endif
dest[0] = mp_obj_new_tuple(1, &parent_obj);
return;
}
#endif
struct class_lookup_data lookup = {
.obj = (mp_obj_instance_t*)self,

View File

@ -0,0 +1,45 @@
# test for type.__bases__ implementation
if not hasattr(object, '__bases__'):
print("SKIP")
raise SystemExit
class A:
pass
class B(object):
pass
class C(B):
pass
class D(C, A):
pass
# Check the attribute exists
print(hasattr(A, '__bases__'))
print(hasattr(B, '__bases__'))
print(hasattr(C, '__bases__'))
print(hasattr(D, '__bases__'))
# Check it is always a tuple
print(type(A.__bases__) == tuple)
print(type(B.__bases__) == tuple)
print(type(C.__bases__) == tuple)
print(type(D.__bases__) == tuple)
# Check size
print(len(A.__bases__) == 1)
print(len(B.__bases__) == 1)
print(len(C.__bases__) == 1)
print(len(D.__bases__) == 2)
# Check values
print(A.__bases__[0] == object)
print(B.__bases__[0] == object)
print(C.__bases__[0] == B)
print(D.__bases__[0] == C)
print(D.__bases__[1] == A)
# Object has an empty tuple
print(object.__bases__ == tuple())