def __contains__(self,ob):
     if isinstance(ob,ClassTypes):
         bases = self.subject._Protocol__adapters
         for base in getMRO(ob,True):
             if base in bases:
                 return bases[base][0] is not protocols.DOES_NOT_SUPPORT
     return False
Example #2
0
 def __contains__(self, ob):
     if isinstance(ob, ClassTypes):
         bases = self.subject._Protocol__adapters
         for base in getMRO(ob, True):
             if base in bases:
                 return bases[base][0] is not protocols.DOES_NOT_SUPPORT
     return False
 def testClassCriteriaSeedsAndDispatchFunctions(self):
     for klass in (Vehicle,LandVehicle,WaterVehicle,HumanPowered,GasPowered):
         seeds = list(ISeededCriterion(klass).seeds())
         self.failUnless(klass in seeds)
         self.failUnless(object in seeds)
         self.failIf(len(seeds)!=2)
         validateCriterion(klass,
             strategy.make_node_type(strategy.dispatch_by_mro),
             parents=[ICriterion(cls) for cls in getMRO(klass,True)])
 def testClassCriteriaSeedsAndDispatchFunctions(self):
     for klass in (Vehicle, LandVehicle, WaterVehicle, HumanPowered,
                   GasPowered):
         seeds = list(ISeededCriterion(klass).seeds())
         self.failUnless(klass in seeds)
         self.failUnless(object in seeds)
         self.failIf(len(seeds) != 2)
         validateCriterion(
             klass,
             strategy.make_node_type(strategy.dispatch_by_mro),
             parents=[ICriterion(cls) for cls in getMRO(klass, True)])
Example #5
0
def getInheritedRegistries(klass, registryName):
    """Minimal set of 'registryName' registries in reverse MRO order"""

    bases = klass.__bases__

    if len(bases) == 1:
        reg = getattr(bases[0], registryName, NOT_FOUND)
        if reg is not NOT_FOUND:
            yield reg

    else:
        mro = list(getMRO(klass))
        bases = [(-mro.index(b), b) for b in bases]
        bases.sort()
        for (b, b) in bases:
            reg = getattr(b, registryName, NOT_FOUND)
            if reg is not NOT_FOUND:
                yield reg
Example #6
0
    def _installedDescr(self, klass):
        # Return a newly installed descriptor proxy to use, or raise a usage
        # error if self doesn't know its own right name.

        from protocols.advice import getMRO
        name = self.attrName

        for cls in getMRO(klass):
            if name in cls.__dict__:
                if cls.__dict__[name] is self:
                    # Install a proxy, so we don't have to do this again!
                    descr = self._copyWithName(name)
                    setattr(cls, name, descr)
                    return descr
                else:
                    break

        # If we get here, we were not found under the name we were given
        self.usageError()
    def testSubclassCriterion(self):
        s = SubclassCriterion(Vehicle)
        validateCriterion(
            s,
            strategy.make_node_type(strategy.dispatch_by_subclass),
            parents=[SubclassCriterion(c) for c in getMRO(Vehicle, True)])

        # seeds()
        self.assertEqual(s.seeds(), [Vehicle, None])

        # __contains__
        for klass in Vehicle, LandVehicle, WaterVehicle:
            self.failUnless(klass in s)
        for klass in None, GasPowered, object, Wheeled:
            self.failUnless(klass not in s)

        # implies()
        self.failUnless(s.implies(SubclassCriterion(object)))
        self.failUnless(SubclassCriterion(LandVehicle).implies(s))
        self.failIf(s.implies(SubclassCriterion(LandVehicle)))
        self.failIf(SubclassCriterion(object).implies(s))

        # eq/ne/invert
        self.assertEqual(s, SubclassCriterion(Vehicle))
        self.assertNotEqual(s, SubclassCriterion(LandVehicle))

        # matches()
        table = {LandVehicle: 1, object: 2, None: 3}
        items = list(s.matches(table))
        self.assertEqual(items, [LandVehicle])

        # dispatch
        table = {Vehicle: 1, object: 2, None: 3}
        self.assertEqual(strategy.dispatch_by_subclass(table, Vehicle), 1)
        self.assertEqual(strategy.dispatch_by_subclass(table, LandVehicle), 1)
        self.assertEqual(strategy.dispatch_by_subclass(table, object), 2)
        self.assertEqual(strategy.dispatch_by_subclass(table, None), 3)
        self.assertRaises(AttributeError, strategy.dispatch_by_subclass, table,
                          Bicycle)
    def testSubclassCriterion(self):
        s = SubclassCriterion(Vehicle)
        validateCriterion(s,
            strategy.make_node_type(strategy.dispatch_by_subclass),
            parents=[SubclassCriterion(c) for c in getMRO(Vehicle,True)]
        )

        # seeds()
        self.assertEqual( s.seeds(), [Vehicle,None])

        # __contains__
        for klass in Vehicle,LandVehicle,WaterVehicle:
            self.failUnless(klass in s)
        for klass in None,GasPowered,object,Wheeled:
            self.failUnless(klass not in s)

        # implies()
        self.failUnless( s.implies(SubclassCriterion(object)) )
        self.failUnless( SubclassCriterion(LandVehicle).implies(s) )
        self.failIf( s.implies(SubclassCriterion(LandVehicle)) )
        self.failIf( SubclassCriterion(object).implies(s) )

        # eq/ne/invert
        self.assertEqual( s, SubclassCriterion(Vehicle))
        self.assertNotEqual( s, SubclassCriterion(LandVehicle))

        # matches()
        table = {LandVehicle:1,object:2,None:3}
        items = list(s.matches(table))
        self.assertEqual(items,[LandVehicle])

        # dispatch
        table = {Vehicle:1,object:2,None:3}
        self.assertEqual(strategy.dispatch_by_subclass(table, Vehicle), 1)
        self.assertEqual(strategy.dispatch_by_subclass(table, LandVehicle), 1)
        self.assertEqual(strategy.dispatch_by_subclass(table, object), 2)
        self.assertEqual(strategy.dispatch_by_subclass(table, None), 3)
        self.assertRaises(AttributeError,
            strategy.dispatch_by_subclass, table, Bicycle)
Example #9
0
 def parent_criteria(self):
     for cls in getMRO(self.subject, True):
         yield self.__class__(cls)
 def parent_criteria(self):
     for cls in getMRO(self.subject,True):
         yield self.__class__(cls)
Example #11
0
 def lookupKeys(self):
     return getMRO(self.subject)