+ # support indexing on proxies (e.g., parent.cpu[0])
+ def __getitem__(self, key):
+ if not isinstance(key, int):
+ raise TypeError, "Proxy object requires integer index"
+ if self._path == None:
+ raise IndexError, "Index applied to 'any' proxy"
+ # replace index portion of last path element with new index
+ self._path[-1] = (self._path[-1][0], key)
+ return self
+
+ # support multiplying proxies by constants
+ def __mul__(self, other):
+ if not isinstance(other, int):
+ raise TypeError, "Proxy multiplier must be integer"
+ if self._multiplier == None:
+ self._multiplier = other
+ else:
+ # support chained multipliers
+ self._multiplier *= other
+ return self
+
+ def _mulcheck(self, result):
+ if self._multiplier == None:
+ return result
+ if not isinstance(result, int):
+ raise TypeError, "Proxy with multiplier resolves to " \
+ "non-integer value"
+ return result * self._multiplier
+
+ def unproxy(self, base, ptype):
+ obj = base
+ done = False
+ while not done:
+ if obj is None:
+ raise AttributeError, \
+ 'Parent of %s type %s not found at path %s' \
+ % (base.name, ptype, self._path)
+ result, done = obj.find(ptype, self._path)
+ obj = obj.parent
+
+ if isinstance(result, Proxy):
+ result = result.unproxy(obj, ptype)
+
+ return self._mulcheck(result)
+
+ def getindex(obj, index):
+ if index == None:
+ return obj
+ try:
+ obj = obj[index]
+ except TypeError:
+ if index != 0:
+ raise
+ # if index is 0 and item is not subscriptable, just
+ # use item itself (so cpu[0] works on uniprocessors)