Debian Codegraph @

pypy/module/_cffi_backend/test/test_ffi_obj.py

raw symbols SCIP
1 from pypy.module._cffi_backend import newtype
2 from pypy.module._cffi_backend.newtype import _clean_cache
3
4
5 class TestFFIObj:
6 spaceconfig = dict(usemodules=('_cffi_backend', 'array'))
7
8 def teardown_method(self, meth):
9 _clean_cache(self.space)
10
11 def test_new_function_type_during_translation(self):
12 space = self.space
13 BInt = newtype.new_primitive_type(space, "int")
14 BFunc = newtype.new_function_type(space, space.wrap([BInt]), BInt)
15 assert BFunc is newtype.new_function_type(space,space.wrap([BInt]),BInt)
16 unique_cache = space.fromcache(newtype.UniqueCache)
17 unique_cache._cleanup_()
18 assert BFunc is newtype.new_function_type(space,space.wrap([BInt]),BInt)
19
20
21 class AppTestFFIObj:
22 spaceconfig = dict(usemodules=('_cffi_backend', 'array'))
23
24 def teardown_method(self, meth):
25 _clean_cache(self.space)
26
27 def test_ffi_new(self):
28 import _cffi_backend as _cffi1_backend
29 ffi = _cffi1_backend.FFI()
30 p = ffi.new("int *")
31 p[0] = -42
32 assert p[0] == -42
33 assert type(ffi) is ffi.__class__ is _cffi1_backend.FFI
34
35 def test_ffi_subclass(self):
36 import _cffi_backend as _cffi1_backend
37 class FOO(_cffi1_backend.FFI):
38 def __init__(self, x):
39 self.x = x
40 foo = FOO(42)
41 assert foo.x == 42
42 p = foo.new("int *")
43 assert p[0] == 0
44 assert type(foo) is foo.__class__ is FOO
45
46 def test_ffi_no_argument(self):
47 import _cffi_backend as _cffi1_backend
48 raises(TypeError, _cffi1_backend.FFI, 42)
49
50 def test_ffi_cache_type(self):
51 import _cffi_backend as _cffi1_backend
52 ffi = _cffi1_backend.FFI()
53 t1 = ffi.typeof("int **")
54 t2 = ffi.typeof("int *")
55 assert t2.item is t1.item.item
56 assert t2 is t1.item
57 assert ffi.typeof("int[][10]") is ffi.typeof("int[][10]")
58 assert ffi.typeof("int(*)()") is ffi.typeof("int(*)()")
59
60 def test_ffi_cache_type_globally(self):
61 import _cffi_backend as _cffi1_backend
62 ffi1 = _cffi1_backend.FFI()
63 ffi2 = _cffi1_backend.FFI()
64 t1 = ffi1.typeof("int *")
65 t2 = ffi2.typeof("int *")
66 assert t1 is t2
67
68 def test_ffi_invalid(self):
69 import _cffi_backend as _cffi1_backend
70 ffi = _cffi1_backend.FFI()
71 # array of 10 times an "int[]" is invalid
72 raises(ValueError, ffi.typeof, "int[10][]")
73
74 def test_ffi_docstrings(self):
75 import _cffi_backend as _cffi1_backend
76 # check that all methods of the FFI class have a docstring.
77 check_type = type(_cffi1_backend.FFI.new)
78 for methname in dir(_cffi1_backend.FFI):
79 if not methname.startswith('_'):
80 method = getattr(_cffi1_backend.FFI, methname)
81 if isinstance(method, check_type):
82 assert method.__doc__, "method FFI.%s() has no docstring" % (
83 methname,)
84
85 def test_ffi_NULL(self):
86 import _cffi_backend as _cffi1_backend
87 NULL = _cffi1_backend.FFI.NULL
88 assert _cffi1_backend.FFI().typeof(NULL).cname == "void *"
89
90 def test_ffi_no_attr(self):
91 import _cffi_backend as _cffi1_backend
92 ffi = _cffi1_backend.FFI()
93 raises(AttributeError, "ffi.no_such_name")
94 raises(AttributeError, "ffi.no_such_name = 42")
95 raises(AttributeError, "del ffi.no_such_name")
96
97 def test_ffi_string(self):
98 import _cffi_backend as _cffi1_backend
99 ffi = _cffi1_backend.FFI()
100 p = ffi.new("char[]", init=b"foobar\x00baz")
101 assert ffi.string(p) == b"foobar"
102
103 def test_ffi_errno(self):
104 import _cffi_backend as _cffi1_backend
105 # xxx not really checking errno, just checking that we can read/write it
106 ffi = _cffi1_backend.FFI()
107 ffi.errno = 42
108 assert ffi.errno == 42
109
110 def test_ffi_alignof(self):
111 import _cffi_backend as _cffi1_backend
112 ffi = _cffi1_backend.FFI()
113 assert ffi.alignof("int") == 4
114 assert ffi.alignof("int[]") == 4
115 assert ffi.alignof("int[41]") == 4
116 assert ffi.alignof("short[41]") == 2
117 assert ffi.alignof(ffi.new("int[41]")) == 4
118 assert ffi.alignof(ffi.new("int[]", 41)) == 4
119
120 def test_ffi_sizeof(self):
121 import _cffi_backend as _cffi1_backend
122 ffi = _cffi1_backend.FFI()
123 assert ffi.sizeof("int") == 4
124 raises(ffi.error, ffi.sizeof, "int[]")
125 assert ffi.sizeof("int[41]") == 41 * 4
126 assert ffi.sizeof(ffi.new("int[41]")) == 41 * 4
127 assert ffi.sizeof(ffi.new("int[]", 41)) == 41 * 4
128
129 def test_ffi_callback(self):
130 import _cffi_backend as _cffi1_backend
131 ffi = _cffi1_backend.FFI()
132 assert ffi.callback("int(int)", lambda x: x + 42)(10) == 52
133 assert ffi.callback("int(*)(int)", lambda x: x + 42)(10) == 52
134 assert ffi.callback("int(int)", lambda x: x + "", -66)(10) == -66
135 assert ffi.callback("int(int)", lambda x: x + "", error=-66)(10) == -66
136
137 def test_ffi_callback_onerror(self):
138 import _cffi_backend as _cffi1_backend
139 ffi = _cffi1_backend.FFI()
140 seen = []
141 def myerror(exc, val, tb):
142 seen.append(exc)
143 cb = ffi.callback("int(int)", lambda x: x + "", onerror=myerror)
144 assert cb(10) == 0
145 cb = ffi.callback("int(int)", lambda x:int(1E100), -66, onerror=myerror)
146 assert cb(10) == -66
147 assert seen == [TypeError, OverflowError]
148
149 def test_ffi_callback_decorator(self):
150 import _cffi_backend as _cffi1_backend
151 ffi = _cffi1_backend.FFI()
152 assert ffi.callback(ffi.typeof("int(*)(int)"))(lambda x: x + 42)(10) == 52
153 deco = ffi.callback("int(int)", error=-66)
154 assert deco(lambda x: x + "")(10) == -66
155 assert deco(lambda x: x + 42)(10) == 52
156
157 def test_ffi_callback_onerror(self):
158 import _cffi_backend as _cffi1_backend
159 ffi = _cffi1_backend.FFI()
160 seen = []
161 def oops(*args):
162 seen.append(args)
163
164 @ffi.callback("int(int)", onerror=oops)
165 def fn1(x):
166 return x + ""
167 assert fn1(10) == 0
168
169 @ffi.callback("int(int)", onerror=oops, error=-66)
170 def fn2(x):
171 return x + ""
172 assert fn2(10) == -66
173
174 assert len(seen) == 2
175 exc, val, tb = seen[0]
176 assert exc is TypeError
177 assert isinstance(val, TypeError)
178 assert tb.tb_frame.f_code.co_name == "fn1"
179 exc, val, tb = seen[1]
180 assert exc is TypeError
181 assert isinstance(val, TypeError)
182 assert tb.tb_frame.f_code.co_name == "fn2"
183 del seen[:]
184 #
185 raises(TypeError, ffi.callback, "int(int)",
186 lambda x: x, onerror=42) # <- not callable
187
188 def test_ffi_getctype(self):
189 import _cffi_backend as _cffi1_backend
190 ffi = _cffi1_backend.FFI()
191 assert ffi.getctype("int") == "int"
192 assert ffi.getctype("int", 'x') == "int x"
193 assert ffi.getctype("int*") == "int *"
194 assert ffi.getctype("int*", '') == "int *"
195 assert ffi.getctype("int*", 'x') == "int * x"
196 assert ffi.getctype("int", '*') == "int *"
197 assert ffi.getctype("int", replace_with=' * x ') == "int * x"
198 assert ffi.getctype(ffi.typeof("int*"), '*') == "int * *"
199 assert ffi.getctype("int", '[5]') == "int[5]"
200 assert ffi.getctype("int[5]", '[6]') == "int[6][5]"
201 assert ffi.getctype("int[5]", '(*)') == "int(*)[5]"
202 # special-case for convenience: automatically put '()' around '*'
203 assert ffi.getctype("int[5]", '*') == "int(*)[5]"
204 assert ffi.getctype("int[5]", '*foo') == "int(*foo)[5]"
205 assert ffi.getctype("int[5]", ' ** foo ') == "int(** foo)[5]"
206
207 def test_addressof(self):
208 import _cffi_backend as _cffi1_backend
209 ffi = _cffi1_backend.FFI()
210 a = ffi.new("int[10]")
211 b = ffi.addressof(a, 5)
212 b[2] = -123
213 assert a[7] == -123
214
215 def test_handle(self):
216 import _cffi_backend as _cffi1_backend
217 ffi = _cffi1_backend.FFI()
218 x = [2, 4, 6]
219 xp = ffi.new_handle(x)
220 assert ffi.typeof(xp) == ffi.typeof("void *")
221 assert ffi.from_handle(xp) is x
222 yp = ffi.new_handle([6, 4, 2])
223 assert ffi.from_handle(yp) == [6, 4, 2]
224
225 def test_ffi_cast(self):
226 import _cffi_backend as _cffi1_backend
227 ffi = _cffi1_backend.FFI()
228 assert ffi.cast("int(*)(int)", 0) == ffi.NULL
229 ffi.callback("int(int)") # side-effect of registering this string
230 raises(ffi.error, ffi.cast, "int(int)", 0)
231
232 def test_ffi_invalid_type(self):
233 import _cffi_backend as _cffi1_backend
234 ffi = _cffi1_backend.FFI()
235 e = raises(ffi.error, ffi.cast, "", 0)
236 assert str(e.value) == ("identifier expected\n"
237 "\n"
238 "^")
239 e = raises(ffi.error, ffi.cast, "struct struct", 0)
240 assert str(e.value) == ("struct or union name expected\n"
241 "struct struct\n"
242 " ^")
243 e = raises(ffi.error, ffi.cast, "struct never_heard_of_s", 0)
244 assert str(e.value) == ("undefined struct/union name\n"
245 "struct never_heard_of_s\n"
246 " ^")
247 e = raises(ffi.error, ffi.cast, "\t\n\x01\x1f~\x7f\x80\xff", 0)
248 assert str(e.value) == ("identifier expected\n"
249 " ??~?????\n"
250 " ^")
251 e = raises(ffi.error, ffi.cast, "X" * 600, 0)
252 assert str(e.value) == ("undefined type name")
253
254 def test_ffi_buffer(self):
255 import _cffi_backend as _cffi1_backend
256 ffi = _cffi1_backend.FFI()
257 a = ffi.new("signed char[]", [5, 6, 7])
258 assert ffi.buffer(a)[:] == b'\x05\x06\x07'
259 assert ffi.buffer(cdata=a, size=2)[:] == b'\x05\x06'
260 assert type(ffi.buffer(a)) is ffi.buffer
261
262 def test_ffi_buffer_comparisons(self):
263 import _cffi_backend as _cffi1_backend
264 ffi = _cffi1_backend.FFI()
265 ba = bytearray(range(100, 110))
266 assert ba == memoryview(ba) # justification for the following
267 a = ffi.new("uint8_t[]", list(ba))
268 c = ffi.new("uint8_t[]", [99] + list(ba))
269 b_full = ffi.buffer(a)
270 b_short = ffi.buffer(a, 3)
271 b_mid = ffi.buffer(a, 6)
272 b_other = ffi.buffer(c, 6)
273 content = b_full[:]
274 assert content == b_full == ba
275 assert b_short < b_mid < b_full
276 assert b_other < b_short < b_mid < b_full
277 assert ba > b_mid > ba[0:2]
278 assert b_short != ba[1:4]
279 assert b_short != 42
280
281 def test_ffi_from_buffer(self):
282 import _cffi_backend as _cffi1_backend
283 import array
284 ffi = _cffi1_backend.FFI()
285 a = array.array('H', [10000, 20000, 30000, 40000])
286 c = ffi.from_buffer(a)
287 assert ffi.typeof(c) is ffi.typeof("char[]")
288 assert len(c) == 8
289 ffi.cast("unsigned short *", c)[1] += 500
290 assert list(a) == [10000, 20500, 30000, 40000]
291 raises(TypeError, ffi.from_buffer, a, True)
292 assert c == ffi.from_buffer("char[]", a, True)
293 assert c == ffi.from_buffer(a, require_writable=True)
294 #
295 c = ffi.from_buffer("unsigned short[]", a)
296 assert len(c) == 4
297 assert c[1] == 20500
298 #
299 c = ffi.from_buffer("unsigned short[2][2]", a)
300 assert len(c) == 2
301 assert len(c[0]) == 2
302 assert c[0][1] == 20500
303 #
304 p = ffi.from_buffer(b"abcd")
305 assert p[2] == b"c"
306 #
307 assert p == ffi.from_buffer(b"abcd", require_writable=False)
308 raises((TypeError, BufferError), ffi.from_buffer,
309 "char[]", b"abcd", True)
310 raises((TypeError, BufferError), ffi.from_buffer, b"abcd",
311 require_writable=True)
312
313 def test_from_buffer_BytesIO(self):
314 from _cffi_backend import FFI
315 import _io
316 ffi = FFI()
317 a = _io.BytesIO(b"Hello, world!")
318 buf = a.getbuffer()
319 # used to segfault
320 raises(TypeError, ffi.from_buffer, buf)
321
322 def test_memmove(self):
323 import sys
324 import _cffi_backend as _cffi1_backend
325 ffi = _cffi1_backend.FFI()
326 p = ffi.new("short[]", [-1234, -2345, -3456, -4567, -5678])
327 ffi.memmove(p, p + 1, 4)
328 assert list(p) == [-2345, -3456, -3456, -4567, -5678]
329 p[2] = 999
330 ffi.memmove(p + 2, p, 6)
331 assert list(p) == [-2345, -3456, -2345, -3456, 999]
332 ffi.memmove(p + 4, ffi.new("char[]", b"\x71\x72"), 2)
333 if sys.byteorder == 'little':
334 assert list(p) == [-2345, -3456, -2345, -3456, 0x7271]
335 else:
336 assert list(p) == [-2345, -3456, -2345, -3456, 0x7172]
337
338 def test_memmove_buffer(self):
339 import _cffi_backend as _cffi1_backend
340 import array
341 ffi = _cffi1_backend.FFI()
342 a = array.array('H', [10000, 20000, 30000])
343 p = ffi.new("short[]", 5)
344 ffi.memmove(p, a, 6)
345 assert list(p) == [10000, 20000, 30000, 0, 0]
346 ffi.memmove(p + 1, a, 6)
347 assert list(p) == [10000, 10000, 20000, 30000, 0]
348 b = array.array('h', [-1000, -2000, -3000])
349 ffi.memmove(b, a, 4)
350 assert b.tolist() == [10000, 20000, -3000]
351 assert a.tolist() == [10000, 20000, 30000]
352 p[0] = 999
353 p[1] = 998
354 p[2] = 997
355 p[3] = 996
356 p[4] = 995
357 ffi.memmove(b, p, 2)
358 assert b.tolist() == [999, 20000, -3000]
359 ffi.memmove(b, p + 2, 4)
360 assert b.tolist() == [997, 996, -3000]
361 p[2] = -p[2]
362 p[3] = -p[3]
363 ffi.memmove(b, p + 2, 6)
364 assert b.tolist() == [-997, -996, 995]
365
366 def test_memmove_readonly_readwrite(self):
367 import _cffi_backend as _cffi1_backend
368 ffi = _cffi1_backend.FFI()
369 p = ffi.new("signed char[]", 5)
370 ffi.memmove(p, b"abcde", 3)
371 assert list(p) == [ord("a"), ord("b"), ord("c"), 0, 0]
372 ffi.memmove(p, bytearray(b"ABCDE"), 2)
373 assert list(p) == [ord("A"), ord("B"), ord("c"), 0, 0]
374 raises((TypeError, BufferError), ffi.memmove, b"abcde", p, 3)
375 ba = bytearray(b"xxxxx")
376 ffi.memmove(dest=ba, src=p, n=3)
377 assert ba == bytearray(b"ABcxx")
378
379 def test_ffi_types(self):
380 import _cffi_backend as _cffi1_backend
381 CData = _cffi1_backend.FFI.CData
382 CType = _cffi1_backend.FFI.CType
383 ffi = _cffi1_backend.FFI()
384 assert isinstance(ffi.cast("int", 42), CData)
385 assert isinstance(ffi.typeof("int"), CType)
386
387 def test_ffi_gc(self):
388 import _cffi_backend as _cffi1_backend
389 ffi = _cffi1_backend.FFI()
390 p = ffi.new("int *", 123)
391 seen = []
392 def destructor(p1):
393 assert p1 is p
394 assert p1[0] == 123
395 seen.append(1)
396 ffi.gc(p, destructor=destructor) # instantly forgotten
397 for i in range(5):
398 if seen:
399 break
400 import gc
401 gc.collect()
402 assert seen == [1]
403
404 def test_ffi_gc_disable(self):
405 import _cffi_backend as _cffi1_backend
406 ffi = _cffi1_backend.FFI()
407 p = ffi.new("int *", 123)
408 raises(TypeError, ffi.gc, p, None)
409 seen = []
410 q1 = ffi.gc(p, lambda p: seen.append(1))
411 q2 = ffi.gc(q1, lambda p: seen.append(2), size=123)
412 import gc; gc.collect()
413 assert seen == []
414 assert ffi.gc(q1, None) is None
415 del q1, q2
416 for i in range(5):
417 if seen:
418 break
419 import gc
420 gc.collect()
421 assert seen == [2]
422
423 def test_ffi_new_allocator_1(self):
424 import _cffi_backend as _cffi1_backend
425 ffi = _cffi1_backend.FFI()
426 alloc1 = ffi.new_allocator()
427 alloc2 = ffi.new_allocator(should_clear_after_alloc=False)
428 for retry in range(100):
429 p1 = alloc1("int[10]")
430 p2 = alloc2("int[10]")
431 combination = 0
432 for i in range(10):
433 assert p1[i] == 0
434 combination |= p2[i]
435 p1[i] = -42
436 p2[i] = -43
437 if combination != 0:
438 break
439 del p1, p2
440 import gc; gc.collect()
441 else:
442 raise AssertionError("cannot seem to get an int[10] not "
443 "completely cleared")
444
445 def test_ffi_new_allocator_2(self):
446 import _cffi_backend as _cffi1_backend
447 ffi = _cffi1_backend.FFI()
448 seen = []
449 def myalloc(size):
450 seen.append(size)
451 return ffi.new("char[]", b"X" * size)
452 def myfree(raw):
453 seen.append(raw)
454 alloc1 = ffi.new_allocator(myalloc, myfree)
455 alloc2 = ffi.new_allocator(alloc=myalloc, free=myfree,
456 should_clear_after_alloc=False)
457 p1 = alloc1("int[10]")
458 p2 = alloc2("int[]", 10)
459 assert seen == [40, 40]
460 assert ffi.typeof(p1) == ffi.typeof("int[10]")
461 assert ffi.sizeof(p1) == 40
462 assert ffi.typeof(p2) == ffi.typeof("int[]")
463 assert ffi.sizeof(p2) == 40
464 assert p1[5] == 0
465 assert p2[6] == ord('X') * 0x01010101
466 raw1 = ffi.cast("char *", p1)
467 raw2 = ffi.cast("char *", p2)
468 del p1, p2
469 retries = 0
470 while len(seen) != 4:
471 retries += 1
472 assert retries <= 5
473 import gc; gc.collect()
474 assert (seen == [40, 40, raw1, raw2] or
475 seen == [40, 40, raw2, raw1])
476 assert repr(seen[2]) == "<cdata 'char[]' owning 41 bytes>"
477 assert repr(seen[3]) == "<cdata 'char[]' owning 41 bytes>"
478
479 def test_ffi_new_allocator_3(self):
480 import _cffi_backend as _cffi1_backend
481 ffi = _cffi1_backend.FFI()
482 seen = []
483 def myalloc(size):
484 seen.append(size)
485 return ffi.new("char[]", b"X" * size)
486 alloc1 = ffi.new_allocator(myalloc) # no 'free'
487 p1 = alloc1("int[10]")
488 assert seen == [40]
489 assert ffi.typeof(p1) == ffi.typeof("int[10]")
490 assert ffi.sizeof(p1) == 40
491 assert p1[5] == 0
492
493 def test_ffi_new_allocator_4(self):
494 import _cffi_backend as _cffi1_backend
495 ffi = _cffi1_backend.FFI()
496 raises(TypeError, ffi.new_allocator, free=lambda x: None)
497 #
498 def myalloc2(size):
499 raise LookupError
500 alloc2 = ffi.new_allocator(myalloc2)
501 raises(LookupError, alloc2, "int[5]")
502 #
503 def myalloc3(size):
504 return 42
505 alloc3 = ffi.new_allocator(myalloc3)
506 e = raises(TypeError, alloc3, "int[5]")
507 assert str(e.value) == "alloc() must return a cdata object (got int)"
508 #
509 def myalloc4(size):
510 return ffi.cast("int", 42)
511 alloc4 = ffi.new_allocator(myalloc4)
512 e = raises(TypeError, alloc4, "int[5]")
513 assert str(e.value) == "alloc() must return a cdata pointer, not 'int'"
514 #
515 def myalloc5(size):
516 return ffi.NULL
517 alloc5 = ffi.new_allocator(myalloc5)
518 raises(MemoryError, alloc5, "int[5]")
519
520 def test_bool_issue228(self):
521 import _cffi_backend as _cffi1_backend
522 ffi = _cffi1_backend.FFI()
523 fntype = ffi.typeof("int(*callback)(bool is_valid)")
524 assert repr(fntype.args[0]) == "<ctype '_Bool'>"
525
526 def test_FILE_issue228(self):
527 import _cffi_backend as _cffi1_backend
528 fntype1 = _cffi1_backend.FFI().typeof("FILE *")
529 fntype2 = _cffi1_backend.FFI().typeof("FILE *")
530 assert repr(fntype1) == "<ctype 'FILE *'>"
531 assert fntype1 is fntype2
532
533 def test_cast_from_int_type_to_bool(self):
534 import _cffi_backend as _cffi1_backend
535 ffi = _cffi1_backend.FFI()
536 for basetype in ['char', 'short', 'int', 'long', 'long long']:
537 for sign in ['signed', 'unsigned']:
538 type = '%s %s' % (sign, basetype)
539 assert int(ffi.cast("_Bool", ffi.cast(type, 42))) == 1
540 assert int(ffi.cast("bool", ffi.cast(type, 42))) == 1
541 assert int(ffi.cast("_Bool", ffi.cast(type, 0))) == 0
542
543 def test_init_once(self):
544 import _cffi_backend as _cffi1_backend
545 def do_init():
546 seen.append(1)
547 return 42
548 ffi = _cffi1_backend.FFI()
549 seen = []
550 for i in range(3):
551 res = ffi.init_once(do_init, "tag1")
552 assert res == 42
553 assert seen == [1]
554 for i in range(3):
555 res = ffi.init_once(do_init, "tag2")
556 assert res == 42
557 assert seen == [1, 1]
558
559 def test_init_once_failure(self):
560 import _cffi_backend as _cffi1_backend
561 def do_init():
562 seen.append(1)
563 raise ValueError
564 ffi = _cffi1_backend.FFI()
565 seen = []
566 for i in range(5):
567 raises(ValueError, ffi.init_once, do_init, "tag")
568 assert seen == [1] * (i + 1)
569
570 def test_unpack(self):
571 import _cffi_backend as _cffi1_backend
572 ffi = _cffi1_backend.FFI()
573 p = ffi.new("char[]", b"abc\x00def")
574 assert ffi.unpack(p+1, 7) == b"bc\x00def\x00"
575 p = ffi.new("int[]", [-123456789])
576 assert ffi.unpack(p, 1) == [-123456789]
577
578 def test_bug_1(self):
579 import _cffi_backend as _cffi1_backend
580 ffi = _cffi1_backend.FFI()
581 q = ffi.new("char[]", b"abcd")
582 p = ffi.cast("char(*)(void)", q)
583 raises(TypeError, ffi.string, p)
584
585 def test_negative_array_size(self):
586 import _cffi_backend as _cffi1_backend
587 ffi = _cffi1_backend.FFI()
588 raises(ffi.error, ffi.cast, "int[-5]", 0)
589
590 def test_char32_t(self):
591 import _cffi_backend as _cffi1_backend
592 ffi = _cffi1_backend.FFI()
593 z = ffi.new("char32_t[]", u'\U00012345')
594 assert len(z) == 2
595 assert ffi.cast("int *", z)[0] == 0x12345
596 assert list(z) == [u'\U00012345', u'\x00'] # maybe a 2-unichars str
597
598 def test_ffi_array_as_init(self):
599 import _cffi_backend as _cffi1_backend
600 ffi = _cffi1_backend.FFI()
601 p = ffi.new("int[4]", [10, 20, 30, 400])
602 q = ffi.new("int[4]", p)
603 assert list(q) == [10, 20, 30, 400]
604 raises(TypeError, ffi.new, "int[3]", p)
605 raises(TypeError, ffi.new, "int[5]", p)
606 raises(TypeError, ffi.new, "int16_t[4]", p)