1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
class AppTest_make_proxy:
spaceconfig = {"objspace.std.withtproxy": True}
def test_errors(self):
from tputil import make_proxy
raises(TypeError, "make_proxy(None)")
raises(TypeError, "make_proxy(None, None)")
def f(): pass
raises(TypeError, "make_proxy(f)")
raises(TypeError, "make_proxy(f, None, None)")
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_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 == '__getitem__'
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_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_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 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
|