1.1 --- a/micropython/inspect.py Tue Feb 19 00:05:22 2008 +0100
1.2 +++ b/micropython/inspect.py Fri Feb 22 00:42:28 2008 +0100
1.3 @@ -252,14 +252,17 @@
1.4 self.node = node
1.5
1.6 self.bases = []
1.7 - self.instattr = set() # instance attributes
1.8 + self.instattr = set() # instance attributes
1.9 + self.instattr_relocated = set() # instance attributes which do not have
1.10 + # the same position as those of the same
1.11 + # name in some superclasses
1.12
1.13 - self.all_instattr = None # cache for instance_attributes
1.14 - self.all_instattr_names = None # from all_instattr
1.15 - self.all_classattr = None # cache for all_class_attributes
1.16 + self.all_instattr = None # cache for instance_attributes
1.17 + self.all_instattr_names = None # from all_instattr
1.18 + self.all_classattr = None # cache for all_class_attributes
1.19 self.all_classattr_names = None # from all_classattr
1.20 - self.allattr = None # cache for all_attributes
1.21 - self.allattr_names = None # from allattr
1.22 + self.allattr = None # cache for all_attributes
1.23 + self.allattr_names = None # from allattr
1.24
1.25 # Image generation details.
1.26
1.27 @@ -329,25 +332,74 @@
1.28
1.29 if self.all_instattr is None:
1.30 self.all_instattr = {}
1.31 - instattr = set()
1.32 + instattr = {}
1.33
1.34 reversed_bases = self.bases[:]
1.35 reversed_bases.reverse()
1.36 +
1.37 + # For the bases in reverse order, acquire instance attribute
1.38 + # details.
1.39 +
1.40 for cls in reversed_bases:
1.41 - instattr.update(cls.instance_attributes().keys())
1.42 + for name, attr in cls.instance_attributes().items():
1.43 + if not instattr.has_key(name):
1.44 + instattr[name] = set()
1.45 + instattr[name].add(attr.position)
1.46
1.47 # Record instance attributes provided by this class and its bases,
1.48 # along with their positions.
1.49
1.50 - instattr.update(self.instattr)
1.51 + for name in self.instattr:
1.52 + if not instattr.has_key(name):
1.53 + instattr[name] = set([-1]) # position not yet defined
1.54 +
1.55 + if not instattr:
1.56 + self.all_instattr = {}
1.57 + else:
1.58 + positions = instattr.items()
1.59 + instarray = [None] * len(positions)
1.60 +
1.61 + # Get the positions in ascending order of list size, with lists
1.62 + # of the same size ordered according to their smallest position
1.63 + # value.
1.64 +
1.65 + positions.sort(self._cmp_positions)
1.66 + #print self.name, positions
1.67
1.68 - for i, name in enumerate(instattr):
1.69 - self.all_instattr[name] = Attr(i, None, name)
1.70 + # Get the names in position order.
1.71 +
1.72 + held = []
1.73 +
1.74 + for name, pos in positions:
1.75 + pos = list(pos)
1.76 + if pos[0] != -1 and instarray[pos[0]] is None:
1.77 + instarray[pos[0]] = name, Attr(pos[0], None, name)
1.78 + else:
1.79 + if pos[0] != -1 or len(pos) > 1:
1.80 + self.instattr_relocated.add(name)
1.81 + held.append((name, pos))
1.82 +
1.83 + for i, attr in enumerate(instarray):
1.84 + if attr is None:
1.85 + name, pos = held.pop()
1.86 + instarray[i] = name, Attr(i, None, name)
1.87 +
1.88 + self.all_instattr = dict(instarray)
1.89
1.90 self.all_instattr_names = self.all_instattr.keys()
1.91
1.92 return self.all_instattr
1.93
1.94 + def _cmp_positions(self, a, b):
1.95 + name_a, list_a = a
1.96 + name_b, list_b = b
1.97 + if len(list_a) < len(list_b):
1.98 + return -1
1.99 + elif len(list_a) > len(list_b):
1.100 + return 1
1.101 + else:
1.102 + return cmp(min(list_a), min(list_b))
1.103 +
1.104 def all_attribute_names(self):
1.105
1.106 """
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/tests/subclass.py Fri Feb 22 00:42:28 2008 +0100
2.3 @@ -0,0 +1,32 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +class A:
2.7 + def __init__(self, x):
2.8 + self.x = x
2.9 +
2.10 + def a(self):
2.11 + pass
2.12 +
2.13 +class B(A):
2.14 + def b(self):
2.15 + pass
2.16 +
2.17 +class C(A, B):
2.18 + def __init__(self, x, y):
2.19 + self.x = x
2.20 + self.y = y
2.21 +
2.22 + def a(self):
2.23 + pass
2.24 +
2.25 +class D:
2.26 + def __init__(self, y):
2.27 + self.y = y
2.28 +
2.29 +class E(C, D):
2.30 + pass
2.31 +
2.32 +class F(A, D):
2.33 + pass
2.34 +
2.35 +# vim: tabstop=4 expandtab shiftwidth=4