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'
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
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__'
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__'
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=[])
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)
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
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")
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
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")
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
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
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'
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)
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)
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
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
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__'
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__'