| 1 |
from rpython.rlib.objectmodel import specialize |
| 2 |
from rpython.rtyper.test.test_llinterp import interpret |
| 3 |
|
| 4 |
from pypy.objspace.fake.objspace import FakeObjSpace, is_root |
| 5 |
from pypy.interpreter.baseobjspace import W_Root |
| 6 |
from pypy.interpreter.typedef import TypeDef, GetSetProperty |
| 7 |
from pypy.interpreter.gateway import interp2app, ObjSpace |
| 8 |
|
| 9 |
|
| 10 |
def make_checker(): |
| 11 |
check = [] |
| 12 |
@specialize.memo() |
| 13 |
def see(): |
| 14 |
check.append(True) |
| 15 |
return see, check |
| 16 |
|
| 17 |
def test_wrap_interp2app(): |
| 18 |
see, check = make_checker() |
| 19 |
space = FakeObjSpace() |
| 20 |
assert len(space._seen_extras) == 2 |
| 21 |
assert len(check) == 0 |
| 22 |
interp2app(lambda space: see()).spacebind(space) |
| 23 |
assert len(space._seen_extras) == 3 |
| 24 |
assert len(check) == 0 |
| 25 |
space.translates() |
| 26 |
assert len(check) == 1 |
| 27 |
|
| 28 |
def test_wrap_interp2app_int(): |
| 29 |
see, check = make_checker() |
| 30 |
def foobar(space, x, w_y, z): |
| 31 |
is_root(w_y) |
| 32 |
see() |
| 33 |
return space.newint(x - z) |
| 34 |
space = FakeObjSpace() |
| 35 |
space.wrap(interp2app(foobar, unwrap_spec=[ObjSpace, int, W_Root, int])) |
| 36 |
space.translates() |
| 37 |
assert check |
| 38 |
|
| 39 |
def test_wrap_interp2app_later(): |
| 40 |
see, check = make_checker() |
| 41 |
|
| 42 |
@specialize.memo() |
| 43 |
def hithere(space): |
| 44 |
space.wrap(interp2app(foobar2)) |
| 45 |
|
| 46 |
def foobar(space): |
| 47 |
hithere(space) |
| 48 |
def foobar2(space): |
| 49 |
see() |
| 50 |
space = FakeObjSpace() |
| 51 |
space.wrap(interp2app(foobar)) |
| 52 |
space.translates() |
| 53 |
assert check |
| 54 |
|
| 55 |
def test_wrap_GetSetProperty(): |
| 56 |
see, check = make_checker() |
| 57 |
def foobar(w_obj, space): |
| 58 |
is_root(w_obj) |
| 59 |
see() |
| 60 |
return space.w_None |
| 61 |
space = FakeObjSpace() |
| 62 |
space.wrap(GetSetProperty(foobar)) |
| 63 |
space.translates() |
| 64 |
assert check |
| 65 |
|
| 66 |
|
| 67 |
def test_gettypefor_untranslated(): |
| 68 |
see, check = make_checker() |
| 69 |
class W_Foo(W_Root): |
| 70 |
def do_it(self, space, w_x): |
| 71 |
is_root(w_x) |
| 72 |
see() |
| 73 |
return W_Root() |
| 74 |
W_Foo.typedef = TypeDef('foo', |
| 75 |
__module__ = 'barmod', |
| 76 |
do_it = interp2app(W_Foo.do_it)) |
| 77 |
space = FakeObjSpace() |
| 78 |
space.gettypefor(W_Foo) |
| 79 |
assert not check |
| 80 |
space.translates() |
| 81 |
assert check |
| 82 |
|
| 83 |
def test_gettype_mro_untranslated(): |
| 84 |
space = FakeObjSpace() |
| 85 |
w_type = space.type(space.wrap(1)) |
| 86 |
assert len(w_type.mro_w) == 2 |
| 87 |
|
| 88 |
def test_gettype_mro(): |
| 89 |
space = FakeObjSpace() |
| 90 |
|
| 91 |
def f(i): |
| 92 |
w_x = space.newint(i) |
| 93 |
w_type = space.type(w_x) |
| 94 |
return len(w_type.mro_w) |
| 95 |
|
| 96 |
assert interpret(f, [1]) == 2 |
| 97 |
|
| 98 |
def test_see_objects(): |
| 99 |
see, check = make_checker() |
| 100 |
class W_Foo(W_Root): |
| 101 |
def __init__(self, x): |
| 102 |
self.x = x |
| 103 |
def do_it(self): |
| 104 |
if self.x == 42: |
| 105 |
return |
| 106 |
see() |
| 107 |
def f(): |
| 108 |
W_Foo(42).do_it() |
| 109 |
|
| 110 |
space = FakeObjSpace() |
| 111 |
space.translates(f) |
| 112 |
assert not check |
| 113 |
|
| 114 |
space = FakeObjSpace() |
| 115 |
space.translates(f, seeobj_w=[W_Foo(15)]) |
| 116 |
assert check |