Debian Codegraph @

pypy/module/__pypy__/test/test_magic.py

raw symbols SCIP
1 # encoding: utf-8
2 import pytest
3 import sys
4
5 class AppTestMagic:
6 spaceconfig = dict(usemodules=['__pypy__'])
7
8 def setup_class(cls):
9 cls.w_file = cls.space.wrap(__file__)
10
11 def test_new_code_hook(self):
12 # workaround for running on top of old CPython 2.7 versions
13 def exec_(code, d):
14 exec(code, d)
15
16 l = []
17
18 def callable(code):
19 l.append(code)
20
21 import __pypy__
22 __pypy__.set_code_callback(callable)
23 d = {}
24 try:
25 exec_("""
26 def f():
27 pass
28 """, d)
29 finally:
30 __pypy__.set_code_callback(None)
31 assert d['f'].__code__ in l
32
33 def test_decode_long(self):
34 from __pypy__ import decode_long
35 assert decode_long(b'') == 0
36 assert decode_long(b'\xff\x00') == 255
37 assert decode_long(b'\xff\x7f') == 32767
38 assert decode_long(b'\x00\xff') == -256
39 assert decode_long(b'\x00\x80') == -32768
40 assert decode_long(b'\x80') == -128
41 assert decode_long(b'\x7f') == 127
42 assert decode_long(b'\x55' * 97) == (1 << (97 * 8)) // 3
43 assert decode_long(b'\x00\x80', 'big') == 128
44 assert decode_long(b'\xff\x7f', 'little', False) == 32767
45 assert decode_long(b'\x00\x80', 'little', False) == 32768
46 assert decode_long(b'\x00\x80', 'little', True) == -32768
47 raises(ValueError, decode_long, b'', 'foo')
48
49 def test_promote(self):
50 from __pypy__ import _promote
51 assert _promote(1) == 1
52 assert _promote(1.1) == 1.1
53 assert _promote(b"abc") == b"abc"
54 raises(TypeError, _promote, u"abc")
55 l = []
56 assert _promote(l) is l
57 class A(object):
58 pass
59 a = A()
60 assert _promote(a) is a
61
62 def test_set_exc_info(self):
63 from __pypy__ import set_exc_info
64 terr = TypeError("hello world")
65 set_exc_info(TypeError, terr)
66 try:
67 raise ValueError
68 except ValueError as e:
69 assert e.__context__ is terr
70
71 def test_set_exc_info_issue3096(self):
72 from __pypy__ import set_exc_info
73 def recover():
74 set_exc_info(None, None)
75 def main():
76 try:
77 raise RuntimeError('aaa')
78 finally:
79 recover()
80 raise RuntimeError('bbb')
81 try:
82 main()
83 except RuntimeError as e:
84 assert e.__cause__ is None
85 assert e.__context__ is None
86
87 def test_set_exc_info_traceback(self):
88 import sys
89 from __pypy__ import set_exc_info
90 def f():
91 1 // 0
92 def g():
93 try:
94 f()
95 except ZeroDivisionError:
96 return sys.exc_info()[2]
97 tb = g()
98 terr = TypeError("hello world")
99 terr.__traceback__ = tb
100 set_exc_info(TypeError, terr, tb)
101 assert sys.exc_info()[2] is tb
102
103 def test_utf8_content(self):
104 from __pypy__ import utf8content
105 assert utf8content(u"a") == b"a"
106 assert utf8content(u"\xe4") == b'\xc3\xa4'
107
108 @pytest.mark.skipif(sys.platform != 'win32', reason="win32 only")
109 def test_get_osfhandle(self):
110 from __pypy__ import get_osfhandle
111 with open(self.file) as fid:
112 f = get_osfhandle(fid.fileno())
113 raises(OSError, get_osfhandle, 2**30)
114
115 def test_get_set_contextvar_context(self):
116 from __pypy__ import get_contextvar_context, set_contextvar_context
117 context = get_contextvar_context()
118 try:
119 set_contextvar_context(1)
120 assert get_contextvar_context() == 1
121 set_contextvar_context(5)
122 assert get_contextvar_context() == 5
123
124 finally:
125 set_contextvar_context(context)
126
127 def test_list_get_physical_size(self):
128 from __pypy__ import list_get_physical_size
129 l = [1, 2]
130 l.append(3)
131 assert list_get_physical_size(l) >= 3 # should be 6, but untranslated 3