| 1 |
class AppTest_make_proxy: |
| 2 |
spaceconfig = {"objspace.std.withtproxy": True} |
| 3 |
|
| 4 |
def test_errors(self): |
| 5 |
from tputil import make_proxy |
| 6 |
raises(TypeError, "make_proxy(None)") |
| 7 |
raises(TypeError, "make_proxy(None, None)") |
| 8 |
def f(): pass |
| 9 |
raises(TypeError, "make_proxy(f)") |
| 10 |
raises(TypeError, "make_proxy(f, None, None)") |
| 11 |
|
| 12 |
def test_repr(self): |
| 13 |
from tputil import make_proxy |
| 14 |
|
| 15 |
class A(object): |
| 16 |
def append(self, item): |
| 17 |
pass |
| 18 |
|
| 19 |
l = [] |
| 20 |
def func(operation): |
| 21 |
l.append(repr(operation)) |
| 22 |
return operation.delegate() |
| 23 |
tp = make_proxy(func, obj=A()) |
| 24 |
tp.append(3) |
| 25 |
for rep in l: |
| 26 |
assert isinstance(rep, str) |
| 27 |
assert rep.find("append") != -1 |
| 28 |
|
| 29 |
def test_virtual_proxy(self): |
| 30 |
skip("XXX seems that proxies are more than a bit broken by now, but noone cares") |
| 31 |
class A(object): |
| 32 |
def __getitem__(self, item): |
| 33 |
pass |
| 34 |
|
| 35 |
def __getslice__(self, start, stop): |
| 36 |
xxx |
| 37 |
|
| 38 |
from tputil import make_proxy |
| 39 |
l = [] |
| 40 |
|
| 41 |
def f(*args): |
| 42 |
print(args) |
| 43 |
|
| 44 |
tp = make_proxy(f, type=A) |
| 45 |
|
| 46 |
tp[0:1] |
| 47 |
assert len(l) == 1 |
| 48 |
assert l[0].opname == '__getitem__' |
| 49 |
|
| 50 |
def test_simple(self): |
| 51 |
from tputil import make_proxy |
| 52 |
|
| 53 |
class A(object): |
| 54 |
def append(self, item): |
| 55 |
pass |
| 56 |
|
| 57 |
record = [] |
| 58 |
def func(operation): |
| 59 |
record.append(operation) |
| 60 |
return operation.delegate() |
| 61 |
l = make_proxy(func, obj=A()) |
| 62 |
l.append(1) |
| 63 |
assert len(record) == 1 |
| 64 |
i1, = record |
| 65 |
assert i1.opname == '__getattribute__' |
| 66 |
|
| 67 |
def test_missing_attr(self): |
| 68 |
from tputil import make_proxy |
| 69 |
|
| 70 |
class A(object): |
| 71 |
pass |
| 72 |
|
| 73 |
def func(operation): |
| 74 |
return operation.delegate() |
| 75 |
l = make_proxy(func, obj=A()) |
| 76 |
raises(AttributeError, "l.asdasd") |
| 77 |
|
| 78 |
def test_proxy_double(self): |
| 79 |
from tputil import make_proxy |
| 80 |
|
| 81 |
class A(object): |
| 82 |
def append(self, item): |
| 83 |
pass |
| 84 |
r1 = [] |
| 85 |
r2 = [] |
| 86 |
def func1(operation): |
| 87 |
r1.append(operation) |
| 88 |
return operation.delegate() |
| 89 |
def func2(operation): |
| 90 |
r2.append(operation) |
| 91 |
return operation.delegate() |
| 92 |
|
| 93 |
l = make_proxy(func1, obj=A()) |
| 94 |
l2 = make_proxy(func2, obj=l) |
| 95 |
assert not r1 and not r2 |
| 96 |
l2.append |
| 97 |
assert len(r2) == 1 |
| 98 |
assert r2[0].opname == '__getattribute__' |
| 99 |
assert len(r1) == 2 |
| 100 |
assert r1[0].opname == '__getattribute__' |
| 101 |
assert r1[0].args[0] == '__getattribute__' |
| 102 |
assert r1[1].opname == '__getattribute__' |
| 103 |
assert r1[1].args[0] == 'append' |
| 104 |
|
| 105 |
def test_proxy_inplace_add(self): |
| 106 |
r = [] |
| 107 |
from tputil import make_proxy |
| 108 |
|
| 109 |
class A(object): |
| 110 |
def __iadd__(self, other): |
| 111 |
return self |
| 112 |
|
| 113 |
def func1(operation): |
| 114 |
r.append(operation) |
| 115 |
return operation.delegate() |
| 116 |
|
| 117 |
l2 = make_proxy(func1, obj=A()) |
| 118 |
l = l2 |
| 119 |
l += [3] |
| 120 |
assert l is l2 |