Skip to content

Commit d86020a

Browse files
committed
objtype: Don't treat inheritance from "object" as from native type.
"object" type in MicroPython currently doesn't implement any methods, and hopefully, we'll try to stay like that for as long as possible. Even if we have to add something eventually, look up from there might be handled in adhoc manner, as last resort (that's not compliant with Python3 MRO, but we're already non-compliant). Hence: 1) no need to spend type trying to lookup anything in object; 2) no need to allocate subobject when explicitly inheriting from object; 3) and having multiple bases inheriting from object is not a case of incompatible multiple inheritance.
1 parent d0a5bf3 commit d86020a

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

py/objtype.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ STATIC int instance_count_native_bases(const mp_obj_type_t *type, const mp_obj_t
6767
int count = 0;
6868
for (uint i = 0; i < len; i++) {
6969
assert(MP_OBJ_IS_TYPE(items[i], &mp_type_type));
70-
if (is_native_type((const mp_obj_type_t *)items[i])) {
70+
const mp_obj_type_t *bt = (const mp_obj_type_t *)items[i];
71+
if (bt == &mp_type_object) {
72+
// Not a "real" type
73+
continue;
74+
}
75+
if (is_native_type(bt)) {
7176
*last_native_base = items[i];
7277
count++;
7378
} else {
@@ -144,7 +149,12 @@ STATIC void mp_obj_class_lookup(mp_obj_instance_t *o, const mp_obj_type_t *type,
144149
}
145150
for (uint i = 0; i < len - 1; i++) {
146151
assert(MP_OBJ_IS_TYPE(items[i], &mp_type_type));
147-
mp_obj_class_lookup(o, (mp_obj_type_t*)items[i], attr, meth_offset, dest);
152+
mp_obj_type_t *bt = (mp_obj_type_t*)items[i];
153+
if (bt == &mp_type_object) {
154+
// Not a "real" type
155+
continue;
156+
}
157+
mp_obj_class_lookup(o, bt, attr, meth_offset, dest);
148158
if (dest[0] != MP_OBJ_NULL) {
149159
return;
150160
}
@@ -153,6 +163,10 @@ STATIC void mp_obj_class_lookup(mp_obj_instance_t *o, const mp_obj_type_t *type,
153163
// search last base (simple tail recursion elimination)
154164
assert(MP_OBJ_IS_TYPE(items[len - 1], &mp_type_type));
155165
type = (mp_obj_type_t*)items[len - 1];
166+
if (type == &mp_type_object) {
167+
// Not a "real" type
168+
return;
169+
}
156170
}
157171
}
158172

tests/basics/subclass-native5.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Subclass from 2 bases explicitly subclasses from object
2+
3+
class Base1(object):
4+
pass
5+
6+
class Base2(object):
7+
pass
8+
9+
class Sub(Base1, Base2):
10+
pass
11+
12+
o = Sub()

0 commit comments

Comments
 (0)