Exemple #1
0
 def getattr(ins, s_attr):
     if s_attr.is_constant() and isinstance(s_attr.const, str):
         attr = s_attr.const
         if attr == '__class__':
             return ins.classdef.read_attr__class__()
         attrdef = ins.classdef.find_attribute(attr)
         position = getbookkeeper().position_key
         attrdef.read_locations[position] = True
         s_result = attrdef.getvalue()
         # hack: if s_result is a set of methods, discard the ones
         #       that can't possibly apply to an instance of ins.classdef.
         # XXX do it more nicely
         if isinstance(s_result, SomePBC):
             s_result = ins.classdef.lookup_filter(s_result, attr,
                                                   ins.flags)
         elif isinstance(s_result, SomeImpossibleValue):
             ins.classdef.check_missing_attribute_update(attr)
             # blocking is harmless if the attribute is explicitly listed
             # in the class or a parent class.
             for basedef in ins.classdef.getmro():
                 if basedef.classdesc.all_enforced_attrs is not None:
                     if attr in basedef.classdesc.all_enforced_attrs:
                         raise HarmlesslyBlocked("get enforced attr")
         elif isinstance(s_result, SomeList):
             s_result = ins.classdef.classdesc.maybe_return_immutable_list(
                 attr, s_result)
         return s_result
     return SomeObject()
Exemple #2
0
    def pbc_getattr(self, pbc, s_attr):
        assert s_attr.is_constant()
        attr = s_attr.const

        descs = list(pbc.descriptions)
        if not descs:
            return s_ImpossibleValue

        first = descs[0]
        if len(descs) == 1:
            return first.s_read_attribute(attr)

        change = first.mergeattrfamilies(descs[1:], attr)
        attrfamily = first.getattrfamily(attr)

        position = self.position_key
        attrfamily.read_locations[position] = True

        actuals = []
        for desc in descs:
            actuals.append(desc.s_read_attribute(attr))
        s_result = unionof(*actuals)

        s_oldvalue = attrfamily.get_s_value(attr)
        attrfamily.set_s_value(attr, unionof(s_result, s_oldvalue))

        if change:
            for position in attrfamily.read_locations:
                self.annotator.reflowfromposition(position)

        if isinstance(s_result, SomeImpossibleValue):
            for desc in descs:
                try:
                    attrs = desc.read_attribute('_attrs_')
                except AttributeError:
                    continue
                if isinstance(attrs, Constant):
                    attrs = attrs.value
                if attr in attrs:
                    raise HarmlesslyBlocked("getattr on enforced attr")

        return s_result