| 1 | import pytest |
| 2 | from rpython.tool.udir import udir |
| 3 | |
| 4 | class AppTest(object): |
| 5 | spaceconfig = {"objspace.usemodules.select": False} |
| 6 | |
| 7 | def setup_class(cls): |
| 8 | if cls.runappdirect: |
| 9 | pytest.skip("does not make sense on pypy-c") |
| 10 | |
| 11 | def test_cpumodel(self): |
| 12 | import __pypy__ |
| 13 | assert hasattr(__pypy__, 'cpumodel') |
| 14 | |
| 15 | def test_builtinify(self): |
| 16 | import __pypy__ |
| 17 | class A(object): |
| 18 | a = lambda *args: args |
| 19 | b = __pypy__.builtinify(a) |
| 20 | my = A() |
| 21 | assert my.a() == (my,) |
| 22 | assert my.b() == () |
| 23 | assert A.a(my) == (my,) |
| 24 | assert A.b(my) == (my,) |
| 25 | assert not hasattr(A.a, 'im_func') |
| 26 | assert not hasattr(A.b, 'im_func') |
| 27 | assert A.a is A.__dict__['a'] |
| 28 | assert A.b is A.__dict__['b'] |
| 29 | |
| 30 | def test_hidden_applevel(self): |
| 31 | import __pypy__ |
| 32 | import sys |
| 33 | |
| 34 | @__pypy__.hidden_applevel |
| 35 | def sneak(): (lambda: 1/0)() |
| 36 | try: |
| 37 | sneak() |
| 38 | except ZeroDivisionError as e: |
| 39 | tb = e.__traceback__ |
| 40 | assert tb.tb_frame == sys._getframe() |
| 41 | assert tb.tb_next.tb_frame.f_code.co_name == '<lambda>' |
| 42 | else: |
| 43 | assert False, 'Expected ZeroDivisionError' |
| 44 | |
| 45 | def test_hidden_applevel_frames(self): |
| 46 | import __pypy__ |
| 47 | import sys |
| 48 | |
| 49 | @__pypy__.hidden_applevel |
| 50 | def test_hidden(): |
| 51 | assert sys._getframe().f_code.co_name != 'test_hidden' |
| 52 | def e(): 1/0 |
| 53 | try: e() |
| 54 | except ZeroDivisionError as e: |
| 55 | assert sys.exc_info() == (None, None, None) |
| 56 | frame = e.__traceback__.tb_frame |
| 57 | assert frame != sys._getframe() |
| 58 | assert frame.f_code.co_name == 'e' |
| 59 | else: assert False |
| 60 | return 2 |
| 61 | assert test_hidden() == 2 |
| 62 | |
| 63 | def test_lookup_special(self): |
| 64 | from __pypy__ import lookup_special |
| 65 | class X(object): |
| 66 | def foo(self): return 42 |
| 67 | x = X() |
| 68 | x.foo = 23 |
| 69 | x.bar = 80 |
| 70 | assert lookup_special(x, "foo")() == 42 |
| 71 | assert lookup_special(x, "bar") is None |
| 72 | |
| 73 | def test_do_what_I_mean(self): |
| 74 | from __pypy__ import do_what_I_mean |
| 75 | x = do_what_I_mean() |
| 76 | assert x == 42 |
| 77 | |
| 78 | def test_list_strategy(self): |
| 79 | from __pypy__ import strategy |
| 80 | |
| 81 | l = [1, 2, 3] |
| 82 | assert strategy(l) == "IntegerListStrategy" |
| 83 | l = [b"a", b"b", b"c"] |
| 84 | assert strategy(l) == "BytesListStrategy" |
| 85 | l = [u"a", u"b", u"c"] |
| 86 | assert strategy(l) == "AsciiListStrategy" |
| 87 | l = [1.1, 2.2, 3.3] |
| 88 | assert strategy(l) == "FloatListStrategy" |
| 89 | l = [1, "b", 3] |
| 90 | assert strategy(l) == "ObjectListStrategy" |
| 91 | l = [] |
| 92 | assert strategy(l) == "EmptyListStrategy" |
| 93 | o = 5 |
| 94 | raises(TypeError, strategy, 5) |
| 95 | |
| 96 | def test_dict_strategy(self): |
| 97 | from __pypy__ import strategy |
| 98 | |
| 99 | d = {} |
| 100 | assert strategy(d) == "EmptyDictStrategy" |
| 101 | d = {1: None, 5: None} |
| 102 | assert strategy(d) == "IntDictStrategy" |
| 103 | |
| 104 | def test_set_strategy(self): |
| 105 | from __pypy__ import strategy |
| 106 | |
| 107 | s = set() |
| 108 | assert strategy(s) == "EmptySetStrategy" |
| 109 | s = set([2, 3, 4]) |
| 110 | assert strategy(s) == "IntegerSetStrategy" |
| 111 | |
| 112 | def test_normalize_exc(self): |
| 113 | from __pypy__ import normalize_exc |
| 114 | e = normalize_exc(TypeError) |
| 115 | assert isinstance(e, TypeError) |
| 116 | e = normalize_exc(TypeError, 'foo') |
| 117 | assert isinstance(e, TypeError) |
| 118 | assert str(e) == 'foo' |
| 119 | e = normalize_exc(TypeError('doh')) |
| 120 | assert isinstance(e, TypeError) |
| 121 | assert str(e) == 'doh' |
| 122 | |
| 123 | try: |
| 124 | 1 / 0 |
| 125 | except ZeroDivisionError as e: |
| 126 | tb = e.__traceback__ |
| 127 | e = normalize_exc(TypeError, None, tb) |
| 128 | assert isinstance(e, TypeError) |
| 129 | assert e.__traceback__ == tb |
| 130 | |
| 131 | def test_instance_strategy(self): |
| 132 | import sys |
| 133 | from __pypy__ import strategy |
| 134 | if sys.maxsize < 2**32: |
| 135 | skip('not for 32-bit python') |
| 136 | class A(object): |
| 137 | pass |
| 138 | a = A() |
| 139 | a.x = 1 |
| 140 | a.y = 2 |
| 141 | assert strategy(a).startswith("<UnboxedPlainAttribute y DICT 0 1 immutable <UnboxedPlainAttribute x DICT 0 0 immutable <DictTerminator w_cls=<W_TypeObject 'A'") |
| 142 | |
| 143 | |
| 144 | class AppTestJitFeatures(object): |
| 145 | spaceconfig = {"translation.jit": True} |
| 146 | |
| 147 | def setup_class(cls): |
| 148 | cls.w_runappdirect = cls.space.wrap(cls.runappdirect) |
| 149 | |
| 150 | def test_jit_backend_features(self): |
| 151 | try: |
| 152 | from __pypy__ import jit_backend_features |
| 153 | except ImportError: |
| 154 | skip("compiled without jit") |
| 155 | supported_types = jit_backend_features |
| 156 | assert isinstance(supported_types, list) |
| 157 | for x in supported_types: |
| 158 | assert x in ['floats', 'singlefloats', 'longlong'] |
| 159 | |
| 160 | def test_internal_error(self): |
| 161 | if not self.runappdirect: |
| 162 | skip("we don't wrap a random exception inside SystemError " |
| 163 | "when untranslated, because it makes testing harder") |
| 164 | from __pypy__ import _internal_crash |
| 165 | raises(SystemError, _internal_crash, 1) |
| 166 | |
| 167 | |
| 168 | class AppTestBuiltinify(object): |
| 169 | def setup_class(cls): |
| 170 | if cls.runappdirect: |
| 171 | pytest.skip("can only run untranslated") |
| 172 | cls.w_path = cls.space.appexec( |
| 173 | [cls.space.wrap(str(tmpdir))], |
| 174 | """(path): |
| 175 | import sys |
| 176 | sys.path.append(path) |
| 177 | return path |
| 178 | """) |
| 179 | |
| 180 | def test_builtinify___self__(self): |
| 181 | import os |
| 182 | source = """ |
| 183 | import __pypy__ |
| 184 | |
| 185 | @__pypy__.builtinify |
| 186 | def f(): |
| 187 | pass |
| 188 | |
| 189 | def g(): |
| 190 | pass |
| 191 | g.__module__ = 'non existant' |
| 192 | g = __pypy__.builtinify(g) |
| 193 | """ |
| 194 | with open(os.path.join(self.path, "dummy.py"), "w", encoding="utf-8") as f: |
| 195 | f.write(source) |
| 196 | import dummy |
| 197 | assert dummy.f.__self__ is dummy |
| 198 | assert dummy.g.__self__ is None |
| 199 | |
| 200 | tmpdir = udir.join('test_special').ensure(dir=1) |