| 1 |
|
| 2 |
import sys, platform |
| 3 |
import pytest |
| 4 |
from extra_tests.cffi_tests.cffi0 import backend_tests, test_function, test_ownlib |
| 5 |
from extra_tests.cffi_tests.support import u |
| 6 |
from cffi import FFI |
| 7 |
import _cffi_backend |
| 8 |
|
| 9 |
|
| 10 |
class TestFFI(backend_tests.BackendTests, |
| 11 |
test_function.TestFunction, |
| 12 |
test_ownlib.TestOwnLib): |
| 13 |
TypeRepr = "<ctype '%s'>" |
| 14 |
|
| 15 |
@staticmethod |
| 16 |
def Backend(): |
| 17 |
return _cffi_backend |
| 18 |
|
| 19 |
def test_not_supported_bitfield_in_result(self): |
| 20 |
ffi = FFI(backend=self.Backend()) |
| 21 |
ffi.cdef("struct foo_s { int a,b,c,d,e; int x:1; };") |
| 22 |
e = pytest.raises(NotImplementedError, ffi.callback, |
| 23 |
"struct foo_s foo(void)", lambda: 42) |
| 24 |
assert str(e.value) == ("struct foo_s(*)(): " |
| 25 |
"callback with unsupported argument or return type or with '...'") |
| 26 |
|
| 27 |
def test_inspecttype(self): |
| 28 |
ffi = FFI(backend=self.Backend()) |
| 29 |
assert ffi.typeof("long").kind == "primitive" |
| 30 |
assert ffi.typeof("long(*)(long, long**, ...)").cname == ( |
| 31 |
"long(*)(long, long * *, ...)") |
| 32 |
assert ffi.typeof("long(*)(long, long**, ...)").ellipsis is True |
| 33 |
|
| 34 |
def test_new_handle(self): |
| 35 |
ffi = FFI(backend=self.Backend()) |
| 36 |
o = [2, 3, 4] |
| 37 |
p = ffi.new_handle(o) |
| 38 |
assert ffi.typeof(p) == ffi.typeof("void *") |
| 39 |
assert ffi.from_handle(p) is o |
| 40 |
assert ffi.from_handle(ffi.cast("char *", p)) is o |
| 41 |
pytest.raises(RuntimeError, ffi.from_handle, ffi.NULL) |
| 42 |
|
| 43 |
def test_callback_onerror(self): |
| 44 |
ffi = FFI(backend=self.Backend()) |
| 45 |
seen = [] |
| 46 |
def oops(*args): |
| 47 |
seen.append(args) |
| 48 |
def otherfunc(): |
| 49 |
raise LookupError |
| 50 |
def cb(n): |
| 51 |
otherfunc() |
| 52 |
a = ffi.callback("int(*)(int)", cb, error=42, onerror=oops) |
| 53 |
res = a(234) |
| 54 |
assert res == 42 |
| 55 |
assert len(seen) == 1 |
| 56 |
exc, val, tb = seen[0] |
| 57 |
assert exc is LookupError |
| 58 |
assert isinstance(val, LookupError) |
| 59 |
assert tb.tb_frame.f_code.co_name == 'cb' |
| 60 |
assert tb.tb_frame.f_locals['n'] == 234 |
| 61 |
|
| 62 |
def test_ffi_new_allocator_2(self): |
| 63 |
ffi = FFI(backend=self.Backend()) |
| 64 |
seen = [] |
| 65 |
def myalloc(size): |
| 66 |
seen.append(size) |
| 67 |
return ffi.new("char[]", b"X" * size) |
| 68 |
def myfree(raw): |
| 69 |
seen.append(raw) |
| 70 |
alloc1 = ffi.new_allocator(myalloc, myfree) |
| 71 |
alloc2 = ffi.new_allocator(alloc=myalloc, free=myfree, |
| 72 |
should_clear_after_alloc=False) |
| 73 |
p1 = alloc1("int[10]") |
| 74 |
p2 = alloc2("int[]", 10) |
| 75 |
assert seen == [40, 40] |
| 76 |
assert ffi.typeof(p1) == ffi.typeof("int[10]") |
| 77 |
assert ffi.sizeof(p1) == 40 |
| 78 |
assert ffi.typeof(p2) == ffi.typeof("int[]") |
| 79 |
assert ffi.sizeof(p2) == 40 |
| 80 |
assert p1[5] == 0 |
| 81 |
assert p2[6] == ord('X') * 0x01010101 |
| 82 |
raw1 = ffi.cast("char *", p1) |
| 83 |
raw2 = ffi.cast("char *", p2) |
| 84 |
del p1, p2 |
| 85 |
retries = 0 |
| 86 |
while len(seen) != 4: |
| 87 |
retries += 1 |
| 88 |
assert retries <= 5 |
| 89 |
import gc; gc.collect() |
| 90 |
assert seen == [40, 40, raw1, raw2] |
| 91 |
assert repr(seen[2]) == "<cdata 'char[]' owning 41 bytes>" |
| 92 |
assert repr(seen[3]) == "<cdata 'char[]' owning 41 bytes>" |
| 93 |
|
| 94 |
def test_ffi_new_allocator_3(self): |
| 95 |
ffi = FFI(backend=self.Backend()) |
| 96 |
seen = [] |
| 97 |
def myalloc(size): |
| 98 |
seen.append(size) |
| 99 |
return ffi.new("char[]", b"X" * size) |
| 100 |
alloc1 = ffi.new_allocator(myalloc) |
| 101 |
p1 = alloc1("int[10]") |
| 102 |
assert seen == [40] |
| 103 |
assert ffi.typeof(p1) == ffi.typeof("int[10]") |
| 104 |
assert ffi.sizeof(p1) == 40 |
| 105 |
assert p1[5] == 0 |
| 106 |
|
| 107 |
def test_ffi_new_allocator_4(self): |
| 108 |
ffi = FFI(backend=self.Backend()) |
| 109 |
pytest.raises(TypeError, ffi.new_allocator, free=lambda x: None) |
| 110 |
|
| 111 |
def myalloc2(size): |
| 112 |
raise LookupError |
| 113 |
alloc2 = ffi.new_allocator(myalloc2) |
| 114 |
pytest.raises(LookupError, alloc2, "int[5]") |
| 115 |
|
| 116 |
def myalloc3(size): |
| 117 |
return 42 |
| 118 |
alloc3 = ffi.new_allocator(myalloc3) |
| 119 |
e = pytest.raises(TypeError, alloc3, "int[5]") |
| 120 |
assert str(e.value) == "alloc() must return a cdata object (got int)" |
| 121 |
|
| 122 |
def myalloc4(size): |
| 123 |
return ffi.cast("int", 42) |
| 124 |
alloc4 = ffi.new_allocator(myalloc4) |
| 125 |
e = pytest.raises(TypeError, alloc4, "int[5]") |
| 126 |
assert str(e.value) == "alloc() must return a cdata pointer, not 'int'" |
| 127 |
|
| 128 |
def myalloc5(size): |
| 129 |
return ffi.NULL |
| 130 |
alloc5 = ffi.new_allocator(myalloc5) |
| 131 |
pytest.raises(MemoryError, alloc5, "int[5]") |
| 132 |
|
| 133 |
def test_new_struct_containing_struct_containing_array_varsize(self): |
| 134 |
ffi = FFI(backend=self.Backend()) |
| 135 |
ffi.cdef(""" |
| 136 |
struct foo_s { int len[100]; short data[]; }; |
| 137 |
struct bar_s { int abc[100]; struct foo_s tail; }; |
| 138 |
""") |
| 139 |
|
| 140 |
|
| 141 |
for i in range(1, 501, 100): |
| 142 |
p = ffi.new("struct bar_s *", [[10], [[20], [3,4,5,6,7,8,9] * i]]) |
| 143 |
assert p.abc[0] == 10 |
| 144 |
assert p.tail.len[0] == 20 |
| 145 |
assert p.tail.data[0] == 3 |
| 146 |
assert p.tail.data[6] == 9 |
| 147 |
assert p.tail.data[7 * i - 1] == 9 |
| 148 |
|
| 149 |
def test_bogus_struct_containing_struct_containing_array_varsize(self): |
| 150 |
ffi = FFI(backend=self.Backend()) |
| 151 |
ffi.cdef(""" |
| 152 |
struct foo_s { signed char len; signed char data[]; }; |
| 153 |
struct bar_s { struct foo_s foo; int bcd; }; |
| 154 |
""") |
| 155 |
p = ffi.new("struct bar_s *", [[123, [45, 56, 67, 78]], 9999999]) |
| 156 |
assert p.foo.len == 123 |
| 157 |
assert p.foo.data[0] == 45 |
| 158 |
assert p.foo.data[1] == 56 |
| 159 |
assert p.foo.data[2] == 67 |
| 160 |
assert p.bcd == 9999999 |
| 161 |
assert p.foo.data[3] != 78 |
| 162 |
|
| 163 |
def test_issue553(self): |
| 164 |
import gc, warnings |
| 165 |
ffi = FFI(backend=self.Backend()) |
| 166 |
p = ffi.new("int *", 123) |
| 167 |
with warnings.catch_warnings(record=True) as w: |
| 168 |
ffi.gc(p, lambda x: None) |
| 169 |
gc.collect() |
| 170 |
assert w == [] |
| 171 |
|
| 172 |
def test_issue553_from_buffer(self): |
| 173 |
import gc, warnings |
| 174 |
ffi = FFI(backend=self.Backend()) |
| 175 |
buf = b"123" |
| 176 |
with warnings.catch_warnings(record=True) as w: |
| 177 |
ffi.from_buffer(buf) |
| 178 |
gc.collect() |
| 179 |
assert w == [] |
| 180 |
|
| 181 |
|
| 182 |
class TestBitfield: |
| 183 |
def check(self, source, expected_ofs_y, expected_align, expected_size): |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
ffi = FFI() |
| 188 |
ffi.cdef("struct s1 { %s };" % source) |
| 189 |
ctype = ffi.typeof("struct s1") |
| 190 |
|
| 191 |
ffi1 = FFI() |
| 192 |
ffi1.cdef(""" |
| 193 |
static const int Gofs_y, Galign, Gsize; |
| 194 |
struct s1 *try_with_value(int fieldnum, long long value); |
| 195 |
""") |
| 196 |
fnames = [name for name, cfield in ctype.fields |
| 197 |
if name and cfield.bitsize > 0] |
| 198 |
setters = ['case %d: s.%s = value; break;' % iname |
| 199 |
for iname in enumerate(fnames)] |
| 200 |
lib = ffi1.verify(""" |
| 201 |
#include <string.h> |
| 202 |
struct s1 { %s }; |
| 203 |
struct sa { char a; struct s1 b; }; |
| 204 |
#define Gofs_y offsetof(struct s1, y) |
| 205 |
#define Galign offsetof(struct sa, b) |
| 206 |
#define Gsize sizeof(struct s1) |
| 207 |
struct s1 *try_with_value(int fieldnum, long long value) |
| 208 |
{ |
| 209 |
static struct s1 s; |
| 210 |
memset(&s, 0, sizeof(s)); |
| 211 |
switch (fieldnum) { %s } |
| 212 |
return &s; |
| 213 |
} |
| 214 |
""" % (source, ' '.join(setters))) |
| 215 |
if sys.platform == 'win32': |
| 216 |
expected_ofs_y = lib.Gofs_y |
| 217 |
expected_align = lib.Galign |
| 218 |
expected_size = lib.Gsize |
| 219 |
else: |
| 220 |
assert (lib.Gofs_y, lib.Galign, lib.Gsize) == ( |
| 221 |
expected_ofs_y, expected_align, expected_size) |
| 222 |
|
| 223 |
assert ffi.offsetof("struct s1", "y") == expected_ofs_y |
| 224 |
assert ffi.alignof("struct s1") == expected_align |
| 225 |
assert ffi.sizeof("struct s1") == expected_size |
| 226 |
|
| 227 |
for name, cfield in ctype.fields: |
| 228 |
if cfield.bitsize < 0 or not name: |
| 229 |
continue |
| 230 |
if int(ffi.cast(cfield.type, -1)) == -1: |
| 231 |
min_value = -(1 << (cfield.bitsize-1)) |
| 232 |
max_value = (1 << (cfield.bitsize-1)) - 1 |
| 233 |
else: |
| 234 |
min_value = 0 |
| 235 |
max_value = (1 << cfield.bitsize) - 1 |
| 236 |
for t in [1, 2, 4, 8, 16, 128, 2813, 89728, 981729, |
| 237 |
-1,-2,-4,-8,-16,-128,-2813,-89728,-981729]: |
| 238 |
if min_value <= t <= max_value: |
| 239 |
self._fieldcheck(ffi, lib, fnames, name, t) |
| 240 |
|
| 241 |
def _fieldcheck(self, ffi, lib, fnames, name, value): |
| 242 |
s = ffi.new("struct s1 *") |
| 243 |
setattr(s, name, value) |
| 244 |
assert getattr(s, name) == value |
| 245 |
raw1 = ffi.buffer(s)[:] |
| 246 |
buff1 = ffi.buffer(s) |
| 247 |
t = lib.try_with_value(fnames.index(name), value) |
| 248 |
raw2 = ffi.buffer(t, len(raw1))[:] |
| 249 |
assert raw1 == raw2 |
| 250 |
buff2 = ffi.buffer(t, len(buff1)) |
| 251 |
assert buff1 == buff2 |
| 252 |
|
| 253 |
def test_bitfield_basic(self): |
| 254 |
self.check("int a; int b:9; int c:20; int y;", 8, 4, 12) |
| 255 |
self.check("int a; short b:9; short c:7; int y;", 8, 4, 12) |
| 256 |
self.check("int a; short b:9; short c:9; int y;", 8, 4, 12) |
| 257 |
|
| 258 |
def test_bitfield_reuse_if_enough_space(self): |
| 259 |
self.check("int a:2; char y;", 1, 4, 4) |
| 260 |
self.check("int a:1; char b ; int c:1; char y;", 3, 4, 4) |
| 261 |
self.check("int a:1; char b:8; int c:1; char y;", 3, 4, 4) |
| 262 |
self.check("char a; int b:9; char y;", 3, 4, 4) |
| 263 |
self.check("char a; short b:9; char y;", 4, 2, 6) |
| 264 |
self.check("int a:2; char b:6; char y;", 1, 4, 4) |
| 265 |
self.check("int a:2; char b:7; char y;", 2, 4, 4) |
| 266 |
self.check("int a:2; short b:15; char c:2; char y;", 5, 4, 8) |
| 267 |
self.check("int a:2; char b:1; char c:1; char y;", 1, 4, 4) |
| 268 |
|
| 269 |
@pytest.mark.skipif( |
| 270 |
"not (sys.platform == 'darwin' and platform.machine() == 'arm64')" |
| 271 |
" and " |
| 272 |
"platform.machine().startswith(('arm', 'aarch64'))") |
| 273 |
def test_bitfield_anonymous_no_align(self): |
| 274 |
L = FFI().alignof("long long") |
| 275 |
self.check("char y; int :1;", 0, 1, 2) |
| 276 |
self.check("char x; int z:1; char y;", 2, 4, 4) |
| 277 |
self.check("char x; int :1; char y;", 2, 1, 3) |
| 278 |
self.check("char x; long long z:48; char y;", 7, L, 8) |
| 279 |
self.check("char x; long long :48; char y;", 7, 1, 8) |
| 280 |
self.check("char x; long long z:56; char y;", 8, L, 8 + L) |
| 281 |
self.check("char x; long long :56; char y;", 8, 1, 9) |
| 282 |
self.check("char x; long long z:57; char y;", L + 8, L, L + 8 + L) |
| 283 |
self.check("char x; long long :57; char y;", L + 8, 1, L + 9) |
| 284 |
|
| 285 |
@pytest.mark.skipif( |
| 286 |
"(sys.platform == 'darwin' and platform.machine() == 'arm64')" |
| 287 |
" or " |
| 288 |
"not platform.machine().startswith(('arm', 'aarch64'))") |
| 289 |
def test_bitfield_anonymous_align_arm(self): |
| 290 |
L = FFI().alignof("long long") |
| 291 |
self.check("char y; int :1;", 0, 4, 4) |
| 292 |
self.check("char x; int z:1; char y;", 2, 4, 4) |
| 293 |
self.check("char x; int :1; char y;", 2, 4, 4) |
| 294 |
self.check("char x; long long z:48; char y;", 7, L, 8) |
| 295 |
self.check("char x; long long :48; char y;", 7, 8, 8) |
| 296 |
self.check("char x; long long z:56; char y;", 8, L, 8 + L) |
| 297 |
self.check("char x; long long :56; char y;", 8, L, 8 + L) |
| 298 |
self.check("char x; long long z:57; char y;", L + 8, L, L + 8 + L) |
| 299 |
self.check("char x; long long :57; char y;", L + 8, L, L + 8 + L) |
| 300 |
|
| 301 |
@pytest.mark.skipif( |
| 302 |
"not (sys.platform == 'darwin' and platform.machine() == 'arm64')" |
| 303 |
" and " |
| 304 |
"platform.machine().startswith(('arm', 'aarch64'))") |
| 305 |
def test_bitfield_zero(self): |
| 306 |
L = FFI().alignof("long long") |
| 307 |
self.check("char y; int :0;", 0, 1, 4) |
| 308 |
self.check("char x; int :0; char y;", 4, 1, 5) |
| 309 |
self.check("char x; int :0; int :0; char y;", 4, 1, 5) |
| 310 |
self.check("char x; long long :0; char y;", L, 1, L + 1) |
| 311 |
self.check("short x, y; int :0; int :0;", 2, 2, 4) |
| 312 |
self.check("char x; int :0; short b:1; char y;", 5, 2, 6) |
| 313 |
self.check("int a:1; int :0; int b:1; char y;", 5, 4, 8) |
| 314 |
|
| 315 |
@pytest.mark.skipif( |
| 316 |
"(sys.platform == 'darwin' and platform.machine() == 'arm64')" |
| 317 |
" or " |
| 318 |
"not platform.machine().startswith(('arm', 'aarch64'))") |
| 319 |
def test_bitfield_zero_arm(self): |
| 320 |
L = FFI().alignof("long long") |
| 321 |
self.check("char y; int :0;", 0, 4, 4) |
| 322 |
self.check("char x; int :0; char y;", 4, 4, 8) |
| 323 |
self.check("char x; int :0; int :0; char y;", 4, 4, 8) |
| 324 |
self.check("char x; long long :0; char y;", L, 8, L + 8) |
| 325 |
self.check("short x, y; int :0; int :0;", 2, 4, 4) |
| 326 |
self.check("char x; int :0; short b:1; char y;", 5, 4, 8) |
| 327 |
self.check("int a:1; int :0; int b:1; char y;", 5, 4, 8) |
| 328 |
|
| 329 |
def test_error_cases(self): |
| 330 |
ffi = FFI() |
| 331 |
ffi.cdef("struct s1 { float x:1; };") |
| 332 |
with pytest.raises(TypeError): |
| 333 |
ffi.new("struct s1 *") |
| 334 |
ffi.cdef("struct s2 { char x:0; };") |
| 335 |
with pytest.raises(TypeError): |
| 336 |
ffi.new("struct s2 *") |
| 337 |
ffi.cdef("struct s3 { char x:9; };") |
| 338 |
with pytest.raises(TypeError): |
| 339 |
ffi.new("struct s3 *") |
| 340 |
|
| 341 |
def test_struct_with_typedef(self): |
| 342 |
ffi = FFI() |
| 343 |
ffi.cdef("typedef struct { float x; } foo_t;") |
| 344 |
p = ffi.new("foo_t *", [5.2]) |
| 345 |
assert repr(p).startswith("<cdata 'foo_t *' ") |
| 346 |
|
| 347 |
def test_struct_array_no_length(self): |
| 348 |
ffi = FFI() |
| 349 |
ffi.cdef("struct foo_s { int x; int a[]; };") |
| 350 |
p = ffi.new("struct foo_s *", [100, [200, 300, 400]]) |
| 351 |
assert p.x == 100 |
| 352 |
assert ffi.typeof(p.a) is ffi.typeof("int[]") |
| 353 |
assert len(p.a) == 3 |
| 354 |
assert p.a[0] == 200 |
| 355 |
assert p.a[1] == 300 |
| 356 |
assert p.a[2] == 400 |
| 357 |
assert list(p.a) == [200, 300, 400] |
| 358 |
q = ffi.cast("struct foo_s *", p) |
| 359 |
assert q.x == 100 |
| 360 |
assert ffi.typeof(q.a) is ffi.typeof("int *") |
| 361 |
pytest.raises(TypeError, len, q.a) |
| 362 |
assert q.a[0] == 200 |
| 363 |
assert q.a[1] == 300 |
| 364 |
assert q.a[2] == 400 |
| 365 |
pytest.raises(TypeError, list, q.a) |
| 366 |
|
| 367 |
@pytest.mark.skipif("sys.platform != 'win32'") |
| 368 |
def test_getwinerror(self): |
| 369 |
ffi = FFI() |
| 370 |
code, message = ffi.getwinerror(1155) |
| 371 |
assert code == 1155 |
| 372 |
assert message == ("No application is associated with the " |
| 373 |
"specified file for this operation") |
| 374 |
ffi.cdef("void SetLastError(int);") |
| 375 |
lib = ffi.dlopen("Kernel32.dll") |
| 376 |
lib.SetLastError(2) |
| 377 |
code, message = ffi.getwinerror() |
| 378 |
assert code == 2 |
| 379 |
assert message == "The system cannot find the file specified" |
| 380 |
code, message = ffi.getwinerror(-1) |
| 381 |
assert code == 2 |
| 382 |
assert message == "The system cannot find the file specified" |
| 383 |
|
| 384 |
def test_from_buffer(self): |
| 385 |
import array |
| 386 |
ffi = FFI() |
| 387 |
a = array.array('H', [10000, 20000, 30000]) |
| 388 |
c = ffi.from_buffer(a) |
| 389 |
assert ffi.typeof(c) is ffi.typeof("char[]") |
| 390 |
assert len(c) == 6 |
| 391 |
ffi.cast("unsigned short *", c)[1] += 500 |
| 392 |
assert list(a) == [10000, 20500, 30000] |
| 393 |
assert c == ffi.from_buffer("char[]", a, True) |
| 394 |
assert c == ffi.from_buffer(a, require_writable=True) |
| 395 |
|
| 396 |
c = ffi.from_buffer("unsigned short[]", a) |
| 397 |
assert len(c) == 3 |
| 398 |
assert c[1] == 20500 |
| 399 |
|
| 400 |
p = ffi.from_buffer(b"abcd") |
| 401 |
assert p[2] == b"c" |
| 402 |
|
| 403 |
assert p == ffi.from_buffer(b"abcd", require_writable=False) |
| 404 |
pytest.raises((TypeError, BufferError), ffi.from_buffer, |
| 405 |
"char[]", b"abcd", True) |
| 406 |
pytest.raises((TypeError, BufferError), ffi.from_buffer, b"abcd", |
| 407 |
require_writable=True) |
| 408 |
|
| 409 |
def test_release(self): |
| 410 |
ffi = FFI() |
| 411 |
p = ffi.new("int[]", 123) |
| 412 |
ffi.release(p) |
| 413 |
|
| 414 |
ffi.release(p) |
| 415 |
|
| 416 |
def test_memmove(self): |
| 417 |
ffi = FFI() |
| 418 |
p = ffi.new("short[]", [-1234, -2345, -3456, -4567, -5678]) |
| 419 |
ffi.memmove(p, p + 1, 4) |
| 420 |
assert list(p) == [-2345, -3456, -3456, -4567, -5678] |
| 421 |
p[2] = 999 |
| 422 |
ffi.memmove(p + 2, p, 6) |
| 423 |
assert list(p) == [-2345, -3456, -2345, -3456, 999] |
| 424 |
ffi.memmove(p + 4, ffi.new("char[]", b"\x71\x72"), 2) |
| 425 |
if sys.byteorder == 'little': |
| 426 |
assert list(p) == [-2345, -3456, -2345, -3456, 0x7271] |
| 427 |
else: |
| 428 |
assert list(p) == [-2345, -3456, -2345, -3456, 0x7172] |
| 429 |
|
| 430 |
def test_memmove_buffer(self): |
| 431 |
import array |
| 432 |
ffi = FFI() |
| 433 |
a = array.array('H', [10000, 20000, 30000]) |
| 434 |
p = ffi.new("short[]", 5) |
| 435 |
ffi.memmove(p, a, 6) |
| 436 |
assert list(p) == [10000, 20000, 30000, 0, 0] |
| 437 |
ffi.memmove(p + 1, a, 6) |
| 438 |
assert list(p) == [10000, 10000, 20000, 30000, 0] |
| 439 |
b = array.array('h', [-1000, -2000, -3000]) |
| 440 |
ffi.memmove(b, a, 4) |
| 441 |
assert b.tolist() == [10000, 20000, -3000] |
| 442 |
assert a.tolist() == [10000, 20000, 30000] |
| 443 |
p[0] = 999 |
| 444 |
p[1] = 998 |
| 445 |
p[2] = 997 |
| 446 |
p[3] = 996 |
| 447 |
p[4] = 995 |
| 448 |
ffi.memmove(b, p, 2) |
| 449 |
assert b.tolist() == [999, 20000, -3000] |
| 450 |
ffi.memmove(b, p + 2, 4) |
| 451 |
assert b.tolist() == [997, 996, -3000] |
| 452 |
p[2] = -p[2] |
| 453 |
p[3] = -p[3] |
| 454 |
ffi.memmove(b, p + 2, 6) |
| 455 |
assert b.tolist() == [-997, -996, 995] |
| 456 |
|
| 457 |
def test_memmove_readonly_readwrite(self): |
| 458 |
ffi = FFI() |
| 459 |
p = ffi.new("signed char[]", 5) |
| 460 |
ffi.memmove(p, b"abcde", 3) |
| 461 |
assert list(p) == [ord("a"), ord("b"), ord("c"), 0, 0] |
| 462 |
ffi.memmove(p, bytearray(b"ABCDE"), 2) |
| 463 |
assert list(p) == [ord("A"), ord("B"), ord("c"), 0, 0] |
| 464 |
pytest.raises((TypeError, BufferError), ffi.memmove, b"abcde", p, 3) |
| 465 |
ba = bytearray(b"xxxxx") |
| 466 |
ffi.memmove(dest=ba, src=p, n=3) |
| 467 |
assert ba == bytearray(b"ABcxx") |
| 468 |
|
| 469 |
def test_all_primitives(self): |
| 470 |
ffi = FFI() |
| 471 |
for name in [ |
| 472 |
"char", |
| 473 |
"short", |
| 474 |
"int", |
| 475 |
"long", |
| 476 |
"long long", |
| 477 |
"signed char", |
| 478 |
"unsigned char", |
| 479 |
"unsigned short", |
| 480 |
"unsigned int", |
| 481 |
"unsigned long", |
| 482 |
"unsigned long long", |
| 483 |
"float", |
| 484 |
"double", |
| 485 |
"long double", |
| 486 |
"wchar_t", |
| 487 |
"char16_t", |
| 488 |
"char32_t", |
| 489 |
"_Bool", |
| 490 |
"int8_t", |
| 491 |
"uint8_t", |
| 492 |
"int16_t", |
| 493 |
"uint16_t", |
| 494 |
"int32_t", |
| 495 |
"uint32_t", |
| 496 |
"int64_t", |
| 497 |
"uint64_t", |
| 498 |
"int_least8_t", |
| 499 |
"uint_least8_t", |
| 500 |
"int_least16_t", |
| 501 |
"uint_least16_t", |
| 502 |
"int_least32_t", |
| 503 |
"uint_least32_t", |
| 504 |
"int_least64_t", |
| 505 |
"uint_least64_t", |
| 506 |
"int_fast8_t", |
| 507 |
"uint_fast8_t", |
| 508 |
"int_fast16_t", |
| 509 |
"uint_fast16_t", |
| 510 |
"int_fast32_t", |
| 511 |
"uint_fast32_t", |
| 512 |
"int_fast64_t", |
| 513 |
"uint_fast64_t", |
| 514 |
"intptr_t", |
| 515 |
"uintptr_t", |
| 516 |
"intmax_t", |
| 517 |
"uintmax_t", |
| 518 |
"ptrdiff_t", |
| 519 |
"size_t", |
| 520 |
"ssize_t", |
| 521 |
]: |
| 522 |
x = ffi.sizeof(name) |
| 523 |
assert 1 <= x <= 16 |
| 524 |
|
| 525 |
def test_ffi_def_extern(self): |
| 526 |
ffi = FFI() |
| 527 |
pytest.raises(ValueError, ffi.def_extern) |
| 528 |
|
| 529 |
def test_introspect_typedef(self): |
| 530 |
ffi = FFI() |
| 531 |
ffi.cdef("typedef int foo_t;") |
| 532 |
assert ffi.list_types() == (['foo_t'], [], []) |
| 533 |
assert ffi.typeof('foo_t').kind == 'primitive' |
| 534 |
assert ffi.typeof('foo_t').cname == 'int' |
| 535 |
|
| 536 |
ffi.cdef("typedef signed char a_t, c_t, g_t, b_t;") |
| 537 |
assert ffi.list_types() == (['a_t', 'b_t', 'c_t', 'foo_t', 'g_t'], |
| 538 |
[], []) |
| 539 |
|
| 540 |
def test_introspect_struct(self): |
| 541 |
ffi = FFI() |
| 542 |
ffi.cdef("struct foo_s { int a; };") |
| 543 |
assert ffi.list_types() == ([], ['foo_s'], []) |
| 544 |
assert ffi.typeof('struct foo_s').kind == 'struct' |
| 545 |
assert ffi.typeof('struct foo_s').cname == 'struct foo_s' |
| 546 |
|
| 547 |
def test_introspect_union(self): |
| 548 |
ffi = FFI() |
| 549 |
ffi.cdef("union foo_s { int a; };") |
| 550 |
assert ffi.list_types() == ([], [], ['foo_s']) |
| 551 |
assert ffi.typeof('union foo_s').kind == 'union' |
| 552 |
assert ffi.typeof('union foo_s').cname == 'union foo_s' |
| 553 |
|
| 554 |
def test_introspect_struct_and_typedef(self): |
| 555 |
ffi = FFI() |
| 556 |
ffi.cdef("typedef struct { int a; } foo_t;") |
| 557 |
assert ffi.list_types() == (['foo_t'], [], []) |
| 558 |
assert ffi.typeof('foo_t').kind == 'struct' |
| 559 |
assert ffi.typeof('foo_t').cname == 'foo_t' |
| 560 |
|
| 561 |
def test_introspect_included_type(self): |
| 562 |
ffi1 = FFI() |
| 563 |
ffi2 = FFI() |
| 564 |
ffi1.cdef("typedef signed char schar_t; struct sint_t { int x; };") |
| 565 |
ffi2.include(ffi1) |
| 566 |
assert ffi1.list_types() == ffi2.list_types() == ( |
| 567 |
['schar_t'], ['sint_t'], []) |
| 568 |
|
| 569 |
def test_introspect_order(self): |
| 570 |
ffi = FFI() |
| 571 |
ffi.cdef("union CFFIaaa { int a; }; typedef struct CFFIccc { int a; } CFFIb;") |
| 572 |
ffi.cdef("union CFFIg { int a; }; typedef struct CFFIcc { int a; } CFFIbbb;") |
| 573 |
ffi.cdef("union CFFIaa { int a; }; typedef struct CFFIa { int a; } CFFIbb;") |
| 574 |
assert ffi.list_types() == (['CFFIb', 'CFFIbb', 'CFFIbbb'], |
| 575 |
['CFFIa', 'CFFIcc', 'CFFIccc'], |
| 576 |
['CFFIaa', 'CFFIaaa', 'CFFIg']) |
| 577 |
|
| 578 |
def test_unpack(self): |
| 579 |
ffi = FFI() |
| 580 |
p = ffi.new("char[]", b"abc\x00def") |
| 581 |
assert ffi.unpack(p+1, 7) == b"bc\x00def\x00" |
| 582 |
p = ffi.new("int[]", [-123456789]) |
| 583 |
assert ffi.unpack(p, 1) == [-123456789] |
| 584 |
|
| 585 |
def test_negative_array_size(self): |
| 586 |
ffi = FFI() |
| 587 |
pytest.raises(ValueError, ffi.cast, "int[-5]", 0) |
| 588 |
|
| 589 |
def test_cannot_instantiate_manually(self): |
| 590 |
ffi = FFI() |
| 591 |
ct = type(ffi.typeof("void *")) |
| 592 |
pytest.raises(TypeError, ct) |
| 593 |
pytest.raises(TypeError, ct, ffi.NULL) |
| 594 |
for cd in [type(ffi.cast("void *", 0)), |
| 595 |
type(ffi.new("char[]", 3)), |
| 596 |
type(ffi.gc(ffi.NULL, lambda x: None))]: |
| 597 |
pytest.raises(TypeError, cd) |
| 598 |
pytest.raises(TypeError, cd, ffi.NULL) |
| 599 |
pytest.raises(TypeError, cd, ffi.typeof("void *")) |
| 600 |
|
| 601 |
def test_explicitly_defined_char16_t(self): |
| 602 |
ffi = FFI() |
| 603 |
ffi.cdef("typedef uint16_t char16_t;") |
| 604 |
x = ffi.cast("char16_t", 1234) |
| 605 |
assert ffi.typeof(x) is ffi.typeof("uint16_t") |
| 606 |
|
| 607 |
def test_char16_t(self): |
| 608 |
ffi = FFI() |
| 609 |
x = ffi.new("char16_t[]", 5) |
| 610 |
assert len(x) == 5 and ffi.sizeof(x) == 10 |
| 611 |
x[2] = u+'\u1324' |
| 612 |
assert x[2] == u+'\u1324' |
| 613 |
y = ffi.new("char16_t[]", u+'\u1234\u5678') |
| 614 |
assert len(y) == 3 |
| 615 |
assert list(y) == [u+'\u1234', u+'\u5678', u+'\x00'] |
| 616 |
assert ffi.string(y) == u+'\u1234\u5678' |
| 617 |
z = ffi.new("char16_t[]", u+'\U00012345') |
| 618 |
assert len(z) == 3 |
| 619 |
assert list(z) == [u+'\ud808', u+'\udf45', u+'\x00'] |
| 620 |
assert ffi.string(z) == u+'\U00012345' |
| 621 |
|
| 622 |
def test_char32_t(self): |
| 623 |
ffi = FFI() |
| 624 |
x = ffi.new("char32_t[]", 5) |
| 625 |
assert len(x) == 5 and ffi.sizeof(x) == 20 |
| 626 |
x[3] = u+'\U00013245' |
| 627 |
assert x[3] == u+'\U00013245' |
| 628 |
y = ffi.new("char32_t[]", u+'\u1234\u5678') |
| 629 |
assert len(y) == 3 |
| 630 |
assert list(y) == [u+'\u1234', u+'\u5678', u+'\x00'] |
| 631 |
py_uni = u+'\U00012345' |
| 632 |
z = ffi.new("char32_t[]", py_uni) |
| 633 |
assert len(z) == 2 |
| 634 |
assert list(z) == [py_uni, u+'\x00'] |
| 635 |
assert ffi.string(z) == py_uni |
| 636 |
if len(py_uni) == 1: |
| 637 |
s = ffi.new("char32_t[]", u+'\ud808\udf00') |
| 638 |
assert len(s) == 3 |
| 639 |
assert list(s) == [u+'\ud808', u+'\udf00', u+'\x00'] |