File: test_special.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, 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 (200 lines) | stat: -rw-r--r-- 5,997 bytes parent folder | download | duplicates (2)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import pytest
from rpython.tool.udir import udir

class AppTest(object):
    spaceconfig = {"objspace.usemodules.select": False}

    def setup_class(cls):
        if cls.runappdirect:
            pytest.skip("does not make sense on pypy-c")

    def test_cpumodel(self):
        import __pypy__
        assert hasattr(__pypy__, 'cpumodel')

    def test_builtinify(self):
        import __pypy__
        class A(object):
            a = lambda *args: args
            b = __pypy__.builtinify(a)
        my = A()
        assert my.a() == (my,)
        assert my.b() == ()
        assert A.a(my) == (my,)
        assert A.b(my) == (my,)
        assert not hasattr(A.a, 'im_func')
        assert not hasattr(A.b, 'im_func')
        assert A.a is A.__dict__['a']
        assert A.b is A.__dict__['b']

    def test_hidden_applevel(self):
        import __pypy__
        import sys

        @__pypy__.hidden_applevel
        def sneak(): (lambda: 1/0)()
        try:
            sneak()
        except ZeroDivisionError as e:
            tb = e.__traceback__
            assert tb.tb_frame == sys._getframe()
            assert tb.tb_next.tb_frame.f_code.co_name == '<lambda>'
        else:
            assert False, 'Expected ZeroDivisionError'

    def test_hidden_applevel_frames(self):
        import __pypy__
        import sys

        @__pypy__.hidden_applevel
        def test_hidden():
            assert sys._getframe().f_code.co_name != 'test_hidden'
            def e(): 1/0
            try: e()
            except ZeroDivisionError as e:
                assert sys.exc_info() == (None, None, None)
                frame = e.__traceback__.tb_frame
                assert frame != sys._getframe()
                assert frame.f_code.co_name == 'e'
            else: assert False
            return 2
        assert test_hidden() == 2

    def test_lookup_special(self):
        from __pypy__ import lookup_special
        class X(object):
            def foo(self): return 42
        x = X()
        x.foo = 23
        x.bar = 80
        assert lookup_special(x, "foo")() == 42
        assert lookup_special(x, "bar") is None

    def test_do_what_I_mean(self):
        from __pypy__ import do_what_I_mean
        x = do_what_I_mean()
        assert x == 42

    def test_list_strategy(self):
        from __pypy__ import strategy

        l = [1, 2, 3]
        assert strategy(l) == "IntegerListStrategy"
        l = [b"a", b"b", b"c"]
        assert strategy(l) == "BytesListStrategy"
        l = [u"a", u"b", u"c"]
        assert strategy(l) == "AsciiListStrategy"
        l = [1.1, 2.2, 3.3]
        assert strategy(l) == "FloatListStrategy"
        l = [1, "b", 3]
        assert strategy(l) == "ObjectListStrategy"
        l = []
        assert strategy(l) == "EmptyListStrategy"
        o = 5
        raises(TypeError, strategy, 5)

    def test_dict_strategy(self):
        from __pypy__ import strategy

        d = {}
        assert strategy(d) == "EmptyDictStrategy"
        d = {1: None, 5: None}
        assert strategy(d) == "IntDictStrategy"

    def test_set_strategy(self):
        from __pypy__ import strategy

        s = set()
        assert strategy(s) == "EmptySetStrategy"
        s = set([2, 3, 4])
        assert strategy(s) == "IntegerSetStrategy"

    def test_normalize_exc(self):
        from __pypy__ import normalize_exc
        e = normalize_exc(TypeError)
        assert isinstance(e, TypeError)
        e = normalize_exc(TypeError, 'foo')
        assert isinstance(e, TypeError)
        assert str(e) == 'foo'
        e = normalize_exc(TypeError('doh'))
        assert isinstance(e, TypeError)
        assert str(e) == 'doh'

        try:
            1 / 0
        except ZeroDivisionError as e:
            tb = e.__traceback__
        e = normalize_exc(TypeError, None, tb)
        assert isinstance(e, TypeError)
        assert e.__traceback__ == tb

    def test_instance_strategy(self):
        import sys
        from __pypy__ import strategy
        if sys.maxsize < 2**32:
            skip('not for 32-bit python')
        class A(object):
            pass
        a = A()
        a.x = 1
        a.y = 2
        assert strategy(a).startswith("<UnboxedPlainAttribute y DICT 0 1 immutable <UnboxedPlainAttribute x DICT 0 0 immutable <DictTerminator w_cls=<W_TypeObject 'A'")


class AppTestJitFeatures(object):
    spaceconfig = {"translation.jit": True}

    def setup_class(cls):
        cls.w_runappdirect = cls.space.wrap(cls.runappdirect)

    def test_jit_backend_features(self):
        try:
            from __pypy__ import jit_backend_features
        except ImportError:
            skip("compiled without jit")
        supported_types = jit_backend_features
        assert isinstance(supported_types, list)
        for x in supported_types:
            assert x in ['floats', 'singlefloats', 'longlong']

    def test_internal_error(self):
        if not self.runappdirect:
            skip("we don't wrap a random exception inside SystemError "
                 "when untranslated, because it makes testing harder")
        from __pypy__ import _internal_crash
        raises(SystemError, _internal_crash, 1)


class AppTestBuiltinify(object):
    def setup_class(cls):
        if cls.runappdirect:
            pytest.skip("can only run untranslated")
        cls.w_path = cls.space.appexec(
            [cls.space.wrap(str(tmpdir))],
        """(path):
            import sys
            sys.path.append(path)
            return path
        """)

    def test_builtinify___self__(self):
        import os
        source = """
import __pypy__

@__pypy__.builtinify
def f():
    pass

def g():
    pass
g.__module__ = 'non existant'
g = __pypy__.builtinify(g)
        """
        with open(os.path.join(self.path, "dummy.py"), "w", encoding="utf-8") as f:
            f.write(source)
        import dummy
        assert dummy.f.__self__ is dummy
        assert dummy.g.__self__ is None

tmpdir = udir.join('test_special').ensure(dir=1)