Debian Codegraph @

pypy/module/_cffi_backend/test/test_fastpath.py

raw symbols SCIP
1 # side-effect: FORMAT_LONGDOUBLE must be built before the first test
2 from pypy.module._cffi_backend import misc
3 from pypy.module._cffi_backend.ctypeobj import W_CType
4
5
6 class AppTest_fast_path_from_list(object):
7 spaceconfig = dict(usemodules=('_cffi_backend',))
8
9 def setup_method(self, meth):
10 def forbidden(*args):
11 assert False, 'The slow path is forbidden'
12 self._original = W_CType.pack_list_of_items.im_func
13 W_CType.pack_list_of_items = forbidden
14
15 def teardown_method(self, meth):
16 W_CType.pack_list_of_items = self._original
17
18 def test_fast_init_from_list(self):
19 import _cffi_backend
20 LONG = _cffi_backend.new_primitive_type('long')
21 P_LONG = _cffi_backend.new_pointer_type(LONG)
22 LONG_ARRAY = _cffi_backend.new_array_type(P_LONG, None)
23 buf = _cffi_backend.newp(LONG_ARRAY, [1, 2, 3])
24 assert buf[0] == 1
25 assert buf[1] == 2
26 assert buf[2] == 3
27
28 def test_fast_init_from_list_float(self):
29 import _cffi_backend
30 DOUBLE = _cffi_backend.new_primitive_type('double')
31 P_DOUBLE = _cffi_backend.new_pointer_type(DOUBLE)
32 DOUBLE_ARRAY = _cffi_backend.new_array_type(P_DOUBLE, None)
33 buf = _cffi_backend.newp(DOUBLE_ARRAY, [1.1, 2.2, 3.3])
34 assert buf[0] == 1.1
35 assert buf[1] == 2.2
36 assert buf[2] == 3.3
37
38 def test_fast_init_short_from_list(self):
39 import _cffi_backend
40 SHORT = _cffi_backend.new_primitive_type('short')
41 P_SHORT = _cffi_backend.new_pointer_type(SHORT)
42 SHORT_ARRAY = _cffi_backend.new_array_type(P_SHORT, None)
43 buf = _cffi_backend.newp(SHORT_ARRAY, [1, -2, 3])
44 assert buf[0] == 1
45 assert buf[1] == -2
46 assert buf[2] == 3
47 raises(OverflowError, _cffi_backend.newp, SHORT_ARRAY, [40000])
48 raises(OverflowError, _cffi_backend.newp, SHORT_ARRAY, [-40000])
49
50 def test_fast_init_longlong_from_list(self):
51 import _cffi_backend
52 import sys
53 large_int = 2 ** (50 if sys.maxsize > 2**31 - 1 else 30)
54 LONGLONG = _cffi_backend.new_primitive_type('long long')
55 P_LONGLONG = _cffi_backend.new_pointer_type(LONGLONG)
56 LONGLONG_ARRAY = _cffi_backend.new_array_type(P_LONGLONG, None)
57 buf = _cffi_backend.newp(LONGLONG_ARRAY, [1, -2, 3, large_int])
58 assert buf[0] == 1
59 assert buf[1] == -2
60 assert buf[2] == 3
61 assert buf[3] == large_int
62
63 def test_fast_init_ushort_from_list(self):
64 import _cffi_backend
65 USHORT = _cffi_backend.new_primitive_type('unsigned short')
66 P_USHORT = _cffi_backend.new_pointer_type(USHORT)
67 USHORT_ARRAY = _cffi_backend.new_array_type(P_USHORT, None)
68 buf = _cffi_backend.newp(USHORT_ARRAY, [1, 2, 40000])
69 assert buf[0] == 1
70 assert buf[1] == 2
71 assert buf[2] == 40000
72 raises(OverflowError, _cffi_backend.newp, USHORT_ARRAY, [70000])
73 raises(OverflowError, _cffi_backend.newp, USHORT_ARRAY, [-1])
74
75 def test_fast_init_ulong_from_list(self):
76 import sys
77 import _cffi_backend
78 maxlong = sys.maxsize
79 if sys.platform == 'win32':
80 # maxlong == 2**31-1 < sys.maxsize == 2**63-1 on win64!
81 maxlong = int(2**31-1)
82 ULONG = _cffi_backend.new_primitive_type('unsigned long')
83 P_ULONG = _cffi_backend.new_pointer_type(ULONG)
84 ULONG_ARRAY = _cffi_backend.new_array_type(P_ULONG, None)
85 buf = _cffi_backend.newp(ULONG_ARRAY, [1, 2, maxlong])
86 assert buf[0] == 1
87 assert buf[1] == 2
88 assert buf[2] == maxlong
89 raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-1])
90 raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-maxlong])
91
92 def test_fast_init_cfloat_from_list(self):
93 import _cffi_backend
94 FLOAT = _cffi_backend.new_primitive_type('float')
95 P_FLOAT = _cffi_backend.new_pointer_type(FLOAT)
96 FLOAT_ARRAY = _cffi_backend.new_array_type(P_FLOAT, None)
97 buf = _cffi_backend.newp(FLOAT_ARRAY, [1.25, -3.5])
98 assert buf[0] == 1.25
99 assert buf[1] == -3.5
100
101 def test_fast_init_clongdouble_from_list(self):
102 import _cffi_backend
103 LONGDOUBLE = _cffi_backend.new_primitive_type('long double')
104 P_LONGDOUBLE = _cffi_backend.new_pointer_type(LONGDOUBLE)
105 LONGDOUBLE_ARRAY = _cffi_backend.new_array_type(P_LONGDOUBLE, None)
106 buf = _cffi_backend.newp(LONGDOUBLE_ARRAY, [1.25, -3.5])
107 assert float(buf[0]) == 1.25
108 assert float(buf[1]) == -3.5
109
110 def test_fast_init_bool_from_list(self):
111 import _cffi_backend
112 BOOL = _cffi_backend.new_primitive_type('_Bool')
113 P_BOOL = _cffi_backend.new_pointer_type(BOOL)
114 BOOL_ARRAY = _cffi_backend.new_array_type(P_BOOL, None)
115 buf = _cffi_backend.newp(BOOL_ARRAY, [1, 0])
116 assert buf[0] is True
117 assert buf[1] is False
118 raises(OverflowError, _cffi_backend.newp, BOOL_ARRAY, [2])
119 raises(OverflowError, _cffi_backend.newp, BOOL_ARRAY, [-1])
120
121
122 class AppTest_fast_path_bug(object):
123 spaceconfig = dict(usemodules=('_cffi_backend',))
124
125 def test_bug_not_list_or_tuple(self):
126 import _cffi_backend
127 LONG = _cffi_backend.new_primitive_type('long')
128 P_LONG = _cffi_backend.new_pointer_type(LONG)
129 LONG_ARRAY_2 = _cffi_backend.new_array_type(P_LONG, 2)
130 P_LONG_ARRAY_2 = _cffi_backend.new_pointer_type(LONG_ARRAY_2)
131 LONG_ARRAY_ARRAY = _cffi_backend.new_array_type(P_LONG_ARRAY_2, None)
132 raises(TypeError, _cffi_backend.newp, LONG_ARRAY_ARRAY, [set([4, 5])])
133
134
135 class AppTest_fast_path_to_list(object):
136 spaceconfig = dict(usemodules=('_cffi_backend',))
137
138 def setup_method(self, meth):
139 from pypy.interpreter import gateway
140 from rpython.rlib import rrawarray
141 #
142 self.count = 0
143 def get_count(*args):
144 return self.space.wrap(self.count)
145 self.w_get_count = self.space.wrap(gateway.interp2app(get_count))
146 #
147 original = rrawarray.populate_list_from_raw_array
148 def populate_list_from_raw_array(*args):
149 self.count += 1
150 return original(*args)
151 self._original = original
152 rrawarray.populate_list_from_raw_array = populate_list_from_raw_array
153 #
154 original2 = misc.unpack_list_from_raw_array
155 def unpack_list_from_raw_array(*args):
156 self.count += 1
157 return original2(*args)
158 self._original2 = original2
159 misc.unpack_list_from_raw_array = unpack_list_from_raw_array
160 #
161 original3 = misc.unpack_cfloat_list_from_raw_array
162 def unpack_cfloat_list_from_raw_array(*args):
163 self.count += 1
164 return original3(*args)
165 self._original3 = original3
166 misc.unpack_cfloat_list_from_raw_array = (
167 unpack_cfloat_list_from_raw_array)
168 #
169 original4 = misc.unpack_unsigned_list_from_raw_array
170 def unpack_unsigned_list_from_raw_array(*args):
171 self.count += 1
172 return original4(*args)
173 self._original4 = original4
174 misc.unpack_unsigned_list_from_raw_array = (
175 unpack_unsigned_list_from_raw_array)
176 #
177 self.w_runappdirect = self.space.wrap(self.runappdirect)
178
179
180 def teardown_method(self, meth):
181 from rpython.rlib import rrawarray
182 rrawarray.populate_list_from_raw_array = self._original
183 misc.unpack_list_from_raw_array = self._original2
184 misc.unpack_cfloat_list_from_raw_array = self._original3
185 misc.unpack_unsigned_list_from_raw_array = self._original4
186
187 def test_list_int(self):
188 import _cffi_backend
189 LONG = _cffi_backend.new_primitive_type('long')
190 P_LONG = _cffi_backend.new_pointer_type(LONG)
191 LONG_ARRAY = _cffi_backend.new_array_type(P_LONG, 3)
192 buf = _cffi_backend.newp(LONG_ARRAY)
193 buf[0] = 1
194 buf[1] = 2
195 buf[2] = 3
196 lst = list(buf)
197 assert lst == [1, 2, 3]
198 if not self.runappdirect:
199 assert self.get_count() == 1
200
201 def test_TypeError_if_no_length(self):
202 import _cffi_backend
203 LONG = _cffi_backend.new_primitive_type('long')
204 P_LONG = _cffi_backend.new_pointer_type(LONG)
205 LONG_ARRAY = _cffi_backend.new_array_type(P_LONG, 3)
206 buf = _cffi_backend.newp(LONG_ARRAY)
207 pbuf = _cffi_backend.cast(P_LONG, buf)
208 raises(TypeError, "list(pbuf)")
209
210 def test_bug(self):
211 import _cffi_backend
212 LONG = _cffi_backend.new_primitive_type('long')
213 five = _cffi_backend.cast(LONG, 5)
214 raises(TypeError, list, five)
215 DOUBLE = _cffi_backend.new_primitive_type('double')
216 five_and_a_half = _cffi_backend.cast(DOUBLE, 5.5)
217 raises(TypeError, list, five_and_a_half)
218
219 def test_list_float(self):
220 import _cffi_backend
221 DOUBLE = _cffi_backend.new_primitive_type('double')
222 P_DOUBLE = _cffi_backend.new_pointer_type(DOUBLE)
223 DOUBLE_ARRAY = _cffi_backend.new_array_type(P_DOUBLE, 3)
224 buf = _cffi_backend.newp(DOUBLE_ARRAY)
225 buf[0] = 1.1
226 buf[1] = 2.2
227 buf[2] = 3.3
228 lst = list(buf)
229 assert lst == [1.1, 2.2, 3.3]
230 if not self.runappdirect:
231 assert self.get_count() == 1
232
233 def test_list_short(self):
234 import _cffi_backend
235 SHORT = _cffi_backend.new_primitive_type('short')
236 P_SHORT = _cffi_backend.new_pointer_type(SHORT)
237 SHORT_ARRAY = _cffi_backend.new_array_type(P_SHORT, 3)
238 buf = _cffi_backend.newp(SHORT_ARRAY)
239 buf[0] = 1
240 buf[1] = 2
241 buf[2] = 3
242 lst = list(buf)
243 assert lst == [1, 2, 3]
244 if not self.runappdirect:
245 assert self.get_count() == 1
246
247 def test_list_ushort(self):
248 import _cffi_backend
249 USHORT = _cffi_backend.new_primitive_type('unsigned short')
250 P_USHORT = _cffi_backend.new_pointer_type(USHORT)
251 USHORT_ARRAY = _cffi_backend.new_array_type(P_USHORT, 3)
252 buf = _cffi_backend.newp(USHORT_ARRAY)
253 buf[0] = 1
254 buf[1] = 2
255 buf[2] = 50505
256 lst = list(buf)
257 assert lst == [1, 2, 50505]
258 if not self.runappdirect:
259 assert self.get_count() == 1
260
261 def test_list_cfloat(self):
262 import _cffi_backend
263 FLOAT = _cffi_backend.new_primitive_type('float')
264 P_FLOAT = _cffi_backend.new_pointer_type(FLOAT)
265 FLOAT_ARRAY = _cffi_backend.new_array_type(P_FLOAT, 3)
266 buf = _cffi_backend.newp(FLOAT_ARRAY)
267 buf[0] = 1.25
268 buf[1] = -2.5
269 buf[2] = 3.75
270 lst = list(buf)
271 assert lst == [1.25, -2.5, 3.75]
272 if not self.runappdirect:
273 assert self.get_count() == 1
274
275 def test_too_many_initializers(self):
276 import _cffi_backend
277 ffi = _cffi_backend.FFI()
278 raises(IndexError, ffi.new, "int[4]", [10, 20, 30, 40, 50])
279 raises(IndexError, ffi.new, "int[4]", tuple(range(999)))
280 raises(IndexError, ffi.new, "unsigned int[4]", [10, 20, 30, 40, 50])
281 raises(IndexError, ffi.new, "float[4]", [10, 20, 30, 40, 50])
282 raises(IndexError, ffi.new, "long double[4]", [10, 20, 30, 40, 50])
283 raises(IndexError, ffi.new, "char[4]", [10, 20, 30, 40, 50])
284 raises(IndexError, ffi.new, "wchar_t[4]", [10, 20, 30, 40, 50])
285 raises(IndexError, ffi.new, "_Bool[4]", [10, 20, 30, 40, 50])
286 raises(IndexError, ffi.new, "int[4][4]", [[3,4,5,6]] * 5)
287 raises(IndexError, ffi.new, "int[4][4]", [[3,4,5,6,7]] * 4)