Exemplo n.º 1
0
    def test_more_forbidden_subclasses(self):
        mul = multimethod.MultiMethodTable(2,
                                           root_class=W_Root,
                                           argnames_before=['space'])

        class UserW_StringObject(W_StringObject):
            pass

        def mul__String_String(space, w_x, w_y):
            assert space == 'space'
            assert isinstance(w_x, W_StringObject)
            assert isinstance(w_y, W_StringObject)
            return 'fine'

        mul.register(mul__String_String, W_StringObject, W_StringObject)

        ext_typeorder = {
            W_StringObject: [(W_StringObject, None)],
            UserW_StringObject: []
        }
        mul2 = mul.install('__mul2', [ext_typeorder, ext_typeorder])
        assert mul2('space', W_StringObject(), W_StringObject()) == 'fine'
        raises(FailedToImplement, mul2, 'baz', W_StringObject(),
               UserW_StringObject())
        raises(FailedToImplement, mul2, 'baz', UserW_StringObject(),
               W_StringObject())
        raises(FailedToImplement, mul2, 'baz', UserW_StringObject(),
               UserW_StringObject())
Exemplo n.º 2
0
    def test_forbidden_subclasses(self):
        mul = multimethod.MultiMethodTable(2,
                                           root_class=W_Root,
                                           argnames_before=['space'])

        class UserW_StringObject(W_StringObject):
            pass

        def mul__Int_String(space, w_x, w_y):
            assert space == 'space'
            assert isinstance(w_x, W_IntObject)
            assert isinstance(w_y, W_StringObject)
            return 'fine'

        mul.register(mul__Int_String, W_IntObject, W_StringObject)

        mul1 = mul.install('__mul1', [self.typeorder, self.typeorder])
        assert mul1('space', W_IntObject(), W_StringObject()) == 'fine'
        assert mul1('space', W_IntObject(), UserW_StringObject()) == 'fine'

        ext_typeorder = self.typeorder.copy()
        ext_typeorder[UserW_StringObject] = []
        mul2 = mul.install('__mul2', [ext_typeorder, ext_typeorder])
        assert mul2('space', W_IntObject(), W_StringObject()) == 'fine'
        raises(FailedToImplement, mul2, 'baz', W_IntObject(),
               UserW_StringObject())
Exemplo n.º 3
0
 def setup_class(cls):
     cls.prev_installer = multimethod.Installer
     multimethod.Installer = cls.Installer
     add = multimethod.MultiMethodTable(2,
                                        root_class=W_Root,
                                        argnames_before=['space'])
     add.register(add__Int_Int, W_IntObject, W_IntObject)
     typeorder = {
         W_IntObject: [(W_IntObject, None), (W_Root, None)],
         W_BoolObject: [(W_BoolObject, None), (W_IntObject, delegate_b2i),
                        (W_Root, None)],
         W_StringObject: [(W_StringObject, None), (W_Root, None)],
     }
     cls.typeorder = typeorder
     cls.add = add
     cls.add1 = staticmethod(add.install('__add', [typeorder, typeorder]))
Exemplo n.º 4
0
    def test_ANY(self):
        setattr = multimethod.MultiMethodTable(3,
                                               root_class=W_Root,
                                               argnames_before=['space'])

        def setattr__Int_ANY_ANY(space, w_x, w_y, w_z):
            assert space == 'space'
            assert isinstance(w_x, W_IntObject)
            assert isinstance(w_y, W_Root)
            assert isinstance(w_z, W_Root)
            return w_y.__class__.__name__ + w_z.__class__.__name__

        setattr.register(setattr__Int_ANY_ANY, W_IntObject, W_Root, W_Root)
        setattr1 = setattr.install('__setattr1', [self.typeorder] * 3)
        for cls1 in self.typeorder:
            for cls2 in self.typeorder:
                assert setattr1('space', W_IntObject(), cls1(),
                                cls2()) == (cls1.__name__ + cls2.__name__)
Exemplo n.º 5
0
        def test(indices):
            sub = multimethod.MultiMethodTable(2,
                                               root_class=W_Root,
                                               argnames_before=['space'])

            def addimpl(cls1, cls2):
                token = random.random()

                def sub__cls1_cls2(space, w_x, w_y):
                    assert space == 'space'
                    assert isinstance(w_x, cls1)
                    assert isinstance(w_y, cls2)
                    return token

                sub.register(sub__cls1_cls2, cls1, cls2)
                return token

            def check(w1, w2):
                try:
                    res = sub1(space, w1, w2)
                except FailedToImplement:
                    res = FailedToImplement
                for cls1 in w1.expected:
                    for cls2 in w2.expected:
                        if (cls1, cls2) in expected:
                            assert res == expected[cls1, cls2]
                            return
                else:
                    assert res is FailedToImplement

            random.shuffle(indices)
            expected = {}
            for index in indices:
                cls1, cls2 = choices[index]
                token = addimpl(cls1, cls2)
                expected[cls1, cls2] = token

            typeorder = self.typeorder
            sub1 = sub.install('__sub', [typeorder, typeorder])
            for w1 in [w_x, w_s, w_b]:
                for w2 in [w_x, w_s, w_b]:
                    check(w1, w2)