File: test_appinterp.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (156 lines) | stat: -rw-r--r-- 4,899 bytes parent folder | download | duplicates (4)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

import py
from pypy.interpreter.gateway import appdef, ApplevelClass, applevel_temp
from pypy.interpreter.error import OperationError

def test_execwith_novars(space):
    val = space.appexec([], """
    ():
        return 42
    """)
    assert space.eq_w(val, space.wrap(42))

def test_execwith_withvars(space):
    val = space.appexec([space.wrap(7)], """
    (x):
        y = 6 * x
        return y
    """)
    assert space.eq_w(val, space.wrap(42))

def test_execwith_compile_error(space):
    excinfo = py.test.raises(OperationError, space.appexec, [], """
    ():
        y y
    """)
    # NOTE: the following test only works because excinfo.value is not
    # normalized so far
    assert str(excinfo.value.get_w_value(space)).find('y y') != -1

def test_simple_applevel(space):
    app = appdef("""app(x,y):
        return x + y
    """)
    assert app.__name__ == 'app'
    w_result = app(space, space.wrap(41), space.wrap(1))
    assert space.eq_w(w_result, space.wrap(42))

def test_applevel_with_one_default(space):
    app = appdef("""app(x,y=1):
        return x + y
    """)
    assert app.__name__ == 'app'
    w_result = app(space, space.wrap(41))
    assert space.eq_w(w_result, space.wrap(42))

def test_applevel_with_two_defaults(space):
    app = appdef("""app(x=1,y=2):
        return x + y
    """)
    w_result = app(space, space.wrap(41), space.wrap(1))
    assert space.eq_w(w_result, space.wrap(42))

    w_result = app(space, space.wrap(15))
    assert space.eq_w(w_result, space.wrap(17))

    w_result = app(space)
    assert space.eq_w(w_result, space.wrap(3))


def test_applevel_noargs(space):
    app = appdef("""app():
        return 42
    """)
    assert app.__name__ == 'app'
    w_result = app(space)
    assert space.eq_w(w_result, space.wrap(42))

def somefunc(arg2=42):
    return arg2

def test_app2interp_somefunc(space):
    app = appdef(somefunc)
    w_result = app(space)
    assert space.eq_w(w_result, space.wrap(42))

def test_applevel_functions(space, applevel_temp = applevel_temp):
    app = applevel_temp('''
        def f(x, y):
            return x-y
        def g(x, y):
            return f(y, x)
    ''')
    g = app.interphook('g')
    w_res = g(space, space.wrap(10), space.wrap(1))
    assert space.eq_w(w_res, space.wrap(-9))

def test_applevel_class(space, applevel_temp = applevel_temp):
    app = applevel_temp('''
        class C(object):
            clsattr = 42
            def __init__(self, x=13):
                self.attr = x
    ''')
    C = app.interphook('C')
    c = C(space, space.wrap(17))
    w_attr = space.getattr(c, space.wrap('clsattr'))
    assert space.eq_w(w_attr, space.wrap(42))
    w_clsattr = space.getattr(c, space.wrap('attr'))
    assert space.eq_w(w_clsattr, space.wrap(17))

class AppTestMethods:
    def test_some_app_test_method(self):
        assert 2 == 2

class TestMixedModule:
    def test_accesses(self):
        space = self.space
        from .demomixedmod.moduledef import Module
        w_module = Module(space, space.wrap('mixedmodule'))
        space.appexec([w_module], """
            (module):
                assert module.value is None
                assert module.__doc__ == 'mixedmodule doc'

                assert module.somefunc is module.somefunc
                result = module.somefunc()
                assert result == True

                assert module.someappfunc is module.someappfunc
                appresult = module.someappfunc(41)
                assert appresult == 42

                assert module.__dict__ is module.__dict__
                for name in ('somefunc', 'someappfunc', '__doc__', '__name__'):
                    assert name in module.__dict__
        """)
        assert space.is_true(w_module.call('somefunc'))
        assert Module.get_applevel_name() == 'demomixedmod'

    def test_whacking_at_loaders(self):
        """Some MixedModules change 'self.loaders' in __init__(), but doing
        so they incorrectly mutated a class attribute.  'loaders' is now a
        per-instance attribute, holding a fresh copy of the dictionary.
        """
        from pypy.interpreter.mixedmodule import MixedModule
        from pypy.tool.pytest.objspace import maketestobjspace

        class MyModule(MixedModule):
            interpleveldefs = {}
            appleveldefs = {}
            def __init__(self, space, w_name):
                def loader(myspace):
                    assert myspace is space
                    return myspace.wrap("hello")
                MixedModule.__init__(self, space, w_name)
                self.loaders["hi"] = loader

        space1 = self.space
        w_mymod1 = MyModule(space1, space1.wrap('mymod'))

        space2 = maketestobjspace()
        w_mymod2 = MyModule(space2, space2.wrap('mymod'))

        w_str = space1.getattr(w_mymod1, space1.wrap("hi"))
        assert space1.text_w(w_str) == "hello"