Ejemplo n.º 1
0
    def test_proxy_double(self):
        from tputil import make_proxy

        class A(object):
            def append(self, item):
                pass
        r1 = []
        r2 = []
        def func1(operation):
            r1.append(operation)
            return operation.delegate()
        def func2(operation):
            r2.append(operation)
            return operation.delegate()

        l = make_proxy(func1, obj=A())
        l2 = make_proxy(func2, obj=l)
        assert not r1 and not r2
        l2.append
        assert len(r2) == 1
        assert r2[0].opname == '__getattribute__'
        assert len(r1) == 2
        assert r1[0].opname == '__getattribute__'
        assert r1[0].args[0] == '__getattribute__'
        assert r1[1].opname == '__getattribute__'
        assert r1[1].args[0] == 'append'
Ejemplo n.º 2
0
def make_show_proxy(instance):
    def controller(operation):
        print "proxy sees:", operation
        res = operation.delegate()
        return res
    tproxy = make_proxy(controller, obj=instance)
    return tproxy
Ejemplo n.º 3
0
 def test_virtual_proxy(self):
     from tputil import make_proxy 
     l = []
     tp = make_proxy(l.append, type=list)
     x = tp[0:1]
     assert len(l) == 1
     assert l[0].opname == '__getslice__'
Ejemplo n.º 4
0
 def test_virtual_proxy(self):
     from tputil import make_proxy
     l = []
     tp = make_proxy(l.append, type=list)
     x = len(tp)
     assert len(l) == 1
     assert l[0].opname == '__len__'
Ejemplo n.º 5
0
 def test_virtual_proxy(self):
     from tputil import make_proxy
     l = []
     tp = make_proxy(l.append, type=list)
     x = tp[0:1]
     assert len(l) == 1
     assert l[0].opname == '__getslice__'
Ejemplo n.º 6
0
 def test_virtual_proxy(self):
     from tputil import make_proxy 
     l = []
     tp = make_proxy(l.append, type=list)
     x = len(tp)
     assert len(l) == 1
     assert l[0].opname == '__len__'
Ejemplo n.º 7
0
    def test_snippet_1(self):
        from tputil import make_proxy
        history = []
        def recorder(operation):
            history.append(operation) 
            return operation.delegate()

        l = make_proxy(recorder, obj=[])
Ejemplo n.º 8
0
        def tb_set_next(tb, tb_next):
            def controller(op):
                if op.opname == "__getattribute__" and op.args[0] == "tb_next":
                    return tb_next

                return op.delegate()

            return tputil.make_proxy(controller, obj=tb)
Ejemplo n.º 9
0
 def perform(operation): 
     res = operation.delegate()
     if ischangeop(operation):
         print "persisting after:", operation 
         storage.dump(instance)
     if res is not operation.proxyobj and isinstance(res, (dict, list)):
         res = make_proxy(perform, obj=res)
     return res
Ejemplo n.º 10
0
 def perform(operation):
     res = operation.delegate()
     if ischangeop(operation):
         print "persisting after:", operation
         storage.dump(instance)
     if res is not operation.proxyobj and isinstance(res, (dict, list)):
         res = make_proxy(perform, obj=res)
     return res
Ejemplo n.º 11
0
    def test_missing_attr(self):
        from tputil import make_proxy

        def func(operation):
            return operation.delegate()

        l = make_proxy(func, obj=[])
        raises(AttributeError, "l.asdasd")
Ejemplo n.º 12
0
        def tb_set_next(tb: TracebackType,
                        tb_next: t.Optional[TracebackType]) -> TracebackType:
            def controller(op):  # type: ignore
                if op.opname == "__getattribute__" and op.args[0] == "tb_next":
                    return tb_next

                return op.delegate()

            return tputil.make_proxy(controller, obj=tb)  # type: ignore
Ejemplo n.º 13
0
    def test_missing_attr(self):
        from tputil import make_proxy

        class A(object):
            pass

        def func(operation):
            return operation.delegate()
        l = make_proxy(func, obj=A())
        raises(AttributeError, "l.asdasd")
Ejemplo n.º 14
0
 def test_repr(self):
     from tputil import make_proxy 
     l = []
     def func(operation): 
         l.append(repr(operation))
         return operation.delegate()
     tp = make_proxy(func, obj=[])
     tp.append(3)
     for rep in l:
         assert isinstance(rep, str)
         assert rep.find("list") != -1
