| 1 |
|
| 2 |
import py |
| 3 |
from pypy.interpreter.gateway import appdef, ApplevelClass, applevel_temp |
| 4 |
from pypy.interpreter.error import OperationError |
| 5 |
|
| 6 |
def test_execwith_novars(space): |
| 7 |
val = space.appexec([], """ |
| 8 |
(): |
| 9 |
return 42 |
| 10 |
""") |
| 11 |
assert space.eq_w(val, space.wrap(42)) |
| 12 |
|
| 13 |
def test_execwith_withvars(space): |
| 14 |
val = space.appexec([space.wrap(7)], """ |
| 15 |
(x): |
| 16 |
y = 6 * x |
| 17 |
return y |
| 18 |
""") |
| 19 |
assert space.eq_w(val, space.wrap(42)) |
| 20 |
|
| 21 |
def test_execwith_compile_error(space): |
| 22 |
excinfo = py.test.raises(OperationError, space.appexec, [], """ |
| 23 |
(): |
| 24 |
y y |
| 25 |
""") |
| 26 |
|
| 27 |
|
| 28 |
assert str(excinfo.value.get_w_value(space)).find('y y') != -1 |
| 29 |
|
| 30 |
def test_simple_applevel(space): |
| 31 |
app = appdef("""app(x,y): |
| 32 |
return x + y |
| 33 |
""") |
| 34 |
assert app.__name__ == 'app' |
| 35 |
w_result = app(space, space.wrap(41), space.wrap(1)) |
| 36 |
assert space.eq_w(w_result, space.wrap(42)) |
| 37 |
|
| 38 |
def test_applevel_with_one_default(space): |
| 39 |
app = appdef("""app(x,y=1): |
| 40 |
return x + y |
| 41 |
""") |
| 42 |
assert app.__name__ == 'app' |
| 43 |
w_result = app(space, space.wrap(41)) |
| 44 |
assert space.eq_w(w_result, space.wrap(42)) |
| 45 |
|
| 46 |
def test_applevel_with_two_defaults(space): |
| 47 |
app = appdef("""app(x=1,y=2): |
| 48 |
return x + y |
| 49 |
""") |
| 50 |
w_result = app(space, space.wrap(41), space.wrap(1)) |
| 51 |
assert space.eq_w(w_result, space.wrap(42)) |
| 52 |
|
| 53 |
w_result = app(space, space.wrap(15)) |
| 54 |
assert space.eq_w(w_result, space.wrap(17)) |
| 55 |
|
| 56 |
w_result = app(space) |
| 57 |
assert space.eq_w(w_result, space.wrap(3)) |
| 58 |
|
| 59 |
|
| 60 |
def test_applevel_noargs(space): |
| 61 |
app = appdef("""app(): |
| 62 |
return 42 |
| 63 |
""") |
| 64 |
assert app.__name__ == 'app' |
| 65 |
w_result = app(space) |
| 66 |
assert space.eq_w(w_result, space.wrap(42)) |
| 67 |
|
| 68 |
def somefunc(arg2=42): |
| 69 |
return arg2 |
| 70 |
|
| 71 |
def test_app2interp_somefunc(space): |
| 72 |
app = appdef(somefunc) |
| 73 |
w_result = app(space) |
| 74 |
assert space.eq_w(w_result, space.wrap(42)) |
| 75 |
|
| 76 |
def test_applevel_functions(space, applevel_temp = applevel_temp): |
| 77 |
app = applevel_temp(''' |
| 78 |
def f(x, y): |
| 79 |
return x-y |
| 80 |
def g(x, y): |
| 81 |
return f(y, x) |
| 82 |
''') |
| 83 |
g = app.interphook('g') |
| 84 |
w_res = g(space, space.wrap(10), space.wrap(1)) |
| 85 |
assert space.eq_w(w_res, space.wrap(-9)) |
| 86 |
|
| 87 |
def test_applevel_class(space, applevel_temp = applevel_temp): |
| 88 |
app = applevel_temp(''' |
| 89 |
class C(object): |
| 90 |
clsattr = 42 |
| 91 |
def __init__(self, x=13): |
| 92 |
self.attr = x |
| 93 |
''') |
| 94 |
C = app.interphook('C') |
| 95 |
c = C(space, space.wrap(17)) |
| 96 |
w_attr = space.getattr(c, space.wrap('clsattr')) |
| 97 |
assert space.eq_w(w_attr, space.wrap(42)) |
| 98 |
w_clsattr = space.getattr(c, space.wrap('attr')) |
| 99 |
assert space.eq_w(w_clsattr, space.wrap(17)) |
| 100 |
|
| 101 |
class AppTestMethods: |
| 102 |
def test_some_app_test_method(self): |
| 103 |
assert 2 == 2 |
| 104 |
|
| 105 |
class TestMixedModule: |
| 106 |
def test_accesses(self): |
| 107 |
space = self.space |
| 108 |
from .demomixedmod.moduledef import Module |
| 109 |
w_module = Module(space, space.wrap('mixedmodule')) |
| 110 |
space.appexec([w_module], """ |
| 111 |
(module): |
| 112 |
assert module.value is None |
| 113 |
assert module.__doc__ == 'mixedmodule doc' |
| 114 |
|
| 115 |
assert module.somefunc is module.somefunc |
| 116 |
result = module.somefunc() |
| 117 |
assert result == True |
| 118 |
|
| 119 |
assert module.someappfunc is module.someappfunc |
| 120 |
appresult = module.someappfunc(41) |
| 121 |
assert appresult == 42 |
| 122 |
|
| 123 |
assert module.__dict__ is module.__dict__ |
| 124 |
for name in ('somefunc', 'someappfunc', '__doc__', '__name__'): |
| 125 |
assert name in module.__dict__ |
| 126 |
""") |
| 127 |
assert space.is_true(w_module.call('somefunc')) |
| 128 |
assert Module.get_applevel_name() == 'demomixedmod' |
| 129 |
|
| 130 |
def test_whacking_at_loaders(self): |
| 131 |
"""Some MixedModules change 'self.loaders' in __init__(), but doing |
| 132 |
so they incorrectly mutated a class attribute. 'loaders' is now a |
| 133 |
per-instance attribute, holding a fresh copy of the dictionary. |
| 134 |
""" |
| 135 |
from pypy.interpreter.mixedmodule import MixedModule |
| 136 |
from pypy.tool.pytest.objspace import maketestobjspace |
| 137 |
|
| 138 |
class MyModule(MixedModule): |
| 139 |
interpleveldefs = {} |
| 140 |
appleveldefs = {} |
| 141 |
def __init__(self, space, w_name): |
| 142 |
def loader(myspace): |
| 143 |
assert myspace is space |
| 144 |
return myspace.wrap("hello") |
| 145 |
MixedModule.__init__(self, space, w_name) |
| 146 |
self.loaders["hi"] = loader |
| 147 |
|
| 148 |
space1 = self.space |
| 149 |
w_mymod1 = MyModule(space1, space1.wrap('mymod')) |
| 150 |
|
| 151 |
space2 = maketestobjspace() |
| 152 |
w_mymod2 = MyModule(space2, space2.wrap('mymod')) |
| 153 |
|
| 154 |
w_str = space1.getattr(w_mymod1, space1.wrap("hi")) |
| 155 |
assert space1.text_w(w_str) == "hello" |
| 156 |
|