| 1 |
|
| 2 |
|
| 3 |
import sys, os |
| 4 |
import pytest |
| 5 |
from cffi import FFI, VerificationError, FFIError, CDefError |
| 6 |
from cffi import recompiler |
| 7 |
from extra_tests.cffi_tests.udir import udir |
| 8 |
from extra_tests.cffi_tests.support import u, long |
| 9 |
from extra_tests.cffi_tests.support import FdWriteCapture, StdErrCapture, _verify |
| 10 |
|
| 11 |
try: |
| 12 |
import importlib |
| 13 |
except ImportError: |
| 14 |
importlib = None |
| 15 |
|
| 16 |
|
| 17 |
def check_type_table(input, expected_output, included=None): |
| 18 |
ffi = FFI() |
| 19 |
if included: |
| 20 |
ffi1 = FFI() |
| 21 |
ffi1.cdef(included) |
| 22 |
ffi.include(ffi1) |
| 23 |
ffi.cdef(input) |
| 24 |
recomp = recompiler.Recompiler(ffi, 'testmod') |
| 25 |
recomp.collect_type_table() |
| 26 |
assert ''.join(map(str, recomp.cffi_types)) == expected_output |
| 27 |
|
| 28 |
def verify(ffi, module_name, source, *args, **kwds): |
| 29 |
no_cpp = kwds.pop('no_cpp', False) |
| 30 |
ignore_warnings = kwds.pop('ignore_warnings', False) |
| 31 |
kwds.setdefault('undef_macros', ['NDEBUG']) |
| 32 |
module_name = '_CFFI_' + module_name |
| 33 |
ffi.set_source(module_name, source) |
| 34 |
if not os.environ.get('NO_CPP') and not no_cpp: |
| 35 |
kwds.setdefault('source_extension', '.cpp') |
| 36 |
source = 'extern "C" {\n%s\n}' % (source,) |
| 37 |
elif sys.platform != 'win32' and not ignore_warnings: |
| 38 |
|
| 39 |
from extra_tests.cffi_tests.support import extra_compile_args |
| 40 |
kwds['extra_compile_args'] = (kwds.get('extra_compile_args', []) + |
| 41 |
extra_compile_args) |
| 42 |
if sys.platform == 'darwin': |
| 43 |
kwds['extra_link_args'] = (kwds.get('extra_link_args', []) + |
| 44 |
['-stdlib=libc++']) |
| 45 |
return _verify(ffi, module_name, source, *args, **kwds) |
| 46 |
|
| 47 |
def test_set_source_no_slashes(): |
| 48 |
ffi = FFI() |
| 49 |
pytest.raises(ValueError, ffi.set_source, "abc/def", None) |
| 50 |
pytest.raises(ValueError, ffi.set_source, "abc/def", "C code") |
| 51 |
|
| 52 |
|
| 53 |
def test_type_table_func(): |
| 54 |
check_type_table("double sin(double);", |
| 55 |
"(FUNCTION 1)(PRIMITIVE 14)(FUNCTION_END 0)") |
| 56 |
check_type_table("float sin(double);", |
| 57 |
"(FUNCTION 3)(PRIMITIVE 14)(FUNCTION_END 0)(PRIMITIVE 13)") |
| 58 |
check_type_table("float sin(void);", |
| 59 |
"(FUNCTION 2)(FUNCTION_END 0)(PRIMITIVE 13)") |
| 60 |
check_type_table("double sin(float); double cos(float);", |
| 61 |
"(FUNCTION 3)(PRIMITIVE 13)(FUNCTION_END 0)(PRIMITIVE 14)") |
| 62 |
check_type_table("double sin(float); double cos(double);", |
| 63 |
"(FUNCTION 1)(PRIMITIVE 14)(FUNCTION_END 0)" |
| 64 |
"(FUNCTION 1)(PRIMITIVE 13)(FUNCTION_END 0)") |
| 65 |
check_type_table("float sin(double); float cos(float);", |
| 66 |
"(FUNCTION 4)(PRIMITIVE 14)(FUNCTION_END 0)" |
| 67 |
"(FUNCTION 4)(PRIMITIVE 13)(FUNCTION_END 0)") |
| 68 |
|
| 69 |
def test_type_table_use_noop_for_repeated_args(): |
| 70 |
check_type_table("double sin(double *, double *);", |
| 71 |
"(FUNCTION 4)(POINTER 4)(NOOP 1)(FUNCTION_END 0)" |
| 72 |
"(PRIMITIVE 14)") |
| 73 |
check_type_table("double sin(double *, double *, double);", |
| 74 |
"(FUNCTION 3)(POINTER 3)(NOOP 1)(PRIMITIVE 14)" |
| 75 |
"(FUNCTION_END 0)") |
| 76 |
|
| 77 |
def test_type_table_dont_use_noop_for_primitives(): |
| 78 |
check_type_table("double sin(double, double);", |
| 79 |
"(FUNCTION 1)(PRIMITIVE 14)(PRIMITIVE 14)(FUNCTION_END 0)") |
| 80 |
|
| 81 |
def test_type_table_funcptr_as_argument(): |
| 82 |
check_type_table("int sin(double(float));", |
| 83 |
"(FUNCTION 6)(PRIMITIVE 13)(FUNCTION_END 0)" |
| 84 |
"(FUNCTION 7)(POINTER 0)(FUNCTION_END 0)" |
| 85 |
"(PRIMITIVE 14)(PRIMITIVE 7)") |
| 86 |
|
| 87 |
def test_type_table_variadic_function(): |
| 88 |
check_type_table("int sin(int, ...);", |
| 89 |
"(FUNCTION 1)(PRIMITIVE 7)(FUNCTION_END 1)(POINTER 0)") |
| 90 |
|
| 91 |
def test_type_table_array(): |
| 92 |
check_type_table("extern int a[100];", |
| 93 |
"(PRIMITIVE 7)(ARRAY 0)(None 100)") |
| 94 |
|
| 95 |
def test_type_table_typedef(): |
| 96 |
check_type_table("typedef int foo_t;", |
| 97 |
"(PRIMITIVE 7)") |
| 98 |
|
| 99 |
def test_type_table_prebuilt_type(): |
| 100 |
check_type_table("int32_t f(void);", |
| 101 |
"(FUNCTION 2)(FUNCTION_END 0)(PRIMITIVE 21)") |
| 102 |
|
| 103 |
def test_type_table_struct_opaque(): |
| 104 |
check_type_table("struct foo_s;", |
| 105 |
"(STRUCT_UNION 0)") |
| 106 |
|
| 107 |
def test_type_table_struct(): |
| 108 |
check_type_table("struct foo_s { int a; long b; };", |
| 109 |
"(PRIMITIVE 7)(PRIMITIVE 9)(STRUCT_UNION 0)") |
| 110 |
|
| 111 |
def test_type_table_union(): |
| 112 |
check_type_table("union foo_u { int a; long b; };", |
| 113 |
"(PRIMITIVE 7)(PRIMITIVE 9)(STRUCT_UNION 0)") |
| 114 |
|
| 115 |
def test_type_table_struct_used(): |
| 116 |
check_type_table("struct foo_s { int a; long b; }; int f(struct foo_s*);", |
| 117 |
"(FUNCTION 3)(POINTER 5)(FUNCTION_END 0)" |
| 118 |
"(PRIMITIVE 7)(PRIMITIVE 9)" |
| 119 |
"(STRUCT_UNION 0)") |
| 120 |
|
| 121 |
def test_type_table_anonymous_struct_with_typedef(): |
| 122 |
check_type_table("typedef struct { int a; long b; } foo_t;", |
| 123 |
"(STRUCT_UNION 0)(PRIMITIVE 7)(PRIMITIVE 9)") |
| 124 |
|
| 125 |
def test_type_table_enum(): |
| 126 |
check_type_table("enum foo_e { AA, BB, ... };", |
| 127 |
"(ENUM 0)") |
| 128 |
|
| 129 |
def test_type_table_include_1(): |
| 130 |
check_type_table("foo_t sin(foo_t);", |
| 131 |
"(FUNCTION 1)(PRIMITIVE 14)(FUNCTION_END 0)", |
| 132 |
included="typedef double foo_t;") |
| 133 |
|
| 134 |
def test_type_table_include_2(): |
| 135 |
check_type_table("struct foo_s *sin(struct foo_s *);", |
| 136 |
"(FUNCTION 1)(POINTER 3)(FUNCTION_END 0)(STRUCT_UNION 0)", |
| 137 |
included="struct foo_s { int x, y; };") |
| 138 |
|
| 139 |
|
| 140 |
def test_math_sin(): |
| 141 |
import math |
| 142 |
ffi = FFI() |
| 143 |
ffi.cdef("float sin(double); double cos(double);") |
| 144 |
lib = verify(ffi, 'test_math_sin', '#include <math.h>', |
| 145 |
ignore_warnings=True) |
| 146 |
assert lib.cos(1.43) == math.cos(1.43) |
| 147 |
|
| 148 |
def test_repr_lib(): |
| 149 |
ffi = FFI() |
| 150 |
lib = verify(ffi, 'test_repr_lib', '') |
| 151 |
assert repr(lib) == "<Lib object for '_CFFI_test_repr_lib'>" |
| 152 |
|
| 153 |
def test_funcarg_ptr(): |
| 154 |
ffi = FFI() |
| 155 |
ffi.cdef("int foo(int *);") |
| 156 |
lib = verify(ffi, 'test_funcarg_ptr', 'int foo(int *p) { return *p; }') |
| 157 |
assert lib.foo([-12345]) == -12345 |
| 158 |
|
| 159 |
def test_funcres_ptr(): |
| 160 |
ffi = FFI() |
| 161 |
ffi.cdef("int *foo(void);") |
| 162 |
lib = verify(ffi, 'test_funcres_ptr', |
| 163 |
'int *foo(void) { static int x=-12345; return &x; }') |
| 164 |
assert lib.foo()[0] == -12345 |
| 165 |
|
| 166 |
def test_global_var_array(): |
| 167 |
ffi = FFI() |
| 168 |
ffi.cdef("extern int a[100];") |
| 169 |
lib = verify(ffi, 'test_global_var_array', 'int a[100] = { 9999 };') |
| 170 |
lib.a[42] = 123456 |
| 171 |
assert lib.a[42] == 123456 |
| 172 |
assert lib.a[0] == 9999 |
| 173 |
|
| 174 |
def test_verify_typedef(): |
| 175 |
ffi = FFI() |
| 176 |
ffi.cdef("typedef int **foo_t;") |
| 177 |
lib = verify(ffi, 'test_verify_typedef', 'typedef int **foo_t;') |
| 178 |
assert ffi.sizeof("foo_t") == ffi.sizeof("void *") |
| 179 |
|
| 180 |
def test_verify_typedef_dotdotdot(): |
| 181 |
ffi = FFI() |
| 182 |
ffi.cdef("typedef ... foo_t;") |
| 183 |
verify(ffi, 'test_verify_typedef_dotdotdot', 'typedef int **foo_t;') |
| 184 |
|
| 185 |
def test_verify_typedef_star_dotdotdot(): |
| 186 |
ffi = FFI() |
| 187 |
ffi.cdef("typedef ... *foo_t;") |
| 188 |
verify(ffi, 'test_verify_typedef_star_dotdotdot', 'typedef int **foo_t;') |
| 189 |
|
| 190 |
def test_global_var_int(): |
| 191 |
ffi = FFI() |
| 192 |
ffi.cdef("extern int a, b, c;") |
| 193 |
lib = verify(ffi, 'test_global_var_int', 'int a = 999, b, c;') |
| 194 |
assert lib.a == 999 |
| 195 |
lib.a -= 1001 |
| 196 |
assert lib.a == -2 |
| 197 |
lib.a = -2147483648 |
| 198 |
assert lib.a == -2147483648 |
| 199 |
with pytest.raises(OverflowError): |
| 200 |
lib.a = 2147483648 |
| 201 |
with pytest.raises(OverflowError): |
| 202 |
lib.a = -2147483649 |
| 203 |
lib.b = 525 |
| 204 |
assert lib.b == 525 |
| 205 |
with pytest.raises(AttributeError): |
| 206 |
del lib.a |
| 207 |
with pytest.raises(AttributeError): |
| 208 |
del lib.c |
| 209 |
with pytest.raises(AttributeError): |
| 210 |
del lib.foobarbaz |
| 211 |
|
| 212 |
def test_macro(): |
| 213 |
ffi = FFI() |
| 214 |
ffi.cdef("#define FOOBAR ...") |
| 215 |
lib = verify(ffi, 'test_macro', "#define FOOBAR (-6912)") |
| 216 |
assert lib.FOOBAR == -6912 |
| 217 |
with pytest.raises(AttributeError): |
| 218 |
lib.FOOBAR = 2 |
| 219 |
|
| 220 |
def test_macro_check_value(): |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
vals = ['42', '-42', '0x80000000', '-2147483648', |
| 226 |
'0', '9223372036854775809ULL', |
| 227 |
'-9223372036854775807LL'] |
| 228 |
if sys.maxsize <= 2**32 or sys.platform == 'win32': |
| 229 |
vals.remove('-2147483648') |
| 230 |
ffi = FFI() |
| 231 |
cdef_lines = ['#define FOO_%d_%d %s' % (i, j, vals[i]) |
| 232 |
for i in range(len(vals)) |
| 233 |
for j in range(len(vals))] |
| 234 |
ffi.cdef('\n'.join(cdef_lines)) |
| 235 |
|
| 236 |
verify_lines = ['#define FOO_%d_%d %s' % (i, j, vals[j]) |
| 237 |
for i in range(len(vals)) |
| 238 |
for j in range(len(vals))] |
| 239 |
lib = verify(ffi, 'test_macro_check_value_ok', |
| 240 |
'\n'.join(verify_lines)) |
| 241 |
|
| 242 |
for j in range(len(vals)): |
| 243 |
c_got = int(vals[j].replace('U', '').replace('L', ''), 0) |
| 244 |
c_compiler_msg = str(c_got) |
| 245 |
if c_got > 0: |
| 246 |
c_compiler_msg += ' (0x%x)' % (c_got,) |
| 247 |
|
| 248 |
for i in range(len(vals)): |
| 249 |
attrname = 'FOO_%d_%d' % (i, j) |
| 250 |
if i == j: |
| 251 |
x = getattr(lib, attrname) |
| 252 |
assert x == c_got |
| 253 |
else: |
| 254 |
e = pytest.raises(ffi.error, getattr, lib, attrname) |
| 255 |
assert str(e.value) == ( |
| 256 |
"the C compiler says '%s' is equal to " |
| 257 |
"%s, but the cdef disagrees" % (attrname, c_compiler_msg)) |
| 258 |
|
| 259 |
def test_constant(): |
| 260 |
ffi = FFI() |
| 261 |
ffi.cdef("static const int FOOBAR;") |
| 262 |
lib = verify(ffi, 'test_constant', "#define FOOBAR (-6912)") |
| 263 |
assert lib.FOOBAR == -6912 |
| 264 |
with pytest.raises(AttributeError): |
| 265 |
lib.FOOBAR = 2 |
| 266 |
|
| 267 |
def test_check_value_of_static_const(): |
| 268 |
ffi = FFI() |
| 269 |
ffi.cdef("static const int FOOBAR = 042;") |
| 270 |
lib = verify(ffi, 'test_check_value_of_static_const', |
| 271 |
"#define FOOBAR (-6912)") |
| 272 |
e = pytest.raises(ffi.error, getattr, lib, 'FOOBAR') |
| 273 |
assert str(e.value) == ( |
| 274 |
"the C compiler says 'FOOBAR' is equal to -6912, but the cdef disagrees") |
| 275 |
|
| 276 |
def test_constant_nonint(): |
| 277 |
ffi = FFI() |
| 278 |
ffi.cdef("static const double FOOBAR;") |
| 279 |
lib = verify(ffi, 'test_constant_nonint', "#define FOOBAR (-6912.5)") |
| 280 |
assert lib.FOOBAR == -6912.5 |
| 281 |
with pytest.raises(AttributeError): |
| 282 |
lib.FOOBAR = 2 |
| 283 |
|
| 284 |
def test_constant_ptr(): |
| 285 |
ffi = FFI() |
| 286 |
ffi.cdef("static double *const FOOBAR;") |
| 287 |
lib = verify(ffi, 'test_constant_ptr', "#define FOOBAR NULL") |
| 288 |
assert lib.FOOBAR == ffi.NULL |
| 289 |
assert ffi.typeof(lib.FOOBAR) == ffi.typeof("double *") |
| 290 |
|
| 291 |
def test_dir(): |
| 292 |
ffi = FFI() |
| 293 |
ffi.cdef("int ff(int); extern int aa; static const int my_constant;") |
| 294 |
lib = verify(ffi, 'test_dir', """ |
| 295 |
#define my_constant (-45) |
| 296 |
int aa; |
| 297 |
int ff(int x) { return x+aa; } |
| 298 |
""") |
| 299 |
lib.aa = 5 |
| 300 |
assert dir(lib) == ['aa', 'ff', 'my_constant'] |
| 301 |
|
| 302 |
aaobj = lib.__dict__['aa'] |
| 303 |
assert not isinstance(aaobj, int) |
| 304 |
assert lib.__dict__ == { |
| 305 |
'ff': lib.ff, |
| 306 |
'aa': aaobj, |
| 307 |
'my_constant': -45} |
| 308 |
lib.__dict__['ff'] = "??" |
| 309 |
assert lib.ff(10) == 15 |
| 310 |
|
| 311 |
def test_verify_opaque_struct(): |
| 312 |
ffi = FFI() |
| 313 |
ffi.cdef("struct foo_s;") |
| 314 |
lib = verify(ffi, 'test_verify_opaque_struct', "struct foo_s;") |
| 315 |
assert ffi.typeof("struct foo_s").cname == "struct foo_s" |
| 316 |
|
| 317 |
def test_verify_opaque_union(): |
| 318 |
ffi = FFI() |
| 319 |
ffi.cdef("union foo_s;") |
| 320 |
lib = verify(ffi, 'test_verify_opaque_union', "union foo_s;") |
| 321 |
assert ffi.typeof("union foo_s").cname == "union foo_s" |
| 322 |
|
| 323 |
def test_verify_struct(): |
| 324 |
ffi = FFI() |
| 325 |
ffi.cdef("""struct foo_s { int b; short a; ...; }; |
| 326 |
struct bar_s { struct foo_s *f; };""") |
| 327 |
lib = verify(ffi, 'test_verify_struct', |
| 328 |
"""struct foo_s { short a; int b; }; |
| 329 |
struct bar_s { struct foo_s *f; };""") |
| 330 |
ffi.typeof("struct bar_s *") |
| 331 |
p = ffi.new("struct foo_s *", {'a': -32768, 'b': -2147483648}) |
| 332 |
assert p.a == -32768 |
| 333 |
assert p.b == -2147483648 |
| 334 |
with pytest.raises(OverflowError): |
| 335 |
p.a -= 1 |
| 336 |
with pytest.raises(OverflowError): |
| 337 |
p.b -= 1 |
| 338 |
q = ffi.new("struct bar_s *", {'f': p}) |
| 339 |
assert q.f == p |
| 340 |
|
| 341 |
assert ffi.offsetof("struct foo_s", "a") == 0 |
| 342 |
assert ffi.offsetof("struct foo_s", "b") == 4 |
| 343 |
assert ffi.offsetof(u+"struct foo_s", u+"b") == 4 |
| 344 |
|
| 345 |
pytest.raises(TypeError, ffi.addressof, p) |
| 346 |
assert ffi.addressof(p[0]) == p |
| 347 |
assert ffi.typeof(ffi.addressof(p[0])) is ffi.typeof("struct foo_s *") |
| 348 |
assert ffi.typeof(ffi.addressof(p, "b")) is ffi.typeof("int *") |
| 349 |
assert ffi.addressof(p, "b")[0] == p.b |
| 350 |
|
| 351 |
def test_verify_exact_field_offset(): |
| 352 |
ffi = FFI() |
| 353 |
ffi.cdef("""struct foo_s { int b; short a; };""") |
| 354 |
lib = verify(ffi, 'test_verify_exact_field_offset', |
| 355 |
"""struct foo_s { short a; int b; };""") |
| 356 |
e = pytest.raises(ffi.error, ffi.new, "struct foo_s *", []) |
| 357 |
assert str(e.value).startswith( |
| 358 |
"struct foo_s: wrong offset for field 'b' (cdef " |
| 359 |
'says 0, but C compiler says 4). fix it or use "...;" ') |
| 360 |
|
| 361 |
def test_type_caching(): |
| 362 |
ffi1 = FFI(); ffi1.cdef("struct foo_s;") |
| 363 |
ffi2 = FFI(); ffi2.cdef("struct foo_s;") |
| 364 |
lib1 = verify(ffi1, 'test_type_caching_1', 'struct foo_s;') |
| 365 |
lib2 = verify(ffi2, 'test_type_caching_2', 'struct foo_s;') |
| 366 |
|
| 367 |
assert ffi1.typeof("long") is ffi2.typeof("long") |
| 368 |
assert ffi1.typeof("long**") is ffi2.typeof("long * *") |
| 369 |
assert ffi1.typeof("long(*)(int, ...)") is ffi2.typeof("long(*)(int, ...)") |
| 370 |
|
| 371 |
assert ffi1.typeof("struct foo_s") is not ffi2.typeof("struct foo_s") |
| 372 |
assert ffi1.typeof("struct foo_s *") is not ffi2.typeof("struct foo_s *") |
| 373 |
assert ffi1.typeof("struct foo_s*(*)()") is not ( |
| 374 |
ffi2.typeof("struct foo_s*(*)()")) |
| 375 |
assert ffi1.typeof("void(*)(struct foo_s*)") is not ( |
| 376 |
ffi2.typeof("void(*)(struct foo_s*)")) |
| 377 |
|
| 378 |
def test_verify_enum(): |
| 379 |
ffi = FFI() |
| 380 |
ffi.cdef("""enum e1 { B1, A1, ... }; enum e2 { B2, A2, ... };""") |
| 381 |
lib = verify(ffi, 'test_verify_enum', |
| 382 |
"enum e1 { A1, B1, C1=%d };" % sys.maxsize + |
| 383 |
"enum e2 { A2, B2, C2 };") |
| 384 |
ffi.typeof("enum e1") |
| 385 |
ffi.typeof("enum e2") |
| 386 |
assert lib.A1 == 0 |
| 387 |
assert lib.B1 == 1 |
| 388 |
assert lib.A2 == 0 |
| 389 |
assert lib.B2 == 1 |
| 390 |
assert ffi.sizeof("enum e1") == ffi.sizeof("long") |
| 391 |
assert ffi.sizeof("enum e2") == ffi.sizeof("int") |
| 392 |
assert repr(ffi.cast("enum e1", 0)) == "<cdata 'enum e1' 0: A1>" |
| 393 |
|
| 394 |
def test_duplicate_enum(): |
| 395 |
ffi = FFI() |
| 396 |
ffi.cdef("enum e1 { A1, ... }; enum e2 { A1, ... };") |
| 397 |
pytest.raises(VerificationError, verify, ffi, 'test_duplicate_enum', |
| 398 |
"enum e1 { A1 }; enum e2 { B1 };") |
| 399 |
|
| 400 |
def test_dotdotdot_length_of_array_field(): |
| 401 |
ffi = FFI() |
| 402 |
ffi.cdef("struct foo_s { int a[...]; int b[...]; };") |
| 403 |
verify(ffi, 'test_dotdotdot_length_of_array_field', |
| 404 |
"struct foo_s { int a[42]; int b[11]; };") |
| 405 |
assert ffi.sizeof("struct foo_s") == (42 + 11) * 4 |
| 406 |
p = ffi.new("struct foo_s *") |
| 407 |
assert p.a[41] == p.b[10] == 0 |
| 408 |
with pytest.raises(IndexError): |
| 409 |
p.a[42] |
| 410 |
with pytest.raises(IndexError): |
| 411 |
p.b[11] |
| 412 |
|
| 413 |
def test_dotdotdot_global_array(): |
| 414 |
ffi = FFI() |
| 415 |
ffi.cdef("extern int aa[...]; extern int bb[...];") |
| 416 |
lib = verify(ffi, 'test_dotdotdot_global_array', |
| 417 |
"int aa[41]; int bb[12];") |
| 418 |
assert ffi.sizeof(lib.aa) == 41 * 4 |
| 419 |
assert ffi.sizeof(lib.bb) == 12 * 4 |
| 420 |
assert lib.aa[40] == lib.bb[11] == 0 |
| 421 |
with pytest.raises(IndexError): |
| 422 |
lib.aa[41] |
| 423 |
with pytest.raises(IndexError): |
| 424 |
lib.bb[12] |
| 425 |
|
| 426 |
def test_misdeclared_field_1(): |
| 427 |
ffi = FFI() |
| 428 |
ffi.cdef("struct foo_s { int a[5]; };") |
| 429 |
try: |
| 430 |
verify(ffi, 'test_misdeclared_field_1', |
| 431 |
"struct foo_s { int a[6]; };") |
| 432 |
except VerificationError: |
| 433 |
pass |
| 434 |
else: |
| 435 |
assert ffi.sizeof("struct foo_s") == 24 |
| 436 |
try: |
| 437 |
|
| 438 |
p = ffi.new("struct foo_s *") |
| 439 |
p.a |
| 440 |
assert False, "should have raised" |
| 441 |
except ffi.error as e: |
| 442 |
assert str(e).startswith("struct foo_s: wrong size for field 'a' " |
| 443 |
"(cdef says 20, but C compiler says 24)") |
| 444 |
|
| 445 |
def test_open_array_in_struct(): |
| 446 |
ffi = FFI() |
| 447 |
ffi.cdef("struct foo_s { int b; int a[]; };") |
| 448 |
verify(ffi, 'test_open_array_in_struct', |
| 449 |
"struct foo_s { int b; int a[]; };") |
| 450 |
assert ffi.sizeof("struct foo_s") == 4 |
| 451 |
p = ffi.new("struct foo_s *", [5, [10, 20, 30, 40]]) |
| 452 |
assert p.a[2] == 30 |
| 453 |
assert ffi.sizeof(p) == ffi.sizeof("void *") |
| 454 |
assert ffi.sizeof(p[0]) == 5 * ffi.sizeof("int") |
| 455 |
|
| 456 |
def test_math_sin_type(): |
| 457 |
ffi = FFI() |
| 458 |
ffi.cdef("double sin(double); void *xxtestfunc();") |
| 459 |
lib = verify(ffi, 'test_math_sin_type', """ |
| 460 |
#include <math.h> |
| 461 |
void *xxtestfunc(void) { return 0; } |
| 462 |
""") |
| 463 |
|
| 464 |
assert ffi.typeof(lib.sin).cname == "double(*)(double)" |
| 465 |
|
| 466 |
x = type(lib).__dir__.__get__(lib) |
| 467 |
pytest.raises(TypeError, ffi.typeof, x) |
| 468 |
|
| 469 |
|
| 470 |
assert lib.sin.__name__ == 'sin' |
| 471 |
assert lib.sin.__module__ == '_CFFI_test_math_sin_type' |
| 472 |
assert lib.sin.__doc__ == ( |
| 473 |
"double sin(double);\n" |
| 474 |
"\n" |
| 475 |
"CFFI C function from _CFFI_test_math_sin_type.lib") |
| 476 |
|
| 477 |
assert ffi.typeof(lib.xxtestfunc).cname == "void *(*)()" |
| 478 |
assert lib.xxtestfunc.__doc__ == ( |
| 479 |
"void *xxtestfunc();\n" |
| 480 |
"\n" |
| 481 |
"CFFI C function from _CFFI_test_math_sin_type.lib") |
| 482 |
|
| 483 |
def test_verify_anonymous_struct_with_typedef(): |
| 484 |
ffi = FFI() |
| 485 |
ffi.cdef("typedef struct { int a; long b; ...; } foo_t;") |
| 486 |
verify(ffi, 'test_verify_anonymous_struct_with_typedef', |
| 487 |
"typedef struct { long b; int hidden, a; } foo_t;") |
| 488 |
p = ffi.new("foo_t *", {'b': 42}) |
| 489 |
assert p.b == 42 |
| 490 |
assert repr(p).startswith("<cdata 'foo_t *' ") |
| 491 |
|
| 492 |
def test_verify_anonymous_struct_with_star_typedef(): |
| 493 |
ffi = FFI() |
| 494 |
ffi.cdef("typedef struct { int a; long b; } *foo_t;") |
| 495 |
verify(ffi, 'test_verify_anonymous_struct_with_star_typedef', |
| 496 |
"typedef struct { int a; long b; } *foo_t;") |
| 497 |
p = ffi.new("foo_t", {'b': 42}) |
| 498 |
assert p.b == 42 |
| 499 |
|
| 500 |
def test_verify_anonymous_enum_with_typedef(): |
| 501 |
ffi = FFI() |
| 502 |
ffi.cdef("typedef enum { AA, ... } e1;") |
| 503 |
lib = verify(ffi, 'test_verify_anonymous_enum_with_typedef1', |
| 504 |
"typedef enum { BB, CC, AA } e1;") |
| 505 |
assert lib.AA == 2 |
| 506 |
assert ffi.sizeof("e1") == ffi.sizeof("int") |
| 507 |
assert repr(ffi.cast("e1", 2)) == "<cdata 'e1' 2: AA>" |
| 508 |
|
| 509 |
ffi = FFI() |
| 510 |
ffi.cdef("typedef enum { AA=%d } e1;" % sys.maxsize) |
| 511 |
lib = verify(ffi, 'test_verify_anonymous_enum_with_typedef2', |
| 512 |
"typedef enum { AA=%d } e1;" % sys.maxsize) |
| 513 |
assert lib.AA == int(ffi.cast("long", sys.maxsize)) |
| 514 |
assert ffi.sizeof("e1") == ffi.sizeof("long") |
| 515 |
|
| 516 |
def test_unique_types(): |
| 517 |
CDEF = "struct foo_s; union foo_u; enum foo_e { AA };" |
| 518 |
ffi1 = FFI(); ffi1.cdef(CDEF); verify(ffi1, "test_unique_types_1", CDEF) |
| 519 |
ffi2 = FFI(); ffi2.cdef(CDEF); verify(ffi2, "test_unique_types_2", CDEF) |
| 520 |
|
| 521 |
assert ffi1.typeof("char") is ffi2.typeof("char ") |
| 522 |
assert ffi1.typeof("long") is ffi2.typeof("signed long int") |
| 523 |
assert ffi1.typeof("double *") is ffi2.typeof("double*") |
| 524 |
assert ffi1.typeof("int ***") is ffi2.typeof(" int * * *") |
| 525 |
assert ffi1.typeof("int[]") is ffi2.typeof("signed int[]") |
| 526 |
assert ffi1.typeof("signed int*[17]") is ffi2.typeof("int *[17]") |
| 527 |
assert ffi1.typeof("void") is ffi2.typeof("void") |
| 528 |
assert ffi1.typeof("int(*)(int,int)") is ffi2.typeof("int(*)(int,int)") |
| 529 |
|
| 530 |
|
| 531 |
for name in ["struct foo_s", |
| 532 |
"union foo_u *", |
| 533 |
"enum foo_e", |
| 534 |
"struct foo_s *(*)()", |
| 535 |
"void(*)(struct foo_s *)", |
| 536 |
"struct foo_s *(*[5])[8]", |
| 537 |
]: |
| 538 |
assert ffi1.typeof(name) is not ffi2.typeof(name) |
| 539 |
|
| 540 |
assert ffi1.typeof("struct foo_s*") is ffi1.typeof("struct foo_s *") |
| 541 |
|
| 542 |
def test_module_name_in_package(): |
| 543 |
ffi = FFI() |
| 544 |
ffi.cdef("int foo(int);") |
| 545 |
recompiler.recompile(ffi, "test_module_name_in_package.mymod", |
| 546 |
"int foo(int x) { return x + 32; }", |
| 547 |
tmpdir=str(udir)) |
| 548 |
old_sys_path = sys.path[:] |
| 549 |
try: |
| 550 |
package_dir = udir.join('test_module_name_in_package') |
| 551 |
for name in os.listdir(str(udir)): |
| 552 |
assert not name.startswith('test_module_name_in_package.') |
| 553 |
assert os.path.isdir(str(package_dir)) |
| 554 |
assert len(os.listdir(str(package_dir))) > 0 |
| 555 |
assert os.path.exists(str(package_dir.join('mymod.c'))) |
| 556 |
package_dir.join('__init__.py').write('') |
| 557 |
|
| 558 |
getattr(importlib, 'invalidate_caches', object)() |
| 559 |
|
| 560 |
sys.path.insert(0, str(udir)) |
| 561 |
import test_module_name_in_package.mymod |
| 562 |
assert test_module_name_in_package.mymod.lib.foo(10) == 42 |
| 563 |
assert test_module_name_in_package.mymod.__name__ == ( |
| 564 |
'test_module_name_in_package.mymod') |
| 565 |
finally: |
| 566 |
sys.path[:] = old_sys_path |
| 567 |
|
| 568 |
def test_bad_size_of_global_1(): |
| 569 |
ffi = FFI() |
| 570 |
ffi.cdef("extern short glob;") |
| 571 |
pytest.raises(VerificationError, verify, ffi, |
| 572 |
"test_bad_size_of_global_1", "long glob;") |
| 573 |
|
| 574 |
def test_bad_size_of_global_2(): |
| 575 |
ffi = FFI() |
| 576 |
ffi.cdef("extern int glob[10];") |
| 577 |
pytest.raises(VerificationError, verify, ffi, |
| 578 |
"test_bad_size_of_global_2", "int glob[9];") |
| 579 |
|
| 580 |
def test_unspecified_size_of_global_1(): |
| 581 |
ffi = FFI() |
| 582 |
ffi.cdef("extern int glob[];") |
| 583 |
lib = verify(ffi, "test_unspecified_size_of_global_1", "int glob[10];") |
| 584 |
assert ffi.typeof(lib.glob) == ffi.typeof("int *") |
| 585 |
|
| 586 |
def test_unspecified_size_of_global_2(): |
| 587 |
ffi = FFI() |
| 588 |
ffi.cdef("extern int glob[][5];") |
| 589 |
lib = verify(ffi, "test_unspecified_size_of_global_2", "int glob[10][5];") |
| 590 |
assert ffi.typeof(lib.glob) == ffi.typeof("int(*)[5]") |
| 591 |
|
| 592 |
def test_unspecified_size_of_global_3(): |
| 593 |
ffi = FFI() |
| 594 |
ffi.cdef("extern int glob[][...];") |
| 595 |
lib = verify(ffi, "test_unspecified_size_of_global_3", "int glob[10][5];") |
| 596 |
assert ffi.typeof(lib.glob) == ffi.typeof("int(*)[5]") |
| 597 |
|
| 598 |
def test_unspecified_size_of_global_4(): |
| 599 |
ffi = FFI() |
| 600 |
ffi.cdef("extern int glob[...][...];") |
| 601 |
lib = verify(ffi, "test_unspecified_size_of_global_4", "int glob[10][5];") |
| 602 |
assert ffi.typeof(lib.glob) == ffi.typeof("int[10][5]") |
| 603 |
|
| 604 |
def test_include_1(): |
| 605 |
ffi1 = FFI() |
| 606 |
ffi1.cdef("typedef double foo_t;") |
| 607 |
verify(ffi1, "test_include_1_parent", "typedef double foo_t;") |
| 608 |
ffi = FFI() |
| 609 |
ffi.include(ffi1) |
| 610 |
ffi.cdef("foo_t ff1(foo_t);") |
| 611 |
lib = verify(ffi, "test_include_1", "double ff1(double x) { return 42.5; }") |
| 612 |
assert lib.ff1(0) == 42.5 |
| 613 |
assert ffi1.typeof("foo_t") is ffi.typeof("foo_t") is ffi.typeof("double") |
| 614 |
|
| 615 |
def test_include_1b(): |
| 616 |
ffi1 = FFI() |
| 617 |
ffi1.cdef("int foo1(int);") |
| 618 |
lib1 = verify(ffi1, "test_include_1b_parent", |
| 619 |
"int foo1(int x) { return x + 10; }") |
| 620 |
ffi = FFI() |
| 621 |
ffi.include(ffi1) |
| 622 |
ffi.cdef("int foo2(int);") |
| 623 |
lib = verify(ffi, "test_include_1b", "int foo2(int x) { return x - 5; }") |
| 624 |
assert lib.foo2(42) == 37 |
| 625 |
assert lib.foo1(42) == 52 |
| 626 |
assert lib.foo1 is lib1.foo1 |
| 627 |
|
| 628 |
def test_include_2(): |
| 629 |
ffi1 = FFI() |
| 630 |
ffi1.cdef("struct foo_s { int x, y; };") |
| 631 |
verify(ffi1, "test_include_2_parent", "struct foo_s { int x, y; };") |
| 632 |
ffi = FFI() |
| 633 |
ffi.include(ffi1) |
| 634 |
ffi.cdef("struct foo_s *ff2(struct foo_s *);") |
| 635 |
lib = verify(ffi, "test_include_2", |
| 636 |
"struct foo_s { int x, y; }; //usually from a #include\n" |
| 637 |
"struct foo_s *ff2(struct foo_s *p) { p->y++; return p; }") |
| 638 |
p = ffi.new("struct foo_s *") |
| 639 |
p.y = 41 |
| 640 |
q = lib.ff2(p) |
| 641 |
assert q == p |
| 642 |
assert p.y == 42 |
| 643 |
assert ffi1.typeof("struct foo_s") is ffi.typeof("struct foo_s") |
| 644 |
|
| 645 |
def test_include_3(): |
| 646 |
ffi1 = FFI() |
| 647 |
ffi1.cdef("typedef short sshort_t;") |
| 648 |
verify(ffi1, "test_include_3_parent", "typedef short sshort_t;") |
| 649 |
ffi = FFI() |
| 650 |
ffi.include(ffi1) |
| 651 |
ffi.cdef("sshort_t ff3(sshort_t);") |
| 652 |
lib = verify(ffi, "test_include_3", |
| 653 |
"typedef short sshort_t; //usually from a #include\n" |
| 654 |
"sshort_t ff3(sshort_t x) { return (sshort_t)(x + 42); }") |
| 655 |
assert lib.ff3(10) == 52 |
| 656 |
assert ffi.typeof(ffi.cast("sshort_t", 42)) is ffi.typeof("short") |
| 657 |
assert ffi1.typeof("sshort_t") is ffi.typeof("sshort_t") |
| 658 |
|
| 659 |
def test_include_4(): |
| 660 |
ffi1 = FFI() |
| 661 |
ffi1.cdef("typedef struct { int x; } mystruct_t;") |
| 662 |
verify(ffi1, "test_include_4_parent", |
| 663 |
"typedef struct { int x; } mystruct_t;") |
| 664 |
ffi = FFI() |
| 665 |
ffi.include(ffi1) |
| 666 |
ffi.cdef("mystruct_t *ff4(mystruct_t *);") |
| 667 |
lib = verify(ffi, "test_include_4", |
| 668 |
"typedef struct {int x; } mystruct_t; //usually from a #include\n" |
| 669 |
"mystruct_t *ff4(mystruct_t *p) { p->x += 42; return p; }") |
| 670 |
p = ffi.new("mystruct_t *", [10]) |
| 671 |
q = lib.ff4(p) |
| 672 |
assert q == p |
| 673 |
assert p.x == 52 |
| 674 |
assert ffi1.typeof("mystruct_t") is ffi.typeof("mystruct_t") |
| 675 |
|
| 676 |
def test_include_5(): |
| 677 |
ffi1 = FFI() |
| 678 |
ffi1.cdef("typedef struct { int x[2]; int y; } *mystruct_p;") |
| 679 |
verify(ffi1, "test_include_5_parent", |
| 680 |
"typedef struct { int x[2]; int y; } *mystruct_p;") |
| 681 |
ffi = FFI() |
| 682 |
ffi.include(ffi1) |
| 683 |
ffi.cdef("mystruct_p ff5(mystruct_p);") |
| 684 |
lib = verify(ffi, "test_include_5", |
| 685 |
"typedef struct {int x[2]; int y; } *mystruct_p; //usually #include\n" |
| 686 |
"mystruct_p ff5(mystruct_p p) { p->x[1] += 42; return p; }") |
| 687 |
assert ffi.alignof(ffi.typeof("mystruct_p").item) == 4 |
| 688 |
assert ffi1.typeof("mystruct_p") is ffi.typeof("mystruct_p") |
| 689 |
p = ffi.new("mystruct_p", [[5, 10], -17]) |
| 690 |
q = lib.ff5(p) |
| 691 |
assert q == p |
| 692 |
assert p.x[0] == 5 |
| 693 |
assert p.x[1] == 52 |
| 694 |
assert p.y == -17 |
| 695 |
assert ffi.alignof(ffi.typeof(p[0])) == 4 |
| 696 |
|
| 697 |
def test_include_6(): |
| 698 |
ffi1 = FFI() |
| 699 |
ffi1.cdef("typedef ... mystruct_t;") |
| 700 |
verify(ffi1, "test_include_6_parent", |
| 701 |
"typedef struct _mystruct_s mystruct_t;") |
| 702 |
ffi = FFI() |
| 703 |
ffi.include(ffi1) |
| 704 |
ffi.cdef("mystruct_t *ff6(void); int ff6b(mystruct_t *);") |
| 705 |
lib = verify(ffi, "test_include_6", |
| 706 |
"typedef struct _mystruct_s mystruct_t; //usually from a #include\n" |
| 707 |
"struct _mystruct_s { int x; };\n" |
| 708 |
"static mystruct_t result_struct = { 42 };\n" |
| 709 |
"mystruct_t *ff6(void) { return &result_struct; }\n" |
| 710 |
"int ff6b(mystruct_t *p) { return p->x; }") |
| 711 |
p = lib.ff6() |
| 712 |
assert ffi.cast("int *", p)[0] == 42 |
| 713 |
assert lib.ff6b(p) == 42 |
| 714 |
|
| 715 |
def test_include_7(): |
| 716 |
ffi1 = FFI() |
| 717 |
ffi1.cdef("typedef ... mystruct_t;\n" |
| 718 |
"int ff7b(mystruct_t *);") |
| 719 |
verify(ffi1, "test_include_7_parent", |
| 720 |
"typedef struct { int x; } mystruct_t;\n" |
| 721 |
"int ff7b(mystruct_t *p) { return p->x; }") |
| 722 |
ffi = FFI() |
| 723 |
ffi.include(ffi1) |
| 724 |
ffi.cdef("mystruct_t *ff7(void);") |
| 725 |
lib = verify(ffi, "test_include_7", |
| 726 |
"typedef struct { int x; } mystruct_t; //usually from a #include\n" |
| 727 |
"static mystruct_t result_struct = { 42 };" |
| 728 |
"mystruct_t *ff7(void) { return &result_struct; }") |
| 729 |
p = lib.ff7() |
| 730 |
assert ffi.cast("int *", p)[0] == 42 |
| 731 |
assert lib.ff7b(p) == 42 |
| 732 |
|
| 733 |
def test_include_8(): |
| 734 |
ffi1 = FFI() |
| 735 |
ffi1.cdef("struct foo_s;") |
| 736 |
verify(ffi1, "test_include_8_parent", "struct foo_s;") |
| 737 |
ffi = FFI() |
| 738 |
ffi.include(ffi1) |
| 739 |
ffi.cdef("struct foo_s { int x, y; };") |
| 740 |
verify(ffi, "test_include_8", "struct foo_s { int x, y; };") |
| 741 |
e = pytest.raises(NotImplementedError, ffi.new, "struct foo_s *") |
| 742 |
assert str(e.value) == ( |
| 743 |
"'struct foo_s' is opaque in the ffi.include(), but no longer in " |
| 744 |
"the ffi doing the include (workaround: don't use ffi.include() but" |
| 745 |
" duplicate the declarations of everything using struct foo_s)") |
| 746 |
|
| 747 |
def test_unicode_libraries(): |
| 748 |
try: |
| 749 |
unicode |
| 750 |
except NameError: |
| 751 |
pytest.skip("for python 2.x") |
| 752 |
|
| 753 |
import math |
| 754 |
lib_m = "m" |
| 755 |
if sys.platform == 'win32': |
| 756 |
|
| 757 |
import distutils.ccompiler |
| 758 |
if distutils.ccompiler.get_default_compiler() == 'msvc': |
| 759 |
lib_m = 'msvcrt' |
| 760 |
ffi = FFI() |
| 761 |
ffi.cdef(unicode("float sin(double); double cos(double);")) |
| 762 |
lib = verify(ffi, 'test_math_sin_unicode', unicode('#include <math.h>'), |
| 763 |
libraries=[unicode(lib_m)], ignore_warnings=True) |
| 764 |
assert lib.cos(1.43) == math.cos(1.43) |
| 765 |
|
| 766 |
def test_incomplete_struct_as_arg(): |
| 767 |
ffi = FFI() |
| 768 |
ffi.cdef("struct foo_s { int x; ...; }; int f(int, struct foo_s);") |
| 769 |
lib = verify(ffi, "test_incomplete_struct_as_arg", |
| 770 |
"struct foo_s { int a, x, z; };\n" |
| 771 |
"int f(int b, struct foo_s s) { return s.x * b; }") |
| 772 |
s = ffi.new("struct foo_s *", [21]) |
| 773 |
assert s.x == 21 |
| 774 |
assert ffi.sizeof(s[0]) == 12 |
| 775 |
assert ffi.offsetof(ffi.typeof(s), 'x') == 4 |
| 776 |
assert lib.f(2, s[0]) == 42 |
| 777 |
assert ffi.typeof(lib.f) == ffi.typeof("int(*)(int, struct foo_s)") |
| 778 |
|
| 779 |
def test_incomplete_struct_as_result(): |
| 780 |
ffi = FFI() |
| 781 |
ffi.cdef("struct foo_s { int x; ...; }; struct foo_s f(int);") |
| 782 |
lib = verify(ffi, "test_incomplete_struct_as_result", |
| 783 |
"struct foo_s { int a, x, z; };\n" |
| 784 |
"struct foo_s f(int x) { struct foo_s r; r.x = x * 2; return r; }") |
| 785 |
s = lib.f(21) |
| 786 |
assert s.x == 42 |
| 787 |
assert ffi.typeof(lib.f) == ffi.typeof("struct foo_s(*)(int)") |
| 788 |
|
| 789 |
def test_incomplete_struct_as_both(): |
| 790 |
ffi = FFI() |
| 791 |
ffi.cdef("struct foo_s { int x; ...; }; struct bar_s { int y; ...; };\n" |
| 792 |
"struct foo_s f(int, struct bar_s);") |
| 793 |
lib = verify(ffi, "test_incomplete_struct_as_both", |
| 794 |
"struct foo_s { int a, x, z; };\n" |
| 795 |
"struct bar_s { int b, c, y, d; };\n" |
| 796 |
"struct foo_s f(int x, struct bar_s b) {\n" |
| 797 |
" struct foo_s r; r.x = x * b.y; return r;\n" |
| 798 |
"}") |
| 799 |
b = ffi.new("struct bar_s *", [7]) |
| 800 |
s = lib.f(6, b[0]) |
| 801 |
assert s.x == 42 |
| 802 |
assert ffi.typeof(lib.f) == ffi.typeof( |
| 803 |
"struct foo_s(*)(int, struct bar_s)") |
| 804 |
s = lib.f(14, {'y': -3}) |
| 805 |
assert s.x == -42 |
| 806 |
|
| 807 |
def test_name_of_unnamed_struct(): |
| 808 |
ffi = FFI() |
| 809 |
ffi.cdef("typedef struct { int x; } foo_t;\n" |
| 810 |
"typedef struct { int y; } *bar_p;\n" |
| 811 |
"typedef struct { int y; } **baz_pp;\n") |
| 812 |
verify(ffi, "test_name_of_unnamed_struct", |
| 813 |
"typedef struct { int x; } foo_t;\n" |
| 814 |
"typedef struct { int y; } *bar_p;\n" |
| 815 |
"typedef struct { int y; } **baz_pp;\n") |
| 816 |
assert repr(ffi.typeof("foo_t")) == "<ctype 'foo_t'>" |
| 817 |
assert repr(ffi.typeof("bar_p")) == "<ctype 'struct $1 *'>" |
| 818 |
assert repr(ffi.typeof("baz_pp")) == "<ctype 'struct $2 * *'>" |
| 819 |
|
| 820 |
def test_address_of_global_var(): |
| 821 |
ffi = FFI() |
| 822 |
ffi.cdef(""" |
| 823 |
extern long bottom, bottoms[2]; |
| 824 |
long FetchRectBottom(void); |
| 825 |
long FetchRectBottoms1(void); |
| 826 |
#define FOOBAR 42 |
| 827 |
""") |
| 828 |
lib = verify(ffi, "test_address_of_global_var", """ |
| 829 |
long bottom, bottoms[2]; |
| 830 |
long FetchRectBottom(void) { return bottom; } |
| 831 |
long FetchRectBottoms1(void) { return bottoms[1]; } |
| 832 |
#define FOOBAR 42 |
| 833 |
""") |
| 834 |
lib.bottom = 300 |
| 835 |
assert lib.FetchRectBottom() == 300 |
| 836 |
lib.bottom += 1 |
| 837 |
assert lib.FetchRectBottom() == 301 |
| 838 |
lib.bottoms[1] = 500 |
| 839 |
assert lib.FetchRectBottoms1() == 500 |
| 840 |
lib.bottoms[1] += 2 |
| 841 |
assert lib.FetchRectBottoms1() == 502 |
| 842 |
|
| 843 |
p = ffi.addressof(lib, 'bottom') |
| 844 |
assert ffi.typeof(p) == ffi.typeof("long *") |
| 845 |
assert p[0] == 301 |
| 846 |
p[0] += 1 |
| 847 |
assert lib.FetchRectBottom() == 302 |
| 848 |
p = ffi.addressof(lib, 'bottoms') |
| 849 |
assert ffi.typeof(p) == ffi.typeof("long(*)[2]") |
| 850 |
assert p[0] == lib.bottoms |
| 851 |
|
| 852 |
pytest.raises(AttributeError, ffi.addressof, lib, 'unknown_var') |
| 853 |
pytest.raises(AttributeError, ffi.addressof, lib, "FOOBAR") |
| 854 |
|
| 855 |
def test_defines__CFFI_(): |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
ffi = FFI() |
| 860 |
ffi.cdef(""" |
| 861 |
#define CORRECT 1 |
| 862 |
""") |
| 863 |
lib = verify(ffi, "test_defines__CFFI_", """ |
| 864 |
#ifdef _CFFI_ |
| 865 |
# define CORRECT 1 |
| 866 |
#endif |
| 867 |
""") |
| 868 |
assert lib.CORRECT == 1 |
| 869 |
|
| 870 |
def test_unpack_args(): |
| 871 |
ffi = FFI() |
| 872 |
ffi.cdef("void foo0(void); void foo1(int); void foo2(int, int);") |
| 873 |
lib = verify(ffi, "test_unpack_args", """ |
| 874 |
void foo0(void) { } |
| 875 |
void foo1(int x) { } |
| 876 |
void foo2(int x, int y) { } |
| 877 |
""") |
| 878 |
assert 'foo0' in repr(lib.foo0) |
| 879 |
assert 'foo1' in repr(lib.foo1) |
| 880 |
assert 'foo2' in repr(lib.foo2) |
| 881 |
lib.foo0() |
| 882 |
lib.foo1(42) |
| 883 |
lib.foo2(43, 44) |
| 884 |
e1 = pytest.raises(TypeError, lib.foo0, 42) |
| 885 |
e2 = pytest.raises(TypeError, lib.foo0, 43, 44) |
| 886 |
e3 = pytest.raises(TypeError, lib.foo1) |
| 887 |
e4 = pytest.raises(TypeError, lib.foo1, 43, 44) |
| 888 |
e5 = pytest.raises(TypeError, lib.foo2) |
| 889 |
e6 = pytest.raises(TypeError, lib.foo2, 42) |
| 890 |
e7 = pytest.raises(TypeError, lib.foo2, 45, 46, 47) |
| 891 |
def st1(s): |
| 892 |
s = str(s) |
| 893 |
if s.startswith("_CFFI_test_unpack_args.Lib."): |
| 894 |
s = s[len("_CFFI_test_unpack_args.Lib."):] |
| 895 |
return s |
| 896 |
assert st1(e1.value) == "foo0() takes no arguments (1 given)" |
| 897 |
assert st1(e2.value) == "foo0() takes no arguments (2 given)" |
| 898 |
assert st1(e3.value) == "foo1() takes exactly one argument (0 given)" |
| 899 |
assert st1(e4.value) == "foo1() takes exactly one argument (2 given)" |
| 900 |
assert st1(e5.value) in ["foo2 expected 2 arguments, got 0", |
| 901 |
"foo2() takes exactly 2 arguments (0 given)"] |
| 902 |
assert st1(e6.value) in ["foo2 expected 2 arguments, got 1", |
| 903 |
"foo2() takes exactly 2 arguments (1 given)"] |
| 904 |
assert st1(e7.value) in ["foo2 expected 2 arguments, got 3", |
| 905 |
"foo2() takes exactly 2 arguments (3 given)"] |
| 906 |
|
| 907 |
def test_address_of_function(): |
| 908 |
ffi = FFI() |
| 909 |
ffi.cdef("long myfunc(long x);") |
| 910 |
lib = verify(ffi, "test_addressof_function", """ |
| 911 |
char myfunc(char x) { return (char)(x + 42); } |
| 912 |
""", ignore_warnings=True) |
| 913 |
assert lib.myfunc(5) == 47 |
| 914 |
assert lib.myfunc(0xABC05) == 47 |
| 915 |
assert not isinstance(lib.myfunc, ffi.CData) |
| 916 |
assert ffi.typeof(lib.myfunc) == ffi.typeof("long(*)(long)") |
| 917 |
addr = ffi.addressof(lib, 'myfunc') |
| 918 |
assert addr(5) == 47 |
| 919 |
assert addr(0xABC05) == 47 |
| 920 |
assert isinstance(addr, ffi.CData) |
| 921 |
assert ffi.typeof(addr) == ffi.typeof("long(*)(long)") |
| 922 |
|
| 923 |
def test_address_of_function_with_struct(): |
| 924 |
ffi = FFI() |
| 925 |
ffi.cdef("struct foo_s { int x; }; long myfunc(struct foo_s);") |
| 926 |
lib = verify(ffi, "test_addressof_function_with_struct", """ |
| 927 |
struct foo_s { int x; }; |
| 928 |
char myfunc(struct foo_s input) { return (char)(input.x + 42); } |
| 929 |
""") |
| 930 |
s = ffi.new("struct foo_s *", [5])[0] |
| 931 |
assert lib.myfunc(s) == 47 |
| 932 |
assert not isinstance(lib.myfunc, ffi.CData) |
| 933 |
assert ffi.typeof(lib.myfunc) == ffi.typeof("long(*)(struct foo_s)") |
| 934 |
addr = ffi.addressof(lib, 'myfunc') |
| 935 |
assert addr(s) == 47 |
| 936 |
assert isinstance(addr, ffi.CData) |
| 937 |
assert ffi.typeof(addr) == ffi.typeof("long(*)(struct foo_s)") |
| 938 |
|
| 939 |
def test_issue198(): |
| 940 |
ffi = FFI() |
| 941 |
ffi.cdef(""" |
| 942 |
typedef struct{...;} opaque_t; |
| 943 |
const opaque_t CONSTANT; |
| 944 |
int toint(opaque_t); |
| 945 |
""") |
| 946 |
lib = verify(ffi, 'test_issue198', """ |
| 947 |
typedef int opaque_t; |
| 948 |
#define CONSTANT ((opaque_t)42) |
| 949 |
static int toint(opaque_t o) { return o; } |
| 950 |
""") |
| 951 |
def random_stuff(): |
| 952 |
pass |
| 953 |
assert lib.toint(lib.CONSTANT) == 42 |
| 954 |
random_stuff() |
| 955 |
assert lib.toint(lib.CONSTANT) == 42 |
| 956 |
|
| 957 |
def test_constant_is_not_a_compiler_constant(): |
| 958 |
ffi = FFI() |
| 959 |
ffi.cdef("static const float almost_forty_two;") |
| 960 |
lib = verify(ffi, 'test_constant_is_not_a_compiler_constant', """ |
| 961 |
static float f(void) { return 42.25; } |
| 962 |
#define almost_forty_two (f()) |
| 963 |
""") |
| 964 |
assert lib.almost_forty_two == 42.25 |
| 965 |
|
| 966 |
def test_constant_of_unknown_size(): |
| 967 |
ffi = FFI() |
| 968 |
ffi.cdef(""" |
| 969 |
typedef ... opaque_t; |
| 970 |
const opaque_t CONSTANT; |
| 971 |
""") |
| 972 |
lib = verify(ffi, 'test_constant_of_unknown_size', |
| 973 |
"typedef int opaque_t;" |
| 974 |
"const int CONSTANT = 42;") |
| 975 |
e = pytest.raises(ffi.error, getattr, lib, 'CONSTANT') |
| 976 |
assert str(e.value) == ("constant 'CONSTANT' is of " |
| 977 |
"type 'opaque_t', whose size is not known") |
| 978 |
|
| 979 |
def test_variable_of_unknown_size(): |
| 980 |
ffi = FFI() |
| 981 |
ffi.cdef(""" |
| 982 |
typedef ... opaque_t; |
| 983 |
extern opaque_t globvar; |
| 984 |
""") |
| 985 |
lib = verify(ffi, 'test_variable_of_unknown_size', """ |
| 986 |
typedef char opaque_t[6]; |
| 987 |
opaque_t globvar = "hello"; |
| 988 |
""") |
| 989 |
|
| 990 |
e = pytest.raises(TypeError, getattr, lib, 'globvar') |
| 991 |
assert str(e.value) in ["cdata 'opaque_t' is opaque", |
| 992 |
"'opaque_t' is opaque or not completed yet"] |
| 993 |
e = pytest.raises(TypeError, setattr, lib, 'globvar', []) |
| 994 |
assert str(e.value) in ["'opaque_t' is opaque", |
| 995 |
"'opaque_t' is opaque or not completed yet"] |
| 996 |
|
| 997 |
p = ffi.addressof(lib, 'globvar') |
| 998 |
assert ffi.typeof(p) == ffi.typeof('opaque_t *') |
| 999 |
assert ffi.string(ffi.cast("char *", p), 8) == b"hello" |
| 1000 |
|
| 1001 |
def test_constant_of_value_unknown_to_the_compiler(): |
| 1002 |
extra_c_source = udir.join( |
| 1003 |
'extra_test_constant_of_value_unknown_to_the_compiler.c') |
| 1004 |
extra_c_source.write('const int external_foo = 42;\n') |
| 1005 |
ffi = FFI() |
| 1006 |
ffi.cdef("const int external_foo;") |
| 1007 |
lib = verify(ffi, 'test_constant_of_value_unknown_to_the_compiler', """ |
| 1008 |
extern const int external_foo; |
| 1009 |
""", sources=[str(extra_c_source)]) |
| 1010 |
assert lib.external_foo == 42 |
| 1011 |
|
| 1012 |
def test_dotdot_in_source_file_names(): |
| 1013 |
extra_c_source = udir.join( |
| 1014 |
'extra_test_dotdot_in_source_file_names.c') |
| 1015 |
extra_c_source.write('const int external_foo = 42;\n') |
| 1016 |
ffi = FFI() |
| 1017 |
ffi.cdef("const int external_foo;") |
| 1018 |
lib = verify(ffi, 'test_dotdot_in_source_file_names', """ |
| 1019 |
extern const int external_foo; |
| 1020 |
""", sources=[os.path.join(os.path.dirname(str(extra_c_source)), |
| 1021 |
'foobar', '..', |
| 1022 |
os.path.basename(str(extra_c_source)))]) |
| 1023 |
assert lib.external_foo == 42 |
| 1024 |
|
| 1025 |
def test_call_with_incomplete_structs(): |
| 1026 |
ffi = FFI() |
| 1027 |
ffi.cdef("typedef struct {...;} foo_t; " |
| 1028 |
"extern foo_t myglob; " |
| 1029 |
"foo_t increment(foo_t s); " |
| 1030 |
"double getx(foo_t s);") |
| 1031 |
lib = verify(ffi, 'test_call_with_incomplete_structs', """ |
| 1032 |
typedef double foo_t; |
| 1033 |
double myglob = 42.5; |
| 1034 |
double getx(double x) { return x; } |
| 1035 |
double increment(double x) { return x + 1; } |
| 1036 |
""") |
| 1037 |
assert lib.getx(lib.myglob) == 42.5 |
| 1038 |
assert lib.getx(lib.increment(lib.myglob)) == 43.5 |
| 1039 |
|
| 1040 |
def test_struct_array_guess_length_2(): |
| 1041 |
ffi = FFI() |
| 1042 |
ffi.cdef("struct foo_s { int a[...][...]; };") |
| 1043 |
lib = verify(ffi, 'test_struct_array_guess_length_2', |
| 1044 |
"struct foo_s { int x; int a[5][8]; int y; };") |
| 1045 |
assert ffi.sizeof('struct foo_s') == 42 * ffi.sizeof('int') |
| 1046 |
s = ffi.new("struct foo_s *") |
| 1047 |
assert ffi.typeof(s.a) == ffi.typeof("int[5][8]") |
| 1048 |
assert ffi.sizeof(s.a) == 40 * ffi.sizeof('int') |
| 1049 |
assert s.a[4][7] == 0 |
| 1050 |
with pytest.raises(IndexError): |
| 1051 |
s.a[4][8] |
| 1052 |
with pytest.raises(IndexError): |
| 1053 |
s.a[5][0] |
| 1054 |
assert ffi.typeof(s.a) == ffi.typeof("int[5][8]") |
| 1055 |
assert ffi.typeof(s.a[0]) == ffi.typeof("int[8]") |
| 1056 |
|
| 1057 |
def test_struct_array_guess_length_3(): |
| 1058 |
ffi = FFI() |
| 1059 |
ffi.cdef("struct foo_s { int a[][...]; };") |
| 1060 |
lib = verify(ffi, 'test_struct_array_guess_length_3', |
| 1061 |
"struct foo_s { int x; int a[5][7]; int y; };") |
| 1062 |
assert ffi.sizeof('struct foo_s') == 37 * ffi.sizeof('int') |
| 1063 |
s = ffi.new("struct foo_s *") |
| 1064 |
assert ffi.typeof(s.a) == ffi.typeof("int[][7]") |
| 1065 |
assert s.a[4][6] == 0 |
| 1066 |
with pytest.raises(IndexError): |
| 1067 |
s.a[4][7] |
| 1068 |
assert ffi.typeof(s.a[0]) == ffi.typeof("int[7]") |
| 1069 |
|
| 1070 |
def test_global_var_array_2(): |
| 1071 |
ffi = FFI() |
| 1072 |
ffi.cdef("extern int a[...][...];") |
| 1073 |
lib = verify(ffi, 'test_global_var_array_2', 'int a[10][8];') |
| 1074 |
lib.a[9][7] = 123456 |
| 1075 |
assert lib.a[9][7] == 123456 |
| 1076 |
with pytest.raises(IndexError): |
| 1077 |
lib.a[0][8] |
| 1078 |
with pytest.raises(IndexError): |
| 1079 |
lib.a[10][0] |
| 1080 |
assert ffi.typeof(lib.a) == ffi.typeof("int[10][8]") |
| 1081 |
assert ffi.typeof(lib.a[0]) == ffi.typeof("int[8]") |
| 1082 |
|
| 1083 |
def test_global_var_array_3(): |
| 1084 |
ffi = FFI() |
| 1085 |
ffi.cdef("extern int a[][...];") |
| 1086 |
lib = verify(ffi, 'test_global_var_array_3', 'int a[10][8];') |
| 1087 |
lib.a[9][7] = 123456 |
| 1088 |
assert lib.a[9][7] == 123456 |
| 1089 |
with pytest.raises(IndexError): |
| 1090 |
lib.a[0][8] |
| 1091 |
assert ffi.typeof(lib.a) == ffi.typeof("int(*)[8]") |
| 1092 |
assert ffi.typeof(lib.a[0]) == ffi.typeof("int[8]") |
| 1093 |
|
| 1094 |
def test_global_var_array_4(): |
| 1095 |
ffi = FFI() |
| 1096 |
ffi.cdef("extern int a[10][...];") |
| 1097 |
lib = verify(ffi, 'test_global_var_array_4', 'int a[10][8];') |
| 1098 |
lib.a[9][7] = 123456 |
| 1099 |
assert lib.a[9][7] == 123456 |
| 1100 |
with pytest.raises(IndexError): |
| 1101 |
lib.a[0][8] |
| 1102 |
with pytest.raises(IndexError): |
| 1103 |
lib.a[10][8] |
| 1104 |
assert ffi.typeof(lib.a) == ffi.typeof("int[10][8]") |
| 1105 |
assert ffi.typeof(lib.a[0]) == ffi.typeof("int[8]") |
| 1106 |
|
| 1107 |
def test_some_integer_type(): |
| 1108 |
ffi = FFI() |
| 1109 |
ffi.cdef(""" |
| 1110 |
typedef int... foo_t; |
| 1111 |
typedef unsigned long... bar_t; |
| 1112 |
typedef struct { foo_t a, b; } mystruct_t; |
| 1113 |
foo_t foobar(bar_t, mystruct_t); |
| 1114 |
static const bar_t mu = -20; |
| 1115 |
static const foo_t nu = 20; |
| 1116 |
""") |
| 1117 |
lib = verify(ffi, 'test_some_integer_type', """ |
| 1118 |
typedef unsigned long long foo_t; |
| 1119 |
typedef short bar_t; |
| 1120 |
typedef struct { foo_t a, b; } mystruct_t; |
| 1121 |
static foo_t foobar(bar_t x, mystruct_t s) { |
| 1122 |
return (foo_t)x + s.a + s.b; |
| 1123 |
} |
| 1124 |
static const bar_t mu = -20; |
| 1125 |
static const foo_t nu = 20; |
| 1126 |
""") |
| 1127 |
assert ffi.sizeof("foo_t") == ffi.sizeof("unsigned long long") |
| 1128 |
assert ffi.sizeof("bar_t") == ffi.sizeof("short") |
| 1129 |
maxulonglong = 2 ** 64 - 1 |
| 1130 |
assert int(ffi.cast("foo_t", -1)) == maxulonglong |
| 1131 |
assert int(ffi.cast("bar_t", -1)) == -1 |
| 1132 |
assert lib.foobar(-1, [0, 0]) == maxulonglong |
| 1133 |
assert lib.foobar(2 ** 15 - 1, [0, 0]) == 2 ** 15 - 1 |
| 1134 |
assert lib.foobar(10, [20, 31]) == 61 |
| 1135 |
assert lib.foobar(0, [0, maxulonglong]) == maxulonglong |
| 1136 |
pytest.raises(OverflowError, lib.foobar, 2 ** 15, [0, 0]) |
| 1137 |
pytest.raises(OverflowError, lib.foobar, -(2 ** 15) - 1, [0, 0]) |
| 1138 |
pytest.raises(OverflowError, ffi.new, "mystruct_t *", [0, -1]) |
| 1139 |
assert lib.mu == -20 |
| 1140 |
assert lib.nu == 20 |
| 1141 |
|
| 1142 |
def test_some_float_type(): |
| 1143 |
ffi = FFI() |
| 1144 |
ffi.cdef(""" |
| 1145 |
typedef double... foo_t; |
| 1146 |
typedef float... bar_t; |
| 1147 |
foo_t sum(foo_t[]); |
| 1148 |
bar_t neg(bar_t); |
| 1149 |
""") |
| 1150 |
lib = verify(ffi, 'test_some_float_type', """ |
| 1151 |
typedef float foo_t; |
| 1152 |
static foo_t sum(foo_t x[]) { return x[0] + x[1]; } |
| 1153 |
typedef double bar_t; |
| 1154 |
static double neg(double x) { return -x; } |
| 1155 |
""") |
| 1156 |
assert lib.sum([40.0, 2.25]) == 42.25 |
| 1157 |
assert lib.sum([12.3, 45.6]) != 12.3 + 45.6 |
| 1158 |
assert lib.neg(12.3) == -12.3 |
| 1159 |
assert ffi.sizeof("foo_t") == ffi.sizeof("float") |
| 1160 |
assert ffi.sizeof("bar_t") == ffi.sizeof("double") |
| 1161 |
|
| 1162 |
def test_some_float_invalid_1(): |
| 1163 |
ffi = FFI() |
| 1164 |
pytest.raises((FFIError, |
| 1165 |
CDefError), |
| 1166 |
ffi.cdef, "typedef long double... foo_t;") |
| 1167 |
|
| 1168 |
def test_some_float_invalid_2(): |
| 1169 |
ffi = FFI() |
| 1170 |
ffi.cdef("typedef double... foo_t; foo_t neg(foo_t);") |
| 1171 |
lib = verify(ffi, 'test_some_float_invalid_2', """ |
| 1172 |
typedef unsigned long foo_t; |
| 1173 |
foo_t neg(foo_t x) { return -x; } |
| 1174 |
""") |
| 1175 |
e = pytest.raises(ffi.error, getattr, lib, 'neg') |
| 1176 |
assert str(e.value) == ("primitive floating-point type with an unexpected " |
| 1177 |
"size (or not a float type at all)") |
| 1178 |
|
| 1179 |
def test_some_float_invalid_3(): |
| 1180 |
ffi = FFI() |
| 1181 |
ffi.cdef("typedef double... foo_t; foo_t neg(foo_t);") |
| 1182 |
lib = verify(ffi, 'test_some_float_invalid_3', """ |
| 1183 |
typedef long double foo_t; |
| 1184 |
foo_t neg(foo_t x) { return -x; } |
| 1185 |
""", ignore_warnings=True) |
| 1186 |
if ffi.sizeof("long double") == ffi.sizeof("double"): |
| 1187 |
assert lib.neg(12.3) == -12.3 |
| 1188 |
else: |
| 1189 |
e = pytest.raises(ffi.error, getattr, lib, 'neg') |
| 1190 |
assert str(e.value) == ("primitive floating-point type is " |
| 1191 |
"'long double', not supported for now with " |
| 1192 |
"the syntax 'typedef double... xxx;'") |
| 1193 |
|
| 1194 |
def test_issue200(): |
| 1195 |
ffi = FFI() |
| 1196 |
ffi.cdef(""" |
| 1197 |
typedef void (function_t)(void*); |
| 1198 |
void function(void *); |
| 1199 |
""") |
| 1200 |
lib = verify(ffi, 'test_issue200', """ |
| 1201 |
static void function(void *p) { (void)p; } |
| 1202 |
""") |
| 1203 |
ffi.typeof('function_t*') |
| 1204 |
lib.function(ffi.NULL) |
| 1205 |
|
| 1206 |
|
| 1207 |
def test_alignment_of_longlong(): |
| 1208 |
ffi = FFI() |
| 1209 |
x1 = ffi.alignof('unsigned long long') |
| 1210 |
assert x1 in [4, 8] |
| 1211 |
ffi.cdef("struct foo_s { unsigned long long x; };") |
| 1212 |
lib = verify(ffi, 'test_alignment_of_longlong', |
| 1213 |
"struct foo_s { unsigned long long x; };") |
| 1214 |
assert ffi.alignof('unsigned long long') == x1 |
| 1215 |
assert ffi.alignof('struct foo_s') == x1 |
| 1216 |
|
| 1217 |
def test_import_from_lib(): |
| 1218 |
ffi = FFI() |
| 1219 |
ffi.cdef("int mybar(int); static int myvar;\n#define MYFOO ...") |
| 1220 |
lib = verify(ffi, 'test_import_from_lib', |
| 1221 |
"#define MYFOO 42\n" |
| 1222 |
"static int mybar(int x) { return x + 1; }\n" |
| 1223 |
"static int myvar = -5;") |
| 1224 |
assert sys.modules['_CFFI_test_import_from_lib'].lib is lib |
| 1225 |
assert sys.modules['_CFFI_test_import_from_lib.lib'] is lib |
| 1226 |
from _CFFI_test_import_from_lib.lib import MYFOO |
| 1227 |
assert MYFOO == 42 |
| 1228 |
assert hasattr(lib, '__dict__') |
| 1229 |
assert lib.__all__ == ['MYFOO', 'mybar'] |
| 1230 |
assert lib.__name__ == '_CFFI_test_import_from_lib.lib' |
| 1231 |
assert lib.__class__ is type(sys) |
| 1232 |
|
| 1233 |
def test_macro_var_callback(): |
| 1234 |
ffi = FFI() |
| 1235 |
ffi.cdef("extern int my_value; extern int *(*get_my_value)(void);") |
| 1236 |
lib = verify(ffi, 'test_macro_var_callback', |
| 1237 |
"int *(*get_my_value)(void);\n" |
| 1238 |
"#define my_value (*get_my_value())") |
| 1239 |
|
| 1240 |
values = ffi.new("int[50]") |
| 1241 |
def it(): |
| 1242 |
for i in range(50): |
| 1243 |
yield i |
| 1244 |
it = it() |
| 1245 |
|
| 1246 |
@ffi.callback("int *(*)(void)") |
| 1247 |
def get_my_value(): |
| 1248 |
for nextvalue in it: |
| 1249 |
return values + nextvalue |
| 1250 |
lib.get_my_value = get_my_value |
| 1251 |
|
| 1252 |
values[0] = 41 |
| 1253 |
assert lib.my_value == 41 |
| 1254 |
p = ffi.addressof(lib, 'my_value') |
| 1255 |
assert p == values + 1 |
| 1256 |
assert p[-1] == 41 |
| 1257 |
assert p[+1] == 0 |
| 1258 |
lib.my_value = 42 |
| 1259 |
assert values[2] == 42 |
| 1260 |
assert p[-1] == 41 |
| 1261 |
assert p[+1] == 42 |
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
@ffi.callback("int *(*)(void)") |
| 1268 |
def get_my_value(): |
| 1269 |
raise LookupError |
| 1270 |
lib.get_my_value = get_my_value |
| 1271 |
pytest.raises(ffi.error, getattr, lib, 'my_value') |
| 1272 |
pytest.raises(ffi.error, setattr, lib, 'my_value', 50) |
| 1273 |
pytest.raises(ffi.error, ffi.addressof, lib, 'my_value') |
| 1274 |
@ffi.callback("int *(*)(void)") |
| 1275 |
def get_my_value(): |
| 1276 |
return "hello" |
| 1277 |
lib.get_my_value = get_my_value |
| 1278 |
pytest.raises(ffi.error, getattr, lib, 'my_value') |
| 1279 |
e = pytest.raises(ffi.error, setattr, lib, 'my_value', 50) |
| 1280 |
assert str(e.value) == "global variable 'my_value' is at address NULL" |
| 1281 |
|
| 1282 |
def test_const_fields(): |
| 1283 |
ffi = FFI() |
| 1284 |
ffi.cdef("""struct foo_s { const int a; void *const b; };""") |
| 1285 |
lib = verify(ffi, 'test_const_fields', """ |
| 1286 |
struct foo_s { const int a; void *const b; };""") |
| 1287 |
foo_s = ffi.typeof("struct foo_s") |
| 1288 |
assert foo_s.fields[0][0] == 'a' |
| 1289 |
assert foo_s.fields[0][1].type is ffi.typeof("int") |
| 1290 |
assert foo_s.fields[1][0] == 'b' |
| 1291 |
assert foo_s.fields[1][1].type is ffi.typeof("void *") |
| 1292 |
|
| 1293 |
def test_restrict_fields(): |
| 1294 |
ffi = FFI() |
| 1295 |
ffi.cdef("""struct foo_s { void * restrict b; };""") |
| 1296 |
lib = verify(ffi, 'test_restrict_fields', """ |
| 1297 |
struct foo_s { void * __restrict b; };""") |
| 1298 |
foo_s = ffi.typeof("struct foo_s") |
| 1299 |
assert foo_s.fields[0][0] == 'b' |
| 1300 |
assert foo_s.fields[0][1].type is ffi.typeof("void *") |
| 1301 |
|
| 1302 |
def test_volatile_fields(): |
| 1303 |
ffi = FFI() |
| 1304 |
ffi.cdef("""struct foo_s { void * volatile b; };""") |
| 1305 |
lib = verify(ffi, 'test_volatile_fields', """ |
| 1306 |
struct foo_s { void * volatile b; };""") |
| 1307 |
foo_s = ffi.typeof("struct foo_s") |
| 1308 |
assert foo_s.fields[0][0] == 'b' |
| 1309 |
assert foo_s.fields[0][1].type is ffi.typeof("void *") |
| 1310 |
|
| 1311 |
def test_const_array_fields(): |
| 1312 |
ffi = FFI() |
| 1313 |
ffi.cdef("""struct foo_s { const int a[4]; };""") |
| 1314 |
lib = verify(ffi, 'test_const_array_fields', """ |
| 1315 |
struct foo_s { const int a[4]; };""") |
| 1316 |
foo_s = ffi.typeof("struct foo_s") |
| 1317 |
assert foo_s.fields[0][0] == 'a' |
| 1318 |
assert foo_s.fields[0][1].type is ffi.typeof("int[4]") |
| 1319 |
|
| 1320 |
def test_const_array_fields_varlength(): |
| 1321 |
ffi = FFI() |
| 1322 |
ffi.cdef("""struct foo_s { const int a[]; ...; };""") |
| 1323 |
lib = verify(ffi, 'test_const_array_fields_varlength', """ |
| 1324 |
struct foo_s { const int a[4]; };""") |
| 1325 |
foo_s = ffi.typeof("struct foo_s") |
| 1326 |
assert foo_s.fields[0][0] == 'a' |
| 1327 |
assert foo_s.fields[0][1].type is ffi.typeof("int[]") |
| 1328 |
|
| 1329 |
def test_const_array_fields_unknownlength(): |
| 1330 |
ffi = FFI() |
| 1331 |
ffi.cdef("""struct foo_s { const int a[...]; ...; };""") |
| 1332 |
lib = verify(ffi, 'test_const_array_fields_unknownlength', """ |
| 1333 |
struct foo_s { const int a[4]; };""") |
| 1334 |
foo_s = ffi.typeof("struct foo_s") |
| 1335 |
assert foo_s.fields[0][0] == 'a' |
| 1336 |
assert foo_s.fields[0][1].type is ffi.typeof("int[4]") |
| 1337 |
|
| 1338 |
def test_const_function_args(): |
| 1339 |
ffi = FFI() |
| 1340 |
ffi.cdef("""int foobar(const int a, const int *b, const int c[]);""") |
| 1341 |
lib = verify(ffi, 'test_const_function_args', """ |
| 1342 |
int foobar(const int a, const int *b, const int c[]) { |
| 1343 |
return a + *b + *c; |
| 1344 |
} |
| 1345 |
""") |
| 1346 |
assert lib.foobar(100, ffi.new("int *", 40), ffi.new("int *", 2)) == 142 |
| 1347 |
|
| 1348 |
def test_const_function_type_args(): |
| 1349 |
ffi = FFI() |
| 1350 |
ffi.cdef("""extern int(*foobar)(const int a,const int*b,const int c[]);""") |
| 1351 |
lib = verify(ffi, 'test_const_function_type_args', """ |
| 1352 |
int (*foobar)(const int a, const int *b, const int c[]); |
| 1353 |
""") |
| 1354 |
t = ffi.typeof(lib.foobar) |
| 1355 |
assert t.args[0] is ffi.typeof("int") |
| 1356 |
assert t.args[1] is ffi.typeof("int *") |
| 1357 |
assert t.args[2] is ffi.typeof("int *") |
| 1358 |
|
| 1359 |
def test_const_constant(): |
| 1360 |
ffi = FFI() |
| 1361 |
ffi.cdef("""struct foo_s { int x,y; }; const struct foo_s myfoo;""") |
| 1362 |
lib = verify(ffi, 'test_const_constant', """ |
| 1363 |
struct foo_s { int x,y; }; const struct foo_s myfoo = { 40, 2 }; |
| 1364 |
""") |
| 1365 |
assert lib.myfoo.x == 40 |
| 1366 |
assert lib.myfoo.y == 2 |
| 1367 |
|
| 1368 |
def test_const_via_typedef(): |
| 1369 |
ffi = FFI() |
| 1370 |
ffi.cdef("""typedef const int const_t; const_t aaa;""") |
| 1371 |
lib = verify(ffi, 'test_const_via_typedef', """ |
| 1372 |
typedef const int const_t; |
| 1373 |
#define aaa 42 |
| 1374 |
""") |
| 1375 |
assert lib.aaa == 42 |
| 1376 |
with pytest.raises(AttributeError): |
| 1377 |
lib.aaa = 43 |
| 1378 |
|
| 1379 |
def test_win32_calling_convention_0(): |
| 1380 |
ffi = FFI() |
| 1381 |
ffi.cdef(""" |
| 1382 |
int call1(int(__cdecl *cb)(int)); |
| 1383 |
int (*const call2)(int(__stdcall *cb)(int)); |
| 1384 |
""") |
| 1385 |
lib = verify(ffi, 'test_win32_calling_convention_0', r""" |
| 1386 |
#ifndef _MSC_VER |
| 1387 |
# define __stdcall /* nothing */ |
| 1388 |
#endif |
| 1389 |
int call1(int(*cb)(int)) { |
| 1390 |
int i, result = 0; |
| 1391 |
//printf("call1: cb = %p\n", cb); |
| 1392 |
for (i = 0; i < 1000; i++) |
| 1393 |
result += cb(i); |
| 1394 |
//printf("result = %d\n", result); |
| 1395 |
return result; |
| 1396 |
} |
| 1397 |
int call2(int(__stdcall *cb)(int)) { |
| 1398 |
int i, result = 0; |
| 1399 |
//printf("call2: cb = %p\n", cb); |
| 1400 |
for (i = 0; i < 1000; i++) |
| 1401 |
result += cb(-i); |
| 1402 |
//printf("result = %d\n", result); |
| 1403 |
return result; |
| 1404 |
} |
| 1405 |
""") |
| 1406 |
@ffi.callback("int(int)") |
| 1407 |
def cb1(x): |
| 1408 |
return x * 2 |
| 1409 |
@ffi.callback("int __stdcall(int)") |
| 1410 |
def cb2(x): |
| 1411 |
return x * 3 |
| 1412 |
res = lib.call1(cb1) |
| 1413 |
assert res == 500*999*2 |
| 1414 |
assert res == ffi.addressof(lib, 'call1')(cb1) |
| 1415 |
res = lib.call2(cb2) |
| 1416 |
assert res == -500*999*3 |
| 1417 |
assert res == ffi.addressof(lib, 'call2')(cb2) |
| 1418 |
if sys.platform == 'win32' and not sys.maxsize > 2**32: |
| 1419 |
assert '__stdcall' in str(ffi.typeof(cb2)) |
| 1420 |
assert '__stdcall' not in str(ffi.typeof(cb1)) |
| 1421 |
pytest.raises(TypeError, lib.call1, cb2) |
| 1422 |
pytest.raises(TypeError, lib.call2, cb1) |
| 1423 |
else: |
| 1424 |
assert '__stdcall' not in str(ffi.typeof(cb2)) |
| 1425 |
assert ffi.typeof(cb2) is ffi.typeof(cb1) |
| 1426 |
|
| 1427 |
def test_win32_calling_convention_1(): |
| 1428 |
ffi = FFI() |
| 1429 |
ffi.cdef(""" |
| 1430 |
int __cdecl call1(int(__cdecl *cb)(int)); |
| 1431 |
int __stdcall call2(int(__stdcall *cb)(int)); |
| 1432 |
int (__cdecl *const cb1)(int); |
| 1433 |
int (__stdcall *const cb2)(int); |
| 1434 |
""") |
| 1435 |
lib = verify(ffi, 'test_win32_calling_convention_1', r""" |
| 1436 |
#ifndef _MSC_VER |
| 1437 |
# define __cdecl |
| 1438 |
# define __stdcall |
| 1439 |
#endif |
| 1440 |
int __cdecl cb1(int x) { return x * 2; } |
| 1441 |
int __stdcall cb2(int x) { return x * 3; } |
| 1442 |
|
| 1443 |
int __cdecl call1(int(__cdecl *cb)(int)) { |
| 1444 |
int i, result = 0; |
| 1445 |
//printf("here1\n"); |
| 1446 |
//printf("cb = %p, cb1 = %p\n", cb, (void *)cb1); |
| 1447 |
for (i = 0; i < 1000; i++) |
| 1448 |
result += cb(i); |
| 1449 |
//printf("result = %d\n", result); |
| 1450 |
return result; |
| 1451 |
} |
| 1452 |
int __stdcall call2(int(__stdcall *cb)(int)) { |
| 1453 |
int i, result = 0; |
| 1454 |
//printf("here1\n"); |
| 1455 |
//printf("cb = %p, cb2 = %p\n", cb, (void *)cb2); |
| 1456 |
for (i = 0; i < 1000; i++) |
| 1457 |
result += cb(-i); |
| 1458 |
//printf("result = %d\n", result); |
| 1459 |
return result; |
| 1460 |
} |
| 1461 |
""") |
| 1462 |
|
| 1463 |
ptr_call1 = ffi.addressof(lib, 'call1') |
| 1464 |
assert lib.call1(ffi.addressof(lib, 'cb1')) == 500*999*2 |
| 1465 |
assert ptr_call1(ffi.addressof(lib, 'cb1')) == 500*999*2 |
| 1466 |
|
| 1467 |
ptr_call2 = ffi.addressof(lib, 'call2') |
| 1468 |
assert lib.call2(ffi.addressof(lib, 'cb2')) == -500*999*3 |
| 1469 |
assert ptr_call2(ffi.addressof(lib, 'cb2')) == -500*999*3 |
| 1470 |
|
| 1471 |
|
| 1472 |
def test_win32_calling_convention_2(): |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
ffi = FFI() |
| 1478 |
ffi.cdef(""" |
| 1479 |
int __stdcall call1(int(__cdecl *cb)(int)); |
| 1480 |
int __cdecl call2(int(__stdcall *cb)(int)); |
| 1481 |
int (__cdecl *const cb1)(int); |
| 1482 |
int (__stdcall *const cb2)(int); |
| 1483 |
""") |
| 1484 |
lib = verify(ffi, 'test_win32_calling_convention_2', """ |
| 1485 |
#ifndef _MSC_VER |
| 1486 |
# define __cdecl |
| 1487 |
# define __stdcall |
| 1488 |
#endif |
| 1489 |
int __cdecl call1(int(__cdecl *cb)(int)) { |
| 1490 |
int i, result = 0; |
| 1491 |
for (i = 0; i < 1000; i++) |
| 1492 |
result += cb(i); |
| 1493 |
return result; |
| 1494 |
} |
| 1495 |
int __stdcall call2(int(__stdcall *cb)(int)) { |
| 1496 |
int i, result = 0; |
| 1497 |
for (i = 0; i < 1000; i++) |
| 1498 |
result += cb(-i); |
| 1499 |
return result; |
| 1500 |
} |
| 1501 |
int __cdecl cb1(int x) { return x * 2; } |
| 1502 |
int __stdcall cb2(int x) { return x * 3; } |
| 1503 |
""") |
| 1504 |
ptr_call1 = ffi.addressof(lib, 'call1') |
| 1505 |
ptr_call2 = ffi.addressof(lib, 'call2') |
| 1506 |
if sys.platform == 'win32' and not sys.maxsize > 2**32: |
| 1507 |
pytest.raises(TypeError, lib.call1, ffi.addressof(lib, 'cb2')) |
| 1508 |
pytest.raises(TypeError, ptr_call1, ffi.addressof(lib, 'cb2')) |
| 1509 |
pytest.raises(TypeError, lib.call2, ffi.addressof(lib, 'cb1')) |
| 1510 |
pytest.raises(TypeError, ptr_call2, ffi.addressof(lib, 'cb1')) |
| 1511 |
assert lib.call1(ffi.addressof(lib, 'cb1')) == 500*999*2 |
| 1512 |
assert ptr_call1(ffi.addressof(lib, 'cb1')) == 500*999*2 |
| 1513 |
assert lib.call2(ffi.addressof(lib, 'cb2')) == -500*999*3 |
| 1514 |
assert ptr_call2(ffi.addressof(lib, 'cb2')) == -500*999*3 |
| 1515 |
|
| 1516 |
def test_win32_calling_convention_3(): |
| 1517 |
ffi = FFI() |
| 1518 |
ffi.cdef(""" |
| 1519 |
struct point { int x, y; }; |
| 1520 |
|
| 1521 |
int (*const cb1)(struct point); |
| 1522 |
int (__stdcall *const cb2)(struct point); |
| 1523 |
|
| 1524 |
struct point __stdcall call1(int(*cb)(struct point)); |
| 1525 |
struct point call2(int(__stdcall *cb)(struct point)); |
| 1526 |
""") |
| 1527 |
lib = verify(ffi, 'test_win32_calling_convention_3', r""" |
| 1528 |
#ifndef _MSC_VER |
| 1529 |
# define __cdecl |
| 1530 |
# define __stdcall |
| 1531 |
#endif |
| 1532 |
struct point { int x, y; }; |
| 1533 |
int cb1(struct point pt) { return pt.x + 10 * pt.y; } |
| 1534 |
int __stdcall cb2(struct point pt) { return pt.x + 100 * pt.y; } |
| 1535 |
struct point __stdcall call1(int(__cdecl *cb)(struct point)) { |
| 1536 |
int i; |
| 1537 |
struct point result = { 0, 0 }; |
| 1538 |
//printf("here1\n"); |
| 1539 |
//printf("cb = %p, cb1 = %p\n", cb, (void *)cb1); |
| 1540 |
for (i = 0; i < 1000; i++) { |
| 1541 |
struct point p = { i, -i }; |
| 1542 |
int r = cb(p); |
| 1543 |
result.x += r; |
| 1544 |
result.y -= r; |
| 1545 |
} |
| 1546 |
return result; |
| 1547 |
} |
| 1548 |
struct point __cdecl call2(int(__stdcall *cb)(struct point)) { |
| 1549 |
int i; |
| 1550 |
struct point result = { 0, 0 }; |
| 1551 |
for (i = 0; i < 1000; i++) { |
| 1552 |
struct point p = { -i, i }; |
| 1553 |
int r = cb(p); |
| 1554 |
result.x += r; |
| 1555 |
result.y -= r; |
| 1556 |
} |
| 1557 |
return result; |
| 1558 |
} |
| 1559 |
""") |
| 1560 |
ptr_call1 = ffi.addressof(lib, 'call1') |
| 1561 |
ptr_call2 = ffi.addressof(lib, 'call2') |
| 1562 |
if sys.platform == 'win32' and not sys.maxsize > 2**32: |
| 1563 |
pytest.raises(TypeError, lib.call1, ffi.addressof(lib, 'cb2')) |
| 1564 |
pytest.raises(TypeError, ptr_call1, ffi.addressof(lib, 'cb2')) |
| 1565 |
pytest.raises(TypeError, lib.call2, ffi.addressof(lib, 'cb1')) |
| 1566 |
pytest.raises(TypeError, ptr_call2, ffi.addressof(lib, 'cb1')) |
| 1567 |
pt = lib.call1(ffi.addressof(lib, 'cb1')) |
| 1568 |
assert (pt.x, pt.y) == (-9*500*999, 9*500*999) |
| 1569 |
pt = ptr_call1(ffi.addressof(lib, 'cb1')) |
| 1570 |
assert (pt.x, pt.y) == (-9*500*999, 9*500*999) |
| 1571 |
pt = lib.call2(ffi.addressof(lib, 'cb2')) |
| 1572 |
assert (pt.x, pt.y) == (99*500*999, -99*500*999) |
| 1573 |
pt = ptr_call2(ffi.addressof(lib, 'cb2')) |
| 1574 |
assert (pt.x, pt.y) == (99*500*999, -99*500*999) |
| 1575 |
|
| 1576 |
def test_extern_python_1(): |
| 1577 |
import warnings |
| 1578 |
ffi = FFI() |
| 1579 |
with warnings.catch_warnings(record=True) as log: |
| 1580 |
ffi.cdef(""" |
| 1581 |
extern "Python" { |
| 1582 |
int bar(int, int); |
| 1583 |
void baz(int, int); |
| 1584 |
int bok(void); |
| 1585 |
void boz(void); |
| 1586 |
} |
| 1587 |
""") |
| 1588 |
assert len(log) == 0, "got a warning: %r" % (log,) |
| 1589 |
lib = verify(ffi, 'test_extern_python_1', """ |
| 1590 |
static void baz(int, int); /* forward */ |
| 1591 |
""") |
| 1592 |
assert ffi.typeof(lib.bar) == ffi.typeof("int(*)(int, int)") |
| 1593 |
with FdWriteCapture() as f: |
| 1594 |
res = lib.bar(4, 5) |
| 1595 |
assert res == 0 |
| 1596 |
assert f.getvalue() == ( |
| 1597 |
b"extern \"Python\": function _CFFI_test_extern_python_1.bar() called, " |
| 1598 |
b"but no code was attached " |
| 1599 |
b"to it yet with @ffi.def_extern(). Returning 0.\n") |
| 1600 |
|
| 1601 |
@ffi.def_extern("bar") |
| 1602 |
def my_bar(x, y): |
| 1603 |
seen.append(("Bar", x, y)) |
| 1604 |
return x * y |
| 1605 |
assert my_bar != lib.bar |
| 1606 |
seen = [] |
| 1607 |
res = lib.bar(6, 7) |
| 1608 |
assert seen == [("Bar", 6, 7)] |
| 1609 |
assert res == 42 |
| 1610 |
|
| 1611 |
def baz(x, y): |
| 1612 |
seen.append(("Baz", x, y)) |
| 1613 |
baz1 = ffi.def_extern()(baz) |
| 1614 |
assert baz1 is baz |
| 1615 |
seen = [] |
| 1616 |
baz(long(40), long(4)) |
| 1617 |
res = lib.baz(long(50), long(8)) |
| 1618 |
assert res is None |
| 1619 |
assert seen == [("Baz", 40, 4), ("Baz", 50, 8)] |
| 1620 |
assert type(seen[0][1]) is type(seen[0][2]) is long |
| 1621 |
assert type(seen[1][1]) is type(seen[1][2]) is int |
| 1622 |
|
| 1623 |
@ffi.def_extern(name="bok") |
| 1624 |
def bokk(): |
| 1625 |
seen.append("Bok") |
| 1626 |
return 42 |
| 1627 |
seen = [] |
| 1628 |
assert lib.bok() == 42 |
| 1629 |
assert seen == ["Bok"] |
| 1630 |
|
| 1631 |
@ffi.def_extern() |
| 1632 |
def boz(): |
| 1633 |
seen.append("Boz") |
| 1634 |
seen = [] |
| 1635 |
assert lib.boz() is None |
| 1636 |
assert seen == ["Boz"] |
| 1637 |
|
| 1638 |
def test_extern_python_bogus_name(): |
| 1639 |
ffi = FFI() |
| 1640 |
ffi.cdef("extern int abc;") |
| 1641 |
lib = verify(ffi, 'test_extern_python_bogus_name', "int abc;") |
| 1642 |
def fn(): |
| 1643 |
pass |
| 1644 |
pytest.raises(ffi.error, ffi.def_extern("unknown_name"), fn) |
| 1645 |
pytest.raises(ffi.error, ffi.def_extern("abc"), fn) |
| 1646 |
assert lib.abc == 0 |
| 1647 |
e = pytest.raises(ffi.error, ffi.def_extern("abc"), fn) |
| 1648 |
assert str(e.value) == ("ffi.def_extern('abc'): no 'extern \"Python\"' " |
| 1649 |
"function with this name") |
| 1650 |
e = pytest.raises(ffi.error, ffi.def_extern(), fn) |
| 1651 |
assert str(e.value) == ("ffi.def_extern('fn'): no 'extern \"Python\"' " |
| 1652 |
"function with this name") |
| 1653 |
|
| 1654 |
pytest.raises(TypeError, ffi.def_extern(42), fn) |
| 1655 |
pytest.raises((TypeError, AttributeError), ffi.def_extern(), "foo") |
| 1656 |
class X: |
| 1657 |
pass |
| 1658 |
x = X() |
| 1659 |
x.__name__ = x |
| 1660 |
pytest.raises(TypeError, ffi.def_extern(), x) |
| 1661 |
|
| 1662 |
def test_extern_python_bogus_result_type(): |
| 1663 |
ffi = FFI() |
| 1664 |
ffi.cdef("""extern "Python" void bar(int);""") |
| 1665 |
lib = verify(ffi, 'test_extern_python_bogus_result_type', "") |
| 1666 |
|
| 1667 |
@ffi.def_extern() |
| 1668 |
def bar(n): |
| 1669 |
return n * 10 |
| 1670 |
with StdErrCapture() as f: |
| 1671 |
res = lib.bar(321) |
| 1672 |
assert res is None |
| 1673 |
msg = f.getvalue() |
| 1674 |
assert "rom cffi callback %r" % (bar,) in msg |
| 1675 |
assert "rying to convert the result back to C:\n" in msg |
| 1676 |
assert msg.endswith( |
| 1677 |
"TypeError: callback with the return type 'void' must return None\n") |
| 1678 |
|
| 1679 |
def test_extern_python_redefine(): |
| 1680 |
ffi = FFI() |
| 1681 |
ffi.cdef("""extern "Python" int bar(int);""") |
| 1682 |
lib = verify(ffi, 'test_extern_python_redefine', "") |
| 1683 |
|
| 1684 |
@ffi.def_extern() |
| 1685 |
def bar(n): |
| 1686 |
return n * 10 |
| 1687 |
assert lib.bar(42) == 420 |
| 1688 |
|
| 1689 |
@ffi.def_extern() |
| 1690 |
def bar(n): |
| 1691 |
return -n |
| 1692 |
assert lib.bar(42) == -42 |
| 1693 |
|
| 1694 |
def test_extern_python_struct(): |
| 1695 |
ffi = FFI() |
| 1696 |
ffi.cdef(""" |
| 1697 |
struct foo_s { int a, b, c; }; |
| 1698 |
extern "Python" int bar(int, struct foo_s, int); |
| 1699 |
extern "Python" { struct foo_s baz(int, int); |
| 1700 |
struct foo_s bok(void); } |
| 1701 |
""") |
| 1702 |
lib = verify(ffi, 'test_extern_python_struct', |
| 1703 |
"struct foo_s { int a, b, c; };") |
| 1704 |
|
| 1705 |
@ffi.def_extern() |
| 1706 |
def bar(x, s, z): |
| 1707 |
return x + s.a + s.b + s.c + z |
| 1708 |
res = lib.bar(1000, [1001, 1002, 1004], 1008) |
| 1709 |
assert res == 5015 |
| 1710 |
|
| 1711 |
@ffi.def_extern() |
| 1712 |
def baz(x, y): |
| 1713 |
return [x + y, x - y, x * y] |
| 1714 |
res = lib.baz(1000, 42) |
| 1715 |
assert res.a == 1042 |
| 1716 |
assert res.b == 958 |
| 1717 |
assert res.c == 42000 |
| 1718 |
|
| 1719 |
@ffi.def_extern() |
| 1720 |
def bok(): |
| 1721 |
return [10, 20, 30] |
| 1722 |
res = lib.bok() |
| 1723 |
assert [res.a, res.b, res.c] == [10, 20, 30] |
| 1724 |
|
| 1725 |
def test_extern_python_long_double(): |
| 1726 |
ffi = FFI() |
| 1727 |
ffi.cdef(""" |
| 1728 |
extern "Python" int bar(int, long double, int); |
| 1729 |
extern "Python" long double baz(int, int); |
| 1730 |
extern "Python" long double bok(void); |
| 1731 |
""") |
| 1732 |
lib = verify(ffi, 'test_extern_python_long_double', "") |
| 1733 |
|
| 1734 |
@ffi.def_extern() |
| 1735 |
def bar(x, l, z): |
| 1736 |
seen.append((x, l, z)) |
| 1737 |
return 6 |
| 1738 |
seen = [] |
| 1739 |
lib.bar(10, 3.5, 20) |
| 1740 |
expected = ffi.cast("long double", 3.5) |
| 1741 |
assert repr(seen) == repr([(10, expected, 20)]) |
| 1742 |
|
| 1743 |
@ffi.def_extern() |
| 1744 |
def baz(x, z): |
| 1745 |
assert x == 10 and z == 20 |
| 1746 |
return expected |
| 1747 |
res = lib.baz(10, 20) |
| 1748 |
assert repr(res) == repr(expected) |
| 1749 |
|
| 1750 |
@ffi.def_extern() |
| 1751 |
def bok(): |
| 1752 |
return expected |
| 1753 |
res = lib.bok() |
| 1754 |
assert repr(res) == repr(expected) |
| 1755 |
|
| 1756 |
def test_extern_python_signature(): |
| 1757 |
ffi = FFI() |
| 1758 |
lib = verify(ffi, 'test_extern_python_signature', "") |
| 1759 |
pytest.raises(TypeError, ffi.def_extern(425), None) |
| 1760 |
pytest.raises(TypeError, ffi.def_extern, 'a', 'b', 'c', 'd') |
| 1761 |
|
| 1762 |
def test_extern_python_errors(): |
| 1763 |
ffi = FFI() |
| 1764 |
ffi.cdef(""" |
| 1765 |
extern "Python" int bar(int); |
| 1766 |
""") |
| 1767 |
lib = verify(ffi, 'test_extern_python_errors', "") |
| 1768 |
|
| 1769 |
seen = [] |
| 1770 |
def oops(*args): |
| 1771 |
seen.append(args) |
| 1772 |
|
| 1773 |
@ffi.def_extern(onerror=oops) |
| 1774 |
def bar(x): |
| 1775 |
return x + "" |
| 1776 |
assert lib.bar(10) == 0 |
| 1777 |
|
| 1778 |
@ffi.def_extern(name="bar", onerror=oops, error=-66) |
| 1779 |
def bar2(x): |
| 1780 |
return x + "" |
| 1781 |
assert lib.bar(10) == -66 |
| 1782 |
|
| 1783 |
assert len(seen) == 2 |
| 1784 |
exc, val, tb = seen[0] |
| 1785 |
assert exc is TypeError |
| 1786 |
assert isinstance(val, TypeError) |
| 1787 |
assert tb.tb_frame.f_code.co_name == "bar" |
| 1788 |
exc, val, tb = seen[1] |
| 1789 |
assert exc is TypeError |
| 1790 |
assert isinstance(val, TypeError) |
| 1791 |
assert tb.tb_frame.f_code.co_name == "bar2" |
| 1792 |
|
| 1793 |
|
| 1794 |
pytest.raises(TypeError, ffi.def_extern(name='bar', onerror=42), |
| 1795 |
lambda x: x) |
| 1796 |
|
| 1797 |
def test_extern_python_stdcall(): |
| 1798 |
ffi = FFI() |
| 1799 |
ffi.cdef(""" |
| 1800 |
extern "Python" int __stdcall foo(int); |
| 1801 |
extern "Python" int WINAPI bar(int); |
| 1802 |
static int (__stdcall * mycb1)(int); |
| 1803 |
static int indirect_call(int); |
| 1804 |
""") |
| 1805 |
lib = verify(ffi, 'test_extern_python_stdcall', """ |
| 1806 |
#ifndef _MSC_VER |
| 1807 |
# define __stdcall |
| 1808 |
#endif |
| 1809 |
static int (__stdcall * mycb1)(int); |
| 1810 |
static int indirect_call(int x) { |
| 1811 |
return mycb1(x); |
| 1812 |
} |
| 1813 |
""") |
| 1814 |
|
| 1815 |
@ffi.def_extern() |
| 1816 |
def foo(x): |
| 1817 |
return x + 42 |
| 1818 |
@ffi.def_extern() |
| 1819 |
def bar(x): |
| 1820 |
return x + 43 |
| 1821 |
assert lib.foo(100) == 142 |
| 1822 |
assert lib.bar(100) == 143 |
| 1823 |
lib.mycb1 = lib.foo |
| 1824 |
assert lib.mycb1(200) == 242 |
| 1825 |
assert lib.indirect_call(300) == 342 |
| 1826 |
|
| 1827 |
def test_extern_python_plus_c(): |
| 1828 |
ffi = FFI() |
| 1829 |
ffi.cdef(""" |
| 1830 |
extern "Python+C" int foo(int); |
| 1831 |
extern "C +\tPython" int bar(int); |
| 1832 |
int call_me(int); |
| 1833 |
""") |
| 1834 |
lib = verify(ffi, 'test_extern_python_plus_c', """ |
| 1835 |
int foo(int); |
| 1836 |
#ifdef __GNUC__ |
| 1837 |
__attribute__((visibility("hidden"))) |
| 1838 |
#endif |
| 1839 |
int bar(int); |
| 1840 |
|
| 1841 |
static int call_me(int x) { |
| 1842 |
return foo(x) - bar(x); |
| 1843 |
} |
| 1844 |
""") |
| 1845 |
|
| 1846 |
@ffi.def_extern() |
| 1847 |
def foo(x): |
| 1848 |
return x * 42 |
| 1849 |
@ffi.def_extern() |
| 1850 |
def bar(x): |
| 1851 |
return x * 63 |
| 1852 |
assert lib.foo(100) == 4200 |
| 1853 |
assert lib.bar(100) == 6300 |
| 1854 |
assert lib.call_me(100) == -2100 |
| 1855 |
|
| 1856 |
def test_introspect_function(): |
| 1857 |
ffi = FFI() |
| 1858 |
ffi.cdef("float f1(double);") |
| 1859 |
lib = verify(ffi, 'test_introspect_function', """ |
| 1860 |
float f1(double x) { return (float)x; } |
| 1861 |
""") |
| 1862 |
assert dir(lib) == ['f1'] |
| 1863 |
FUNC = ffi.typeof(lib.f1) |
| 1864 |
assert FUNC.kind == 'function' |
| 1865 |
assert FUNC.args[0].cname == 'double' |
| 1866 |
assert FUNC.result.cname == 'float' |
| 1867 |
assert ffi.typeof(ffi.addressof(lib, 'f1')) is FUNC |
| 1868 |
|
| 1869 |
def test_introspect_global_var(): |
| 1870 |
ffi = FFI() |
| 1871 |
ffi.cdef("extern float g1;") |
| 1872 |
lib = verify(ffi, 'test_introspect_global_var', """ |
| 1873 |
float g1; |
| 1874 |
""") |
| 1875 |
assert dir(lib) == ['g1'] |
| 1876 |
FLOATPTR = ffi.typeof(ffi.addressof(lib, 'g1')) |
| 1877 |
assert FLOATPTR.kind == 'pointer' |
| 1878 |
assert FLOATPTR.item.cname == 'float' |
| 1879 |
|
| 1880 |
def test_introspect_global_var_array(): |
| 1881 |
ffi = FFI() |
| 1882 |
ffi.cdef("extern float g1[100];") |
| 1883 |
lib = verify(ffi, 'test_introspect_global_var_array', """ |
| 1884 |
float g1[100]; |
| 1885 |
""") |
| 1886 |
assert dir(lib) == ['g1'] |
| 1887 |
FLOATARRAYPTR = ffi.typeof(ffi.addressof(lib, 'g1')) |
| 1888 |
assert FLOATARRAYPTR.kind == 'pointer' |
| 1889 |
assert FLOATARRAYPTR.item.kind == 'array' |
| 1890 |
assert FLOATARRAYPTR.item.length == 100 |
| 1891 |
assert ffi.typeof(lib.g1) is FLOATARRAYPTR.item |
| 1892 |
|
| 1893 |
def test_introspect_integer_const(): |
| 1894 |
ffi = FFI() |
| 1895 |
ffi.cdef("#define FOO 42") |
| 1896 |
lib = verify(ffi, 'test_introspect_integer_const', """ |
| 1897 |
#define FOO 42 |
| 1898 |
""") |
| 1899 |
assert dir(lib) == ['FOO'] |
| 1900 |
assert lib.FOO == ffi.integer_const('FOO') == 42 |
| 1901 |
|
| 1902 |
def test_introspect_typedef(): |
| 1903 |
ffi = FFI() |
| 1904 |
ffi.cdef("typedef int foo_t;") |
| 1905 |
lib = verify(ffi, 'test_introspect_typedef', """ |
| 1906 |
typedef int foo_t; |
| 1907 |
""") |
| 1908 |
assert ffi.list_types() == (['foo_t'], [], []) |
| 1909 |
assert ffi.typeof('foo_t').kind == 'primitive' |
| 1910 |
assert ffi.typeof('foo_t').cname == 'int' |
| 1911 |
|
| 1912 |
def test_introspect_typedef_multiple(): |
| 1913 |
ffi = FFI() |
| 1914 |
ffi.cdef("typedef signed char a_t, c_t, g_t, b_t;") |
| 1915 |
lib = verify(ffi, 'test_introspect_typedef_multiple', """ |
| 1916 |
typedef signed char a_t, c_t, g_t, b_t; |
| 1917 |
""") |
| 1918 |
assert ffi.list_types() == (['a_t', 'b_t', 'c_t', 'g_t'], [], []) |
| 1919 |
|
| 1920 |
def test_introspect_struct(): |
| 1921 |
ffi = FFI() |
| 1922 |
ffi.cdef("struct foo_s { int a; };") |
| 1923 |
lib = verify(ffi, 'test_introspect_struct', """ |
| 1924 |
struct foo_s { int a; }; |
| 1925 |
""") |
| 1926 |
assert ffi.list_types() == ([], ['foo_s'], []) |
| 1927 |
assert ffi.typeof('struct foo_s').kind == 'struct' |
| 1928 |
assert ffi.typeof('struct foo_s').cname == 'struct foo_s' |
| 1929 |
|
| 1930 |
def test_introspect_union(): |
| 1931 |
ffi = FFI() |
| 1932 |
ffi.cdef("union foo_s { int a; };") |
| 1933 |
lib = verify(ffi, 'test_introspect_union', """ |
| 1934 |
union foo_s { int a; }; |
| 1935 |
""") |
| 1936 |
assert ffi.list_types() == ([], [], ['foo_s']) |
| 1937 |
assert ffi.typeof('union foo_s').kind == 'union' |
| 1938 |
assert ffi.typeof('union foo_s').cname == 'union foo_s' |
| 1939 |
|
| 1940 |
def test_introspect_struct_and_typedef(): |
| 1941 |
ffi = FFI() |
| 1942 |
ffi.cdef("typedef struct { int a; } foo_t;") |
| 1943 |
lib = verify(ffi, 'test_introspect_struct_and_typedef', """ |
| 1944 |
typedef struct { int a; } foo_t; |
| 1945 |
""") |
| 1946 |
assert ffi.list_types() == (['foo_t'], [], []) |
| 1947 |
assert ffi.typeof('foo_t').kind == 'struct' |
| 1948 |
assert ffi.typeof('foo_t').cname == 'foo_t' |
| 1949 |
|
| 1950 |
def test_introspect_included_type(): |
| 1951 |
SOURCE = """ |
| 1952 |
typedef signed char schar_t; |
| 1953 |
struct sint_t { int x; }; |
| 1954 |
""" |
| 1955 |
ffi1 = FFI() |
| 1956 |
ffi1.cdef(SOURCE) |
| 1957 |
ffi2 = FFI() |
| 1958 |
ffi2.include(ffi1) |
| 1959 |
verify(ffi1, "test_introspect_included_type_parent", SOURCE) |
| 1960 |
verify(ffi2, "test_introspect_included_type", SOURCE) |
| 1961 |
assert ffi1.list_types() == ffi2.list_types() == ( |
| 1962 |
['schar_t'], ['sint_t'], []) |
| 1963 |
|
| 1964 |
def test_introspect_order(): |
| 1965 |
ffi = FFI() |
| 1966 |
ffi.cdef("union CFFIaaa { int a; }; typedef struct CFFIccc { int a; } CFFIb;") |
| 1967 |
ffi.cdef("union CFFIg { int a; }; typedef struct CFFIcc { int a; } CFFIbbb;") |
| 1968 |
ffi.cdef("union CFFIaa { int a; }; typedef struct CFFIa { int a; } CFFIbb;") |
| 1969 |
verify(ffi, "test_introspect_order", """ |
| 1970 |
union CFFIaaa { int a; }; typedef struct CFFIccc { int a; } CFFIb; |
| 1971 |
union CFFIg { int a; }; typedef struct CFFIcc { int a; } CFFIbbb; |
| 1972 |
union CFFIaa { int a; }; typedef struct CFFIa { int a; } CFFIbb; |
| 1973 |
""") |
| 1974 |
assert ffi.list_types() == (['CFFIb', 'CFFIbb', 'CFFIbbb'], |
| 1975 |
['CFFIa', 'CFFIcc', 'CFFIccc'], |
| 1976 |
['CFFIaa', 'CFFIaaa', 'CFFIg']) |
| 1977 |
|
| 1978 |
def test_bool_in_cpp(): |
| 1979 |
|
| 1980 |
ffi = FFI() |
| 1981 |
ffi.cdef("bool f(void);") |
| 1982 |
lib = verify(ffi, "test_bool_in_cpp", "char f(void) { return 2; }") |
| 1983 |
assert lib.f() is True |
| 1984 |
|
| 1985 |
def test_bool_in_cpp_2(): |
| 1986 |
ffi = FFI() |
| 1987 |
ffi.cdef('int add(int a, int b);') |
| 1988 |
lib = verify(ffi, "test_bool_bug_cpp", ''' |
| 1989 |
typedef bool _Bool; /* there is a Windows header with this line */ |
| 1990 |
int add(int a, int b) |
| 1991 |
{ |
| 1992 |
return a + b; |
| 1993 |
}''', source_extension='.cpp') |
| 1994 |
c = lib.add(2, 3) |
| 1995 |
assert c == 5 |
| 1996 |
|
| 1997 |
def test_struct_field_opaque(): |
| 1998 |
ffi = FFI() |
| 1999 |
ffi.cdef("struct a { struct b b; };") |
| 2000 |
e = pytest.raises(TypeError, verify, |
| 2001 |
ffi, "test_struct_field_opaque", "?") |
| 2002 |
assert str(e.value) == ("struct a: field 'a.b' is of an opaque" |
| 2003 |
" type (not declared in cdef())") |
| 2004 |
ffi = FFI() |
| 2005 |
ffi.cdef("struct a { struct b b[2]; };") |
| 2006 |
e = pytest.raises(TypeError, verify, |
| 2007 |
ffi, "test_struct_field_opaque", "?") |
| 2008 |
assert str(e.value) == ("struct a: field 'a.b' is of an opaque" |
| 2009 |
" type (not declared in cdef())") |
| 2010 |
ffi = FFI() |
| 2011 |
ffi.cdef("struct a { struct b b[]; };") |
| 2012 |
e = pytest.raises(TypeError, verify, |
| 2013 |
ffi, "test_struct_field_opaque", "?") |
| 2014 |
assert str(e.value) == ("struct a: field 'a.b' is of an opaque" |
| 2015 |
" type (not declared in cdef())") |
| 2016 |
|
| 2017 |
def test_function_arg_opaque(): |
| 2018 |
pytest.skip("can currently declare a function with an opaque struct " |
| 2019 |
"as argument, but AFAICT it's impossible to call it later") |
| 2020 |
|
| 2021 |
def test_function_returns_opaque(): |
| 2022 |
ffi = FFI() |
| 2023 |
ffi.cdef("struct a foo(int);") |
| 2024 |
e = pytest.raises(TypeError, verify, |
| 2025 |
ffi, "test_function_returns_opaque", "?") |
| 2026 |
assert str(e.value) == ("function foo: 'struct a' is used as result type," |
| 2027 |
" but is opaque") |
| 2028 |
|
| 2029 |
def test_function_returns_union(): |
| 2030 |
ffi = FFI() |
| 2031 |
ffi.cdef("union u1 { int a, b; }; union u1 f1(int);") |
| 2032 |
lib = verify(ffi, "test_function_returns_union", """ |
| 2033 |
union u1 { int a, b; }; |
| 2034 |
static union u1 f1(int x) { union u1 u; u.b = x; return u; } |
| 2035 |
""") |
| 2036 |
assert lib.f1(51).a == 51 |
| 2037 |
|
| 2038 |
def test_function_returns_partial_struct(): |
| 2039 |
ffi = FFI() |
| 2040 |
ffi.cdef("struct aaa { int a; ...; }; struct aaa f1(int);") |
| 2041 |
lib = verify(ffi, "test_function_returns_partial_struct", """ |
| 2042 |
struct aaa { int b, a, c; }; |
| 2043 |
static struct aaa f1(int x) { struct aaa s = {0}; s.a = x; return s; } |
| 2044 |
""") |
| 2045 |
assert lib.f1(52).a == 52 |
| 2046 |
|
| 2047 |
def test_function_returns_float_complex(): |
| 2048 |
ffi = FFI() |
| 2049 |
ffi.cdef("float _Complex f1(float a, float b);"); |
| 2050 |
if sys.platform == 'win32': |
| 2051 |
lib = verify(ffi, "test_function_returns_float_complex", """ |
| 2052 |
#include <complex.h> |
| 2053 |
static _Fcomplex f1(float a, float b) { return _FCbuild(a, 2.0f*b); } |
| 2054 |
""") |
| 2055 |
else: |
| 2056 |
lib = verify(ffi, "test_function_returns_float_complex", """ |
| 2057 |
#include <complex.h> |
| 2058 |
static float _Complex f1(float a, float b) { return a + I*2.0f*b; } |
| 2059 |
""", no_cpp=True) |
| 2060 |
result = lib.f1(1.25, 5.1) |
| 2061 |
assert type(result) == complex |
| 2062 |
assert result.real == 1.25 |
| 2063 |
assert (result.imag != 2*5.1) and (abs(result.imag - 2*5.1) < 1e-5) |
| 2064 |
|
| 2065 |
def test_function_returns_double_complex(): |
| 2066 |
ffi = FFI() |
| 2067 |
ffi.cdef("double _Complex f1(double a, double b);"); |
| 2068 |
if sys.platform == 'win32': |
| 2069 |
lib = verify(ffi, "test_function_returns_double_complex", """ |
| 2070 |
#include <complex.h> |
| 2071 |
static _Dcomplex f1(double a, double b) { return _Cbuild(a, 2.0*b); } |
| 2072 |
""") |
| 2073 |
else: |
| 2074 |
lib = verify(ffi, "test_function_returns_double_complex", """ |
| 2075 |
#include <complex.h> |
| 2076 |
static double _Complex f1(double a, double b) { return a + I*2.0*b; } |
| 2077 |
""", no_cpp=True) |
| 2078 |
result = lib.f1(1.25, 5.1) |
| 2079 |
assert type(result) == complex |
| 2080 |
assert result.real == 1.25 |
| 2081 |
assert result.imag == 2*5.1 |
| 2082 |
|
| 2083 |
def test_cdef_using_windows_complex(): |
| 2084 |
if sys.platform != 'win32': |
| 2085 |
pytest.skip("only for MSVC") |
| 2086 |
ffi = FFI() |
| 2087 |
ffi.cdef("_Fcomplex f1(float a, float b); _Dcomplex f2(double a, double b);"); |
| 2088 |
lib = verify(ffi, "test_cdef_using_windows_complex", """ |
| 2089 |
#include <complex.h> |
| 2090 |
static _Fcomplex f1(float a, float b) { return _FCbuild(a, 2.0f*b); } |
| 2091 |
static _Dcomplex f2(double a, double b) { return _Cbuild(a, 2.0*b); } |
| 2092 |
""") |
| 2093 |
result = lib.f1(1.25, 5.1) |
| 2094 |
assert type(result) == complex |
| 2095 |
assert result.real == 1.25 |
| 2096 |
assert (result.imag != 2*5.1) and (abs(result.imag - 2*5.1) < 1e-5) |
| 2097 |
result = lib.f2(1.25, 5.1) |
| 2098 |
assert type(result) == complex |
| 2099 |
assert result.real == 1.25 |
| 2100 |
assert result.imag == 2*5.1 |
| 2101 |
|
| 2102 |
def test_function_argument_float_complex(): |
| 2103 |
ffi = FFI() |
| 2104 |
ffi.cdef("float f1(float _Complex x);"); |
| 2105 |
if sys.platform == 'win32': |
| 2106 |
lib = verify(ffi, "test_function_argument_float_complex", """ |
| 2107 |
#include <complex.h> |
| 2108 |
static float f1(_Fcomplex x) { return cabsf(x); } |
| 2109 |
""") |
| 2110 |
else: |
| 2111 |
lib = verify(ffi, "test_function_argument_float_complex", """ |
| 2112 |
#include <complex.h> |
| 2113 |
static float f1(float _Complex x) { return cabsf(x); } |
| 2114 |
""", no_cpp=True) |
| 2115 |
x = complex(12.34, 56.78) |
| 2116 |
result = lib.f1(x) |
| 2117 |
assert abs(result - abs(x)) < 1e-5 |
| 2118 |
|
| 2119 |
def test_function_argument_double_complex(): |
| 2120 |
ffi = FFI() |
| 2121 |
ffi.cdef("double f1(double _Complex);"); |
| 2122 |
if sys.platform == 'win32': |
| 2123 |
lib = verify(ffi, "test_function_argument_double_complex", """ |
| 2124 |
#include <complex.h> |
| 2125 |
static double f1(_Dcomplex x) { return cabs(x); } |
| 2126 |
""") |
| 2127 |
else: |
| 2128 |
lib = verify(ffi, "test_function_argument_double_complex", """ |
| 2129 |
#include <complex.h> |
| 2130 |
static double f1(double _Complex x) { return cabs(x); } |
| 2131 |
""", no_cpp=True) |
| 2132 |
x = complex(12.34, 56.78) |
| 2133 |
result = lib.f1(x) |
| 2134 |
assert abs(result - abs(x)) < 1e-11 |
| 2135 |
|
| 2136 |
def test_typedef_array_dotdotdot(): |
| 2137 |
ffi = FFI() |
| 2138 |
ffi.cdef(""" |
| 2139 |
typedef int foo_t[...], bar_t[...]; |
| 2140 |
extern int gv[...]; |
| 2141 |
typedef int mat_t[...][...]; |
| 2142 |
typedef int vmat_t[][...]; |
| 2143 |
""") |
| 2144 |
lib = verify(ffi, "test_typedef_array_dotdotdot", """ |
| 2145 |
typedef int foo_t[50], bar_t[50]; |
| 2146 |
int gv[23]; |
| 2147 |
typedef int mat_t[6][7]; |
| 2148 |
typedef int vmat_t[][8]; |
| 2149 |
""") |
| 2150 |
assert ffi.sizeof("foo_t") == 50 * ffi.sizeof("int") |
| 2151 |
assert ffi.sizeof("bar_t") == 50 * ffi.sizeof("int") |
| 2152 |
assert len(ffi.new("foo_t")) == 50 |
| 2153 |
assert len(ffi.new("bar_t")) == 50 |
| 2154 |
assert ffi.sizeof(lib.gv) == 23 * ffi.sizeof("int") |
| 2155 |
assert ffi.sizeof("mat_t") == 6 * 7 * ffi.sizeof("int") |
| 2156 |
assert len(ffi.new("mat_t")) == 6 |
| 2157 |
assert len(ffi.new("mat_t")[3]) == 7 |
| 2158 |
pytest.raises(ffi.error, ffi.sizeof, "vmat_t") |
| 2159 |
p = ffi.new("vmat_t", 4) |
| 2160 |
assert ffi.sizeof(p[3]) == 8 * ffi.sizeof("int") |
| 2161 |
|
| 2162 |
def test_typedef_array_dotdotdot_usage(): |
| 2163 |
ffi = FFI() |
| 2164 |
ffi.cdef(""" |
| 2165 |
typedef int foo_t[...]; |
| 2166 |
typedef int mat_t[...][...]; |
| 2167 |
struct s { foo_t a; foo_t *b; foo_t **c; }; |
| 2168 |
int myfunc(foo_t a, foo_t *b, foo_t **c); |
| 2169 |
struct sm { mat_t a; mat_t *b; mat_t **c; }; |
| 2170 |
int myfuncm(mat_t a, mat_t *b, mat_t **c); |
| 2171 |
""") |
| 2172 |
lib = verify(ffi, "test_typedef_array_dotdotdot_usage", """ |
| 2173 |
typedef int foo_t[50]; |
| 2174 |
typedef int mat_t[6][7]; |
| 2175 |
struct s { foo_t a; foo_t *b; foo_t **c; }; |
| 2176 |
static int myfunc(foo_t a, foo_t *b, foo_t **c) { return (**c)[49]; } |
| 2177 |
struct sm { mat_t a; mat_t *b; mat_t **c; }; |
| 2178 |
static int myfuncm(mat_t a, mat_t *b, mat_t **c) { return (**c)[5][6]; } |
| 2179 |
""") |
| 2180 |
assert ffi.sizeof("foo_t") == 50 * ffi.sizeof("int") |
| 2181 |
p = ffi.new("struct s *") |
| 2182 |
assert ffi.sizeof(p[0]) == 50 * ffi.sizeof("int") + 2 * ffi.sizeof("void *") |
| 2183 |
p.a[49] = 321 |
| 2184 |
p.b = ffi.addressof(p, 'a') |
| 2185 |
p.c = ffi.addressof(p, 'b') |
| 2186 |
assert lib.myfunc(ffi.NULL, ffi.NULL, p.c) == 321 |
| 2187 |
|
| 2188 |
assert ffi.sizeof("mat_t") == 42 * ffi.sizeof("int") |
| 2189 |
p = ffi.new("struct sm *") |
| 2190 |
assert ffi.sizeof(p[0]) == 42 * ffi.sizeof("int") + 2 * ffi.sizeof("void *") |
| 2191 |
p.a[5][6] = -321 |
| 2192 |
p.b = ffi.addressof(p, 'a') |
| 2193 |
p.c = ffi.addressof(p, 'b') |
| 2194 |
assert lib.myfuncm(ffi.NULL, ffi.NULL, p.c) == -321 |
| 2195 |
|
| 2196 |
def test_call_with_custom_field_pos(): |
| 2197 |
ffi = FFI() |
| 2198 |
ffi.cdef(""" |
| 2199 |
struct foo { int x; ...; }; |
| 2200 |
struct foo f(void); |
| 2201 |
struct foo g(int, ...); |
| 2202 |
""") |
| 2203 |
lib = verify(ffi, "test_call_with_custom_field_pos", """ |
| 2204 |
struct foo { int y, x; }; |
| 2205 |
struct foo f(void) { |
| 2206 |
struct foo s = { 40, 200 }; |
| 2207 |
return s; |
| 2208 |
} |
| 2209 |
struct foo g(int a, ...) { return f(); } |
| 2210 |
""") |
| 2211 |
assert lib.f().x == 200 |
| 2212 |
e = pytest.raises(NotImplementedError, lib.g, 0) |
| 2213 |
assert str(e.value) == ( |
| 2214 |
'ctype \'struct foo\' not supported as return value. It is a ' |
| 2215 |
'struct declared with "...;", but the C calling convention may ' |
| 2216 |
'depend on the missing fields; or, it contains anonymous ' |
| 2217 |
'struct/unions. Such structs are only supported ' |
| 2218 |
'as return value if the function is \'API mode\' and non-variadic ' |
| 2219 |
'(i.e. declared inside ffibuilder.cdef()+ffibuilder.set_source() ' |
| 2220 |
'and not taking a final \'...\' argument)') |
| 2221 |
|
| 2222 |
def test_call_with_nested_anonymous_struct(): |
| 2223 |
if sys.platform == 'win32': |
| 2224 |
pytest.skip("needs a GCC extension") |
| 2225 |
ffi = FFI() |
| 2226 |
ffi.cdef(""" |
| 2227 |
struct foo { int a; union { int b, c; }; }; |
| 2228 |
struct foo f(void); |
| 2229 |
struct foo g(int, ...); |
| 2230 |
""") |
| 2231 |
lib = verify(ffi, "test_call_with_nested_anonymous_struct", """ |
| 2232 |
struct foo { int a; union { int b, c; }; }; |
| 2233 |
struct foo f(void) { |
| 2234 |
struct foo s; |
| 2235 |
s.a = 40; |
| 2236 |
s.b = 200; |
| 2237 |
return s; |
| 2238 |
} |
| 2239 |
struct foo g(int a, ...) { return f(); } |
| 2240 |
""") |
| 2241 |
assert lib.f().b == 200 |
| 2242 |
e = pytest.raises(NotImplementedError, lib.g, 0) |
| 2243 |
assert str(e.value) == ( |
| 2244 |
'ctype \'struct foo\' not supported as return value. It is a ' |
| 2245 |
'struct declared with "...;", but the C calling convention may ' |
| 2246 |
'depend on the missing fields; or, it contains anonymous ' |
| 2247 |
'struct/unions. Such structs are only supported ' |
| 2248 |
'as return value if the function is \'API mode\' and non-variadic ' |
| 2249 |
'(i.e. declared inside ffibuilder.cdef()+ffibuilder.set_source() ' |
| 2250 |
'and not taking a final \'...\' argument)') |
| 2251 |
|
| 2252 |
def test_call_with_bitfield(): |
| 2253 |
ffi = FFI() |
| 2254 |
ffi.cdef(""" |
| 2255 |
struct foo { int x:5; }; |
| 2256 |
struct foo f(void); |
| 2257 |
struct foo g(int, ...); |
| 2258 |
""") |
| 2259 |
lib = verify(ffi, "test_call_with_bitfield", """ |
| 2260 |
struct foo { int x:5; }; |
| 2261 |
struct foo f(void) { |
| 2262 |
struct foo s = { 11 }; |
| 2263 |
return s; |
| 2264 |
} |
| 2265 |
struct foo g(int a, ...) { return f(); } |
| 2266 |
""") |
| 2267 |
assert lib.f().x == 11 |
| 2268 |
e = pytest.raises(NotImplementedError, lib.g, 0) |
| 2269 |
assert str(e.value) == ( |
| 2270 |
"ctype 'struct foo' not supported as return value. It is a struct " |
| 2271 |
"with bit fields, which libffi does not support. Such structs are " |
| 2272 |
"only supported as return value if the function is 'API mode' and " |
| 2273 |
"non-variadic (i.e. declared inside ffibuilder.cdef()+ffibuilder." |
| 2274 |
"set_source() and not taking a final '...' argument)") |
| 2275 |
|
| 2276 |
def test_call_with_zero_length_field(): |
| 2277 |
if sys.platform == 'win32': |
| 2278 |
pytest.skip("zero-length field not supported by MSVC") |
| 2279 |
ffi = FFI() |
| 2280 |
ffi.cdef(""" |
| 2281 |
struct foo { int a; int x[0]; }; |
| 2282 |
struct foo f(void); |
| 2283 |
struct foo g(int, ...); |
| 2284 |
""") |
| 2285 |
lib = verify(ffi, "test_call_with_zero_length_field", """ |
| 2286 |
struct foo { int a; int x[0]; }; |
| 2287 |
struct foo f(void) { |
| 2288 |
struct foo s = { 42 }; |
| 2289 |
return s; |
| 2290 |
} |
| 2291 |
struct foo g(int a, ...) { return f(); } |
| 2292 |
""") |
| 2293 |
assert lib.f().a == 42 |
| 2294 |
e = pytest.raises(NotImplementedError, lib.g, 0) |
| 2295 |
assert str(e.value) == ( |
| 2296 |
"ctype 'struct foo' not supported as return value. It is a " |
| 2297 |
"struct with a zero-length array, which libffi does not support." |
| 2298 |
" Such structs are only supported as return value if the function is " |
| 2299 |
"'API mode' and non-variadic (i.e. declared inside ffibuilder.cdef()" |
| 2300 |
"+ffibuilder.set_source() and not taking a final '...' argument)") |
| 2301 |
|
| 2302 |
def test_call_with_union(): |
| 2303 |
ffi = FFI() |
| 2304 |
ffi.cdef(""" |
| 2305 |
union foo { int a; char b; }; |
| 2306 |
union foo f(void); |
| 2307 |
union foo g(int, ...); |
| 2308 |
""") |
| 2309 |
lib = verify(ffi, "test_call_with_union", """ |
| 2310 |
union foo { int a; char b; }; |
| 2311 |
union foo f(void) { |
| 2312 |
union foo s = { 42 }; |
| 2313 |
return s; |
| 2314 |
} |
| 2315 |
union foo g(int a, ...) { return f(); } |
| 2316 |
""") |
| 2317 |
assert lib.f().a == 42 |
| 2318 |
e = pytest.raises(NotImplementedError, lib.g, 0) |
| 2319 |
assert str(e.value) == ( |
| 2320 |
"ctype 'union foo' not supported as return value by libffi. " |
| 2321 |
"Unions are only supported as return value if the function is " |
| 2322 |
"'API mode' and non-variadic (i.e. declared inside ffibuilder.cdef()" |
| 2323 |
"+ffibuilder.set_source() and not taking a final '...' argument)") |
| 2324 |
|
| 2325 |
def test_call_with_packed_struct(): |
| 2326 |
if sys.platform == 'win32': |
| 2327 |
pytest.skip("needs a GCC extension") |
| 2328 |
ffi = FFI() |
| 2329 |
ffi.cdef(""" |
| 2330 |
struct foo { char y; int x; }; |
| 2331 |
struct foo f(void); |
| 2332 |
struct foo g(int, ...); |
| 2333 |
""", packed=True) |
| 2334 |
lib = verify(ffi, "test_call_with_packed_struct", """ |
| 2335 |
struct foo { char y; int x; } __attribute__((packed)); |
| 2336 |
struct foo f(void) { |
| 2337 |
struct foo s = { 40, 200 }; |
| 2338 |
return s; |
| 2339 |
} |
| 2340 |
struct foo g(int a, ...) { |
| 2341 |
struct foo s = { 41, 201 }; |
| 2342 |
return s; |
| 2343 |
} |
| 2344 |
""") |
| 2345 |
assert ord(lib.f().y) == 40 |
| 2346 |
assert lib.f().x == 200 |
| 2347 |
e = pytest.raises(NotImplementedError, lib.g, 0) |
| 2348 |
assert str(e.value) == ( |
| 2349 |
"ctype 'struct foo' not supported as return value. It is a " |
| 2350 |
"'packed' structure, with a different layout than expected by libffi." |
| 2351 |
" Such structs are only supported as return value if the function is " |
| 2352 |
"'API mode' and non-variadic (i.e. declared inside ffibuilder.cdef()" |
| 2353 |
"+ffibuilder.set_source() and not taking a final '...' argument)") |
| 2354 |
|
| 2355 |
def test_pack_not_supported(): |
| 2356 |
ffi = FFI() |
| 2357 |
ffi.cdef("""struct foo { char y; int x; };""", pack=2) |
| 2358 |
pytest.raises(NotImplementedError, verify, |
| 2359 |
ffi, "test_pack_not_supported", "") |
| 2360 |
|
| 2361 |
def test_gcc_visibility_hidden(): |
| 2362 |
if sys.platform == 'win32': |
| 2363 |
pytest.skip("test for gcc/clang") |
| 2364 |
ffi = FFI() |
| 2365 |
ffi.cdef(""" |
| 2366 |
int f(int); |
| 2367 |
""") |
| 2368 |
lib = verify(ffi, "test_gcc_visibility_hidden", """ |
| 2369 |
int f(int a) { return a + 40; } |
| 2370 |
""", extra_compile_args=['-fvisibility=hidden']) |
| 2371 |
assert lib.f(2) == 42 |
| 2372 |
|
| 2373 |
def test_override_default_definition(): |
| 2374 |
ffi = FFI() |
| 2375 |
ffi.cdef("typedef long int16_t, char16_t;") |
| 2376 |
lib = verify(ffi, "test_override_default_definition", "") |
| 2377 |
assert ffi.typeof("int16_t") is ffi.typeof("char16_t") is ffi.typeof("long") |
| 2378 |
|
| 2379 |
def test_char16_char32_type(no_cpp=False): |
| 2380 |
if no_cpp is False and sys.platform == "win32": |
| 2381 |
pytest.skip("aaaaaaa why do modern MSVC compilers still define " |
| 2382 |
"a very old __cplusplus value") |
| 2383 |
ffi = FFI() |
| 2384 |
ffi.cdef(""" |
| 2385 |
char16_t foo_2bytes(char16_t); |
| 2386 |
char32_t foo_4bytes(char32_t); |
| 2387 |
""") |
| 2388 |
lib = verify(ffi, "test_char16_char32_type" + no_cpp * "_nocpp", """ |
| 2389 |
#if !defined(__cplusplus) || (!defined(_LIBCPP_VERSION) && __cplusplus < 201103L) |
| 2390 |
typedef uint_least16_t char16_t; |
| 2391 |
typedef uint_least32_t char32_t; |
| 2392 |
#endif |
| 2393 |
|
| 2394 |
char16_t foo_2bytes(char16_t a) { return (char16_t)(a + 42); } |
| 2395 |
char32_t foo_4bytes(char32_t a) { return (char32_t)(a + 42); } |
| 2396 |
""", no_cpp=no_cpp) |
| 2397 |
assert lib.foo_2bytes(u+'\u1234') == u+'\u125e' |
| 2398 |
assert lib.foo_4bytes(u+'\u1234') == u+'\u125e' |
| 2399 |
assert lib.foo_4bytes(u+'\U00012345') == u+'\U0001236f' |
| 2400 |
pytest.raises(TypeError, lib.foo_2bytes, u+'\U00012345') |
| 2401 |
pytest.raises(TypeError, lib.foo_2bytes, 1234) |
| 2402 |
pytest.raises(TypeError, lib.foo_4bytes, 1234) |
| 2403 |
|
| 2404 |
def test_char16_char32_plain_c(): |
| 2405 |
test_char16_char32_type(no_cpp=True) |
| 2406 |
|
| 2407 |
def test_loader_spec(): |
| 2408 |
ffi = FFI() |
| 2409 |
lib = verify(ffi, "test_loader_spec", "") |
| 2410 |
if sys.version_info < (3,): |
| 2411 |
assert not hasattr(lib, '__loader__') |
| 2412 |
assert not hasattr(lib, '__spec__') |
| 2413 |
else: |
| 2414 |
assert lib.__loader__ is None |
| 2415 |
assert lib.__spec__ is None |
| 2416 |
|
| 2417 |
def test_realize_struct_error(): |
| 2418 |
ffi = FFI() |
| 2419 |
ffi.cdef("""typedef ... foo_t; struct foo_s { void (*x)(foo_t); };""") |
| 2420 |
lib = verify(ffi, "test_realize_struct_error", """ |
| 2421 |
typedef int foo_t; struct foo_s { void (*x)(foo_t); }; |
| 2422 |
""") |
| 2423 |
pytest.raises(TypeError, ffi.new, "struct foo_s *") |
| 2424 |
|
| 2425 |
def test_from_buffer_struct(): |
| 2426 |
ffi = FFI() |
| 2427 |
ffi.cdef("""struct foo_s { int a, b; };""") |
| 2428 |
lib = verify(ffi, "test_from_buffer_struct_p", """ |
| 2429 |
struct foo_s { int a, b; }; |
| 2430 |
""") |
| 2431 |
p = ffi.new("struct foo_s *", [-219239, 58974983]) |
| 2432 |
q = ffi.from_buffer("struct foo_s[]", ffi.buffer(p)) |
| 2433 |
assert ffi.typeof(q) == ffi.typeof("struct foo_s[]") |
| 2434 |
assert len(q) == 1 |
| 2435 |
assert q[0].a == p.a |
| 2436 |
assert q[0].b == p.b |
| 2437 |
assert q == p |
| 2438 |
q = ffi.from_buffer("struct foo_s *", ffi.buffer(p)) |
| 2439 |
assert ffi.typeof(q) == ffi.typeof("struct foo_s *") |
| 2440 |
assert q.a == p.a |
| 2441 |
assert q.b == p.b |
| 2442 |
assert q[0].a == p.a |
| 2443 |
assert q[0].b == p.b |
| 2444 |
assert q == p |
| 2445 |
|
| 2446 |
def test_unnamed_bitfield_1(): |
| 2447 |
ffi = FFI() |
| 2448 |
ffi.cdef("""struct A { char : 1; };""") |
| 2449 |
lib = verify(ffi, "test_unnamed_bitfield_1", """ |
| 2450 |
struct A { char : 1; }; |
| 2451 |
""") |
| 2452 |
p = ffi.new("struct A *") |
| 2453 |
assert ffi.sizeof(p[0]) == 1 |
| 2454 |
|
| 2455 |
|
| 2456 |
|
| 2457 |
|
| 2458 |
def test_unnamed_bitfield_2(): |
| 2459 |
ffi = FFI() |
| 2460 |
ffi.cdef("""struct A { |
| 2461 |
short c : 1; short : 1; short d : 1; short : 1; };""") |
| 2462 |
lib = verify(ffi, "test_unnamed_bitfield_2", """ |
| 2463 |
struct A { |
| 2464 |
short c : 1; short : 1; short d : 1; short : 1; |
| 2465 |
}; |
| 2466 |
""") |
| 2467 |
p = ffi.new("struct A *") |
| 2468 |
assert ffi.sizeof(p[0]) == ffi.sizeof("short") |
| 2469 |
|
| 2470 |
def test_unnamed_bitfield_3(): |
| 2471 |
ffi = FFI() |
| 2472 |
ffi.cdef("""struct A { struct { char : 1; char : 1; } b; };""") |
| 2473 |
lib = verify(ffi, "test_unnamed_bitfield_3", """ |
| 2474 |
struct A { struct { char : 1; char : 1; } b; }; |
| 2475 |
""") |
| 2476 |
p = ffi.new("struct A *") |
| 2477 |
assert ffi.sizeof(p[0]) == 1 |
| 2478 |
|
| 2479 |
|
| 2480 |
|
| 2481 |
|
| 2482 |
def test_unnamed_bitfield_4(): |
| 2483 |
ffi = FFI() |
| 2484 |
ffi.cdef("""struct A { struct { |
| 2485 |
unsigned c : 1; unsigned : 1; unsigned d : 1; unsigned : 1; } a; |
| 2486 |
}; |
| 2487 |
struct B { struct A a; };""") |
| 2488 |
lib = verify(ffi, "test_unnamed_bitfield_4", """ |
| 2489 |
struct A { struct { |
| 2490 |
unsigned c : 1; unsigned : 1; unsigned d : 1; unsigned : 1; } a; |
| 2491 |
}; |
| 2492 |
struct B { struct A a; }; |
| 2493 |
""") |
| 2494 |
b = ffi.new("struct B *") |
| 2495 |
a = ffi.new("struct A *") |
| 2496 |
assert ffi.sizeof(a[0]) == ffi.sizeof("unsigned") |
| 2497 |
assert ffi.sizeof(b[0]) == ffi.sizeof(a[0]) |
| 2498 |
|
| 2499 |
def test_struct_with_func_with_struct_pointer_arg(): |
| 2500 |
ffi = FFI() |
| 2501 |
ffi.cdef("""struct BinaryTree { |
| 2502 |
int (* CompareKey)(struct BinaryTree *tree); |
| 2503 |
};""") |
| 2504 |
lib = verify(ffi, "test_struct_with_func_with_struct_pointer_arg", """ |
| 2505 |
struct BinaryTree { |
| 2506 |
int (* CompareKey)(struct BinaryTree *tree); |
| 2507 |
}; |
| 2508 |
""") |
| 2509 |
ffi.new("struct BinaryTree *") |
| 2510 |
|
| 2511 |
def test_struct_with_func_with_struct_arg(): |
| 2512 |
ffi = FFI() |
| 2513 |
ffi.cdef("""struct BinaryTree { |
| 2514 |
int (* CompareKey)(struct BinaryTree tree); |
| 2515 |
};""") |
| 2516 |
lib = verify(ffi, "test_struct_with_func_with_struct_arg", """ |
| 2517 |
struct BinaryTree { |
| 2518 |
int (* CompareKey)(struct BinaryTree tree); |
| 2519 |
}; |
| 2520 |
""") |
| 2521 |
pytest.raises(RuntimeError, ffi.new, "struct BinaryTree *") |
| 2522 |
|
| 2523 |
def test_passing_large_list(): |
| 2524 |
ffi = FFI() |
| 2525 |
ffi.cdef("""void passing_large_list(long[]);""") |
| 2526 |
lib = verify(ffi, "test_passing_large_list", """ |
| 2527 |
static void passing_large_list(long a[]) { } |
| 2528 |
""") |
| 2529 |
arg = list(range(20000000)) |
| 2530 |
lib.passing_large_list(arg) |
| 2531 |
|
| 2532 |
|
| 2533 |
def test_convert_api_mode_builtin_function_to_cdata(): |
| 2534 |
ffi = FFI() |
| 2535 |
ffi.cdef( |
| 2536 |
"""struct s { int x; }; |
| 2537 |
struct s add1(struct s); struct s add2(struct s); |
| 2538 |
int mycall(struct s(*)(struct s)); int mycall2(void *);""") |
| 2539 |
lib = verify(ffi, "test_convert_api_mode_builtin_function_to_cdata", """ |
| 2540 |
struct s { int x; }; |
| 2541 |
static struct s add1(struct s a) { |
| 2542 |
struct s r; r.x = a.x + 1; return r; |
| 2543 |
} |
| 2544 |
static struct s add2(struct s a) { |
| 2545 |
struct s r; r.x = a.x + 2; return r; |
| 2546 |
} |
| 2547 |
static int mycall(struct s(*cb)(struct s)) { |
| 2548 |
struct s a; a.x = 100; |
| 2549 |
return cb(a).x; |
| 2550 |
} |
| 2551 |
static int mycall2(void *cb) { |
| 2552 |
struct s a; a.x = 200; |
| 2553 |
return ((struct s(*)(struct s))cb)(a).x; |
| 2554 |
} |
| 2555 |
""") |
| 2556 |
s_ptr = ffi.new("struct s *", [42]) |
| 2557 |
s = s_ptr[0] |
| 2558 |
assert lib.add1(s).x == 43 |
| 2559 |
assert lib.add2(s).x == 44 |
| 2560 |
assert lib.mycall(lib.add1) == 101 |
| 2561 |
assert lib.mycall(lib.add2) == 102 |
| 2562 |
assert lib.mycall2(lib.add1) == 201 |
| 2563 |
assert lib.mycall2(lib.add2) == 202 |
| 2564 |
s.x = -42 |
| 2565 |
my_array = ffi.new("struct s(*[2])(struct s)") |
| 2566 |
my_array[0] = lib.add1 |
| 2567 |
my_array[1] = lib.add2 |
| 2568 |
assert my_array[0](s).x == -41 |
| 2569 |
assert my_array[1](s).x == -40 |
| 2570 |
s.x = 84 |
| 2571 |
p = ffi.cast("void *", lib.add1) |
| 2572 |
assert ffi.cast("struct s(*)(struct s)", p)(s).x == 85 |
| 2573 |
q = ffi.cast("intptr_t", lib.add2) |
| 2574 |
assert ffi.cast("struct s(*)(struct s)", q)(s).x == 86 |
| 2575 |
s.x = 300 |
| 2576 |
my_array_2 = ffi.new("void *[]", [lib.add1, lib.add2]) |
| 2577 |
assert ffi.cast("struct s(*)(struct s)", my_array_2[1])(s).x == 302 |
| 2578 |
assert ffi.typeof(lib.add1) == ffi.typeof("struct s(*)(struct s)") |