Ejemplo n.º 15
0
    def test_proxy_inplace_add(self):
        r = []
        from tputil import make_proxy 
        def func1(operation):
            r.append(operation)
            return operation.delegate()

        l2 = make_proxy(func1, obj=[])
        l = l2
        l += [3]
        assert l is l2
Ejemplo n.º 16
0
    def test_proxy_inplace_add(self):
        r = []
        from tputil import make_proxy

        def func1(operation):
            r.append(operation)
            return operation.delegate()

        l2 = make_proxy(func1, obj=[])
        l = l2
        l += [3]
        assert l is l2
Ejemplo n.º 17
0
 def test_simple(self):
     from tputil import make_proxy 
     record = []
     def func(operation):
         record.append(operation)
         return operation.delegate()
     l = make_proxy(func, obj=[])
     l.append(1)
     assert len(record) == 2
     i1, i2 = record 
     assert i1.opname == '__getattribute__'
     assert i2.opname == 'append' 
Ejemplo n.º 18
0
    def test_repr(self):
        from tputil import make_proxy
        l = []

        def func(operation):
            l.append(repr(operation))
            return operation.delegate()

        tp = make_proxy(func, obj=[])
        tp.append(3)
        for rep in l:
            assert isinstance(rep, str)
            assert rep.find("list") != -1
Ejemplo n.º 19
0
    def copy_tb(base_tb, tb_next):
        def controller(operation):
            # Rationale for pragma: I looked fairly carefully and tried a few
            # things, and AFAICT it's not actually possible to get any
            # 'opname' that isn't __getattr__ or __getattribute__. So there's
            # no missing test we could add, and no value in coverage nagging
            # us about adding one.
            if operation.opname in ["__getattribute__",
                                    "__getattr__"]:  # pragma: no cover
                if operation.args[0] == "tb_next":
                    return tb_next
            return operation.delegate()

        return tputil.make_proxy(controller, type(base_tb), base_tb)
Ejemplo n.º 20
0
    def test_simple(self):
        from tputil import make_proxy
        record = []

        def func(operation):
            record.append(operation)
            return operation.delegate()

        l = make_proxy(func, obj=[])
        l.append(1)
        assert len(record) == 2
        i1, i2 = record
        assert i1.opname == '__getattribute__'
        assert i2.opname == 'append'
Ejemplo n.º 21
0
    def copy_tb(base_tb, tb_next):
        def controller(operation):
            # Rationale for pragma: I looked fairly carefully and tried a few
            # things, and AFAICT it's not actually possible to get any
            # 'opname' that isn't __getattr__ or __getattribute__. So there's
            # no missing test we could add, and no value in coverage nagging
            # us about adding one.
            if operation.opname in [
                "__getattribute__", "__getattr__"
            ]:  # pragma: no cover
                if operation.args[0] == "tb_next":
                    return tb_next
            return operation.delegate()

        return tputil.make_proxy(controller, type(base_tb), base_tb)
Ejemplo n.º 22
0
    def test_repr(self):
        from tputil import make_proxy

        class A(object):
            def append(self, item):
                pass

        l = []
        def func(operation):
            l.append(repr(operation))
            return operation.delegate()
        tp = make_proxy(func, obj=A())
        tp.append(3)
        for rep in l:
            assert isinstance(rep, str)
            assert rep.find("append") != -1
Ejemplo n.º 23
0
    def test_proxy_inplace_add(self):
        r = []
        from tputil import make_proxy

        class A(object):
            def __iadd__(self, other):
                return self

        def func1(operation):
            r.append(operation)
            return operation.delegate()

        l2 = make_proxy(func1, obj=A())
        l = l2
        l += [3]
        assert l is l2
Ejemplo n.º 24
0
    def test_simple(self):
        from tputil import make_proxy

        class A(object):
            def append(self, item):
                pass

        record = []
        def func(operation):
            record.append(operation)
            return operation.delegate()
        l = make_proxy(func, obj=A())
        l.append(1)
        assert len(record) == 1
        i1, = record
        assert i1.opname == '__getattribute__'
Ejemplo n.º 25
0
    def test_virtual_proxy(self):
        skip("XXX seems that proxies are more than a bit broken by now, but noone cares")
        class A(object):
            def __getitem__(self, item):
                pass

            def __getslice__(self, start, stop):
                xxx

        from tputil import make_proxy
        l = []

        def f(*args):
            print(args)

        tp = make_proxy(f, type=A)
        #tp.__getslice__(0, 1)
        tp[0:1]
        assert len(l) == 1
        assert l[0].opname == '__getslice__'