Given a class object, calculate the mro of the class
A linerization is defined as the class plus the merge of the linerization of all bases and the list of bases
"""
if inspect.isclass(kls):
mro = [kls]
# for each base class, we need to compute the linerization
merge_list = []
for basekls in kls.__bases__:
merge_list.append(compute_linearization(basekls))
# add all bases to the merge list
merge_list.append([])
for basekls in kls.__bases__:
merge_list[-1].append(basekls)
return mro + merge(merge_list)
else:
raise TypeError("argument must a class object")
"""
take the head of the first list, i.e L[B1][0]; if this head is not in the tail of any of the other lists, then add it to the linearization of C and remove it from the lists in the merge, otherwise look at the head of the next list and take it, if it is a good head. Then repeat the operation until all the class are removed or it is impossible to find good heads. In this case, it is impossible to construct the merge, Python 2.3 will refuse to create the class C and will raise an exception.
"""
def merge(merge_list):
res = []
whileTrue:
processed = False
has_good_head = False
for i, l in enumerate(merge_list):
if len(l):
# mark for processing
processed = True
head = l[0]
is_good_head = True
other_lists = merge_list[0:i] + merge_list[i+1:]
# check if the head is in the tail of other lists
for rest in other_lists:
if head in rest[1:]:
is_good_head = False
break
# if is a good head, then need to remove it from other lists