| 1 |
import os, pytest |
| 2 |
import sys |
| 3 |
|
| 4 |
from rpython.tool.udir import udir |
| 5 |
from pypy.interpreter.gateway import unwrap_spec, interp2app |
| 6 |
from pypy.module._cffi_backend.newtype import _clean_cache |
| 7 |
import pypy.module.cpyext.api |
| 8 |
from sysconfig import get_config_var |
| 9 |
|
| 10 |
|
| 11 |
def get_ext_suffix(): |
| 12 |
|
| 13 |
|
| 14 |
soabi = get_config_var('SOABI') or '' |
| 15 |
ret = soabi + get_config_var('SO') |
| 16 |
|
| 17 |
if ret[0] == '.': |
| 18 |
return ret[1:] |
| 19 |
return ret |
| 20 |
|
| 21 |
|
| 22 |
@unwrap_spec(cdef='text', module_name='text', source='text', packed=int) |
| 23 |
def prepare(space, cdef, module_name, source, w_includes=None, |
| 24 |
w_extra_source=None, w_min_version=None, packed=False, |
| 25 |
w_extra_compile_args=None): |
| 26 |
try: |
| 27 |
import cffi |
| 28 |
from cffi import FFI |
| 29 |
from cffi import recompiler |
| 30 |
from cffi import ffiplatform |
| 31 |
except ImportError: |
| 32 |
pytest.skip("system cffi module not found or older than 1.0.0") |
| 33 |
if w_min_version is None: |
| 34 |
min_version = (1, 4, 0) |
| 35 |
else: |
| 36 |
min_version = tuple(space.unwrap(w_min_version)) |
| 37 |
if cffi.__version_info__ < min_version: |
| 38 |
pytest.skip("system cffi module needs to be at least %s, got %s" % ( |
| 39 |
min_version, cffi.__version_info__)) |
| 40 |
space.appexec([], """(): |
| 41 |
import _cffi_backend # force it to be initialized |
| 42 |
""") |
| 43 |
includes = [] |
| 44 |
if w_includes: |
| 45 |
includes += space.unpackiterable(w_includes) |
| 46 |
assert module_name.startswith('test_') |
| 47 |
module_name = '_CFFI_' + module_name |
| 48 |
rdir = udir.ensure('recompiler', dir=1) |
| 49 |
rdir.join('Python.h').write( |
| 50 |
'#include <stdio.h>\n' |
| 51 |
'#define PYPY_VERSION XX\n' |
| 52 |
'#define PyMODINIT_FUNC /*exported*/ void\n' |
| 53 |
) |
| 54 |
path = module_name.replace('.', os.sep) |
| 55 |
if '.' in module_name: |
| 56 |
subrdir = rdir.join(module_name[:module_name.index('.')]) |
| 57 |
os.mkdir(str(subrdir)) |
| 58 |
else: |
| 59 |
subrdir = rdir |
| 60 |
c_file = str(rdir.join('%s.c' % path)) |
| 61 |
ffi = FFI() |
| 62 |
for include_ffi_object in includes: |
| 63 |
ffi.include(include_ffi_object._test_recompiler_source_ffi) |
| 64 |
ffi.cdef(cdef, packed=packed) |
| 65 |
ffi.set_source(module_name, source) |
| 66 |
ffi.emit_c_code(c_file) |
| 67 |
|
| 68 |
base_module_name = module_name.split('.')[-1] |
| 69 |
sources = [] |
| 70 |
if w_extra_source is not None: |
| 71 |
sources.append(space.text_w(w_extra_source)) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
import sys |
| 77 |
if sys.platform == 'win32': |
| 78 |
extra_compile_args = [] |
| 79 |
else: |
| 80 |
extra_compile_args = [ |
| 81 |
'-Wno-error=incompatible-pointer-types', |
| 82 |
'-Wno-error=int-conversion', |
| 83 |
] |
| 84 |
if w_extra_compile_args is not None: |
| 85 |
extra_compile_args += space.unwrap(w_extra_compile_args) |
| 86 |
kwargs = {'extra_compile_args': extra_compile_args} |
| 87 |
ext = ffiplatform.get_extension(c_file, module_name, |
| 88 |
include_dirs=[str(rdir)], |
| 89 |
export_symbols=['_cffi_pypyinit_' + base_module_name], |
| 90 |
sources=sources, |
| 91 |
**kwargs) |
| 92 |
ffiplatform.compile(str(rdir), ext) |
| 93 |
|
| 94 |
for extension in [get_ext_suffix(), 'so', 'pyd', 'dylib', 'pypy-73.so']: |
| 95 |
so_file = str(rdir.join('%s.%s' % (path, extension))) |
| 96 |
if os.path.exists(so_file): |
| 97 |
break |
| 98 |
else: |
| 99 |
raise Exception("could not find the compiled extension module?") |
| 100 |
|
| 101 |
args_w = [space.wrap(module_name), space.wrap(so_file)] |
| 102 |
w_res = space.appexec(args_w, """(modulename, filename): |
| 103 |
import _imp |
| 104 |
class Spec: pass |
| 105 |
spec = Spec() |
| 106 |
spec.name = modulename |
| 107 |
spec.origin = filename |
| 108 |
mod = _imp.create_dynamic(spec) |
| 109 |
assert mod.__name__ == modulename |
| 110 |
return (mod.ffi, mod.lib) |
| 111 |
""") |
| 112 |
ffiobject = space.getitem(w_res, space.wrap(0)) |
| 113 |
ffiobject._test_recompiler_source_ffi = ffi |
| 114 |
if not hasattr(space, '_cleanup_ffi'): |
| 115 |
space._cleanup_ffi = [] |
| 116 |
space._cleanup_ffi.append(ffiobject) |
| 117 |
return w_res |
| 118 |
|
| 119 |
|
| 120 |
class AppTestRecompiler: |
| 121 |
spaceconfig = dict(usemodules=['_cffi_backend', 'imp', 'cpyext', 'struct']) |
| 122 |
|
| 123 |
def setup_class(cls): |
| 124 |
if cls.runappdirect: |
| 125 |
pytest.skip("not a test for -A") |
| 126 |
elif sys.platform == 'darwin': |
| 127 |
pytest.skip('fails untranslated on darwin') |
| 128 |
cls.w_prepare = cls.space.wrap(interp2app(prepare)) |
| 129 |
cls.w_udir = cls.space.wrap(str(udir)) |
| 130 |
cls.w_os_sep = cls.space.wrap(os.sep) |
| 131 |
|
| 132 |
def setup_method(self, meth): |
| 133 |
self._w_modules = self.space.appexec([], """(): |
| 134 |
import cpyext # ignore stuff there in the leakfinder |
| 135 |
import sys |
| 136 |
return set(sys.modules) |
| 137 |
""") |
| 138 |
|
| 139 |
def teardown_method(self, meth): |
| 140 |
if hasattr(self.space, '_cleanup_ffi'): |
| 141 |
for ffi in self.space._cleanup_ffi: |
| 142 |
del ffi.cached_types |
| 143 |
del self.space._cleanup_ffi |
| 144 |
self.space.appexec([self._w_modules], """(old_modules): |
| 145 |
import sys |
| 146 |
for key in list(sys.modules.keys()): |
| 147 |
if key not in old_modules: |
| 148 |
del sys.modules[key] |
| 149 |
""") |
| 150 |
_clean_cache(self.space) |
| 151 |
|
| 152 |
def test_math_sin(self): |
| 153 |
import math |
| 154 |
ffi, lib = self.prepare( |
| 155 |
"float sin(double); double cos(double);", |
| 156 |
'test_math_sin', |
| 157 |
'#include <math.h>') |
| 158 |
assert lib.cos(1.43) == math.cos(1.43) |
| 159 |
|
| 160 |
def test_repr_lib(self): |
| 161 |
ffi, lib = self.prepare( |
| 162 |
"", |
| 163 |
'test_repr_lib', |
| 164 |
"") |
| 165 |
assert repr(lib) == "<Lib object for '_CFFI_test_repr_lib'>" |
| 166 |
|
| 167 |
def test_funcarg_ptr(self): |
| 168 |
ffi, lib = self.prepare( |
| 169 |
"int foo(int *);", |
| 170 |
'test_funcarg_ptr', |
| 171 |
'int foo(int *p) { return *p; }') |
| 172 |
assert lib.foo([-12345]) == -12345 |
| 173 |
|
| 174 |
def test_funcres_ptr(self): |
| 175 |
ffi, lib = self.prepare( |
| 176 |
"int *foo(void);", |
| 177 |
'test_funcres_ptr', |
| 178 |
'int *foo(void) { static int x=-12345; return &x; }') |
| 179 |
assert lib.foo()[0] == -12345 |
| 180 |
|
| 181 |
def test_global_var_array(self): |
| 182 |
ffi, lib = self.prepare( |
| 183 |
"int a[100];", |
| 184 |
'test_global_var_array', |
| 185 |
'int a[100] = { 9999 };') |
| 186 |
lib.a[42] = 123456 |
| 187 |
assert lib.a[42] == 123456 |
| 188 |
assert lib.a[0] == 9999 |
| 189 |
|
| 190 |
def test_verify_typedef(self): |
| 191 |
ffi, lib = self.prepare( |
| 192 |
"typedef int **foo_t;", |
| 193 |
'test_verify_typedef', |
| 194 |
'typedef int **foo_t;') |
| 195 |
assert ffi.sizeof("foo_t") == ffi.sizeof("void *") |
| 196 |
|
| 197 |
def test_verify_typedef_dotdotdot(self): |
| 198 |
ffi, lib = self.prepare( |
| 199 |
"typedef ... foo_t;", |
| 200 |
'test_verify_typedef_dotdotdot', |
| 201 |
'typedef int **foo_t;') |
| 202 |
|
| 203 |
|
| 204 |
def test_verify_typedef_star_dotdotdot(self): |
| 205 |
ffi, lib = self.prepare( |
| 206 |
"typedef ... *foo_t;", |
| 207 |
'test_verify_typedef_star_dotdotdot', |
| 208 |
'typedef int **foo_t;') |
| 209 |
|
| 210 |
|
| 211 |
def test_global_var_int(self): |
| 212 |
ffi, lib = self.prepare( |
| 213 |
"int a, b, c;", |
| 214 |
'test_global_var_int', |
| 215 |
'int a = 999, b, c;') |
| 216 |
assert lib.a == 999 |
| 217 |
lib.a -= 1001 |
| 218 |
assert lib.a == -2 |
| 219 |
lib.a = -2147483648 |
| 220 |
assert lib.a == -2147483648 |
| 221 |
raises(OverflowError, "lib.a = 2147483648") |
| 222 |
raises(OverflowError, "lib.a = -2147483649") |
| 223 |
lib.b = 525 |
| 224 |
assert lib.b == 525 |
| 225 |
raises(AttributeError, "del lib.a") |
| 226 |
raises(AttributeError, "del lib.c") |
| 227 |
raises(AttributeError, "del lib.foobarbaz") |
| 228 |
|
| 229 |
def test_macro(self): |
| 230 |
ffi, lib = self.prepare( |
| 231 |
"#define FOOBAR ...", |
| 232 |
'test_macro', |
| 233 |
"#define FOOBAR (-6912)") |
| 234 |
assert lib.FOOBAR == -6912 |
| 235 |
raises(AttributeError, "lib.FOOBAR = 2") |
| 236 |
|
| 237 |
def test_macro_check_value(self): |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
import sys |
| 243 |
vals = ['42', '-42', '0x80000000', '-2147483648', |
| 244 |
'0', '9223372036854775809ULL', |
| 245 |
'-9223372036854775807LL'] |
| 246 |
if sys.maxsize <= 2**32: |
| 247 |
vals.remove('-2147483648') |
| 248 |
|
| 249 |
cdef_lines = ['#define FOO_%d_%d %s' % (i, j, vals[i]) |
| 250 |
for i in range(len(vals)) |
| 251 |
for j in range(len(vals))] |
| 252 |
|
| 253 |
verify_lines = ['#define FOO_%d_%d %s' % (i, j, vals[j]) |
| 254 |
for i in range(len(vals)) |
| 255 |
for j in range(len(vals))] |
| 256 |
|
| 257 |
ffi, lib = self.prepare( |
| 258 |
'\n'.join(cdef_lines), |
| 259 |
'test_macro_check_value_ok', |
| 260 |
'\n'.join(verify_lines)) |
| 261 |
|
| 262 |
for j in range(len(vals)): |
| 263 |
c_got = int(vals[j].replace('U', '').replace('L', ''), 0) |
| 264 |
c_compiler_msg = str(c_got) |
| 265 |
if c_got > 0: |
| 266 |
c_compiler_msg += ' (0x%x)' % (c_got,) |
| 267 |
|
| 268 |
for i in range(len(vals)): |
| 269 |
attrname = 'FOO_%d_%d' % (i, j) |
| 270 |
if i == j: |
| 271 |
x = getattr(lib, attrname) |
| 272 |
assert x == c_got |
| 273 |
else: |
| 274 |
e = raises(ffi.error, getattr, lib, attrname) |
| 275 |
assert str(e.value) == ( |
| 276 |
"the C compiler says '%s' is equal to " |
| 277 |
"%s, but the cdef disagrees" % (attrname, c_compiler_msg)) |
| 278 |
|
| 279 |
def test_constant(self): |
| 280 |
ffi, lib = self.prepare( |
| 281 |
"static const int FOOBAR;", |
| 282 |
'test_constant', |
| 283 |
"#define FOOBAR (-6912)") |
| 284 |
assert lib.FOOBAR == -6912 |
| 285 |
raises(AttributeError, "lib.FOOBAR = 2") |
| 286 |
|
| 287 |
def test_check_value_of_static_const(self): |
| 288 |
ffi, lib = self.prepare( |
| 289 |
"static const int FOOBAR = 042;", |
| 290 |
'test_check_value_of_static_const', |
| 291 |
"#define FOOBAR (-6912)") |
| 292 |
e = raises(ffi.error, getattr, lib, 'FOOBAR') |
| 293 |
assert str(e.value) == ( |
| 294 |
"the C compiler says 'FOOBAR' is equal to -6912, but the cdef disagrees") |
| 295 |
|
| 296 |
def test_constant_nonint(self): |
| 297 |
ffi, lib = self.prepare( |
| 298 |
"static const double FOOBAR;", |
| 299 |
'test_constant_nonint', |
| 300 |
"#define FOOBAR (-6912.5)") |
| 301 |
assert lib.FOOBAR == -6912.5 |
| 302 |
raises(AttributeError, "lib.FOOBAR = 2") |
| 303 |
|
| 304 |
def test_constant_ptr(self): |
| 305 |
ffi, lib = self.prepare( |
| 306 |
"static double *const FOOBAR;", |
| 307 |
'test_constant_ptr', |
| 308 |
"#define FOOBAR NULL") |
| 309 |
assert lib.FOOBAR == ffi.NULL |
| 310 |
assert ffi.typeof(lib.FOOBAR) == ffi.typeof("double *") |
| 311 |
|
| 312 |
def test_dir(self): |
| 313 |
ffi, lib = self.prepare( |
| 314 |
"int ff(int); int aa; static const int my_constant;", |
| 315 |
'test_dir', """ |
| 316 |
#define my_constant (-45) |
| 317 |
int aa; |
| 318 |
int ff(int x) { return x+aa; } |
| 319 |
""") |
| 320 |
lib.aa = 5 |
| 321 |
assert dir(lib) == ['aa', 'ff', 'my_constant'] |
| 322 |
|
| 323 |
aaobj = lib.__dict__['aa'] |
| 324 |
assert not isinstance(aaobj, int) |
| 325 |
assert lib.__dict__ == { |
| 326 |
'ff': lib.ff, |
| 327 |
'aa': aaobj, |
| 328 |
'my_constant': -45} |
| 329 |
lib.__dict__['ff'] = "??" |
| 330 |
assert lib.ff(10) == 15 |
| 331 |
|
| 332 |
def test_verify_opaque_struct(self): |
| 333 |
ffi, lib = self.prepare( |
| 334 |
"struct foo_s;", |
| 335 |
'test_verify_opaque_struct', |
| 336 |
"struct foo_s;") |
| 337 |
assert ffi.typeof("struct foo_s").cname == "struct foo_s" |
| 338 |
|
| 339 |
def test_verify_opaque_union(self): |
| 340 |
ffi, lib = self.prepare( |
| 341 |
"union foo_s;", |
| 342 |
'test_verify_opaque_union', |
| 343 |
"union foo_s;") |
| 344 |
assert ffi.typeof("union foo_s").cname == "union foo_s" |
| 345 |
|
| 346 |
def test_verify_struct(self): |
| 347 |
ffi, lib = self.prepare( |
| 348 |
"""struct foo_s { int b; short a; ...; }; |
| 349 |
struct bar_s { struct foo_s *f; };""", |
| 350 |
'test_verify_struct', |
| 351 |
"""struct foo_s { short a; int b; }; |
| 352 |
struct bar_s { struct foo_s *f; };""") |
| 353 |
ffi.typeof("struct bar_s *") |
| 354 |
p = ffi.new("struct foo_s *", {'a': -32768, 'b': -2147483648}) |
| 355 |
assert p.a == -32768 |
| 356 |
assert p.b == -2147483648 |
| 357 |
raises(OverflowError, "p.a -= 1") |
| 358 |
raises(OverflowError, "p.b -= 1") |
| 359 |
q = ffi.new("struct bar_s *", {'f': p}) |
| 360 |
assert q.f == p |
| 361 |
|
| 362 |
assert ffi.offsetof("struct foo_s", "a") == 0 |
| 363 |
assert ffi.offsetof("struct foo_s", "b") == 4 |
| 364 |
assert ffi.offsetof(u"struct foo_s", u"b") == 4 |
| 365 |
|
| 366 |
raises(TypeError, ffi.addressof, p) |
| 367 |
assert ffi.addressof(p[0]) == p |
| 368 |
assert ffi.typeof(ffi.addressof(p[0])) is ffi.typeof("struct foo_s *") |
| 369 |
assert ffi.typeof(ffi.addressof(p, "b")) is ffi.typeof("int *") |
| 370 |
assert ffi.addressof(p, "b")[0] == p.b |
| 371 |
|
| 372 |
def test_verify_exact_field_offset(self): |
| 373 |
ffi, lib = self.prepare( |
| 374 |
"""struct foo_s { int b; short a; };""", |
| 375 |
'test_verify_exact_field_offset', |
| 376 |
"""struct foo_s { short a; int b; };""") |
| 377 |
e = raises(ffi.error, ffi.new, "struct foo_s *", []) |
| 378 |
assert str(e.value).startswith( |
| 379 |
"struct foo_s: wrong offset for field 'b' (cdef " |
| 380 |
'says 0, but C compiler says 4). fix it or use "...;" ') |
| 381 |
|
| 382 |
def test_type_caching(self): |
| 383 |
ffi1, lib1 = self.prepare( |
| 384 |
"struct foo_s;", |
| 385 |
'test_type_caching_1', |
| 386 |
'struct foo_s;') |
| 387 |
ffi2, lib2 = self.prepare( |
| 388 |
"struct foo_s;", |
| 389 |
'test_type_caching_2', |
| 390 |
'struct foo_s;') |
| 391 |
|
| 392 |
assert ffi1.typeof("long") is ffi2.typeof("long") |
| 393 |
assert ffi1.typeof("long**") is ffi2.typeof("long * *") |
| 394 |
assert ffi1.typeof("long(*)(int, ...)") is ffi2.typeof("long(*)(int, ...)") |
| 395 |
|
| 396 |
assert ffi1.typeof("struct foo_s") is not ffi2.typeof("struct foo_s") |
| 397 |
assert ffi1.typeof("struct foo_s *") is not ffi2.typeof("struct foo_s *") |
| 398 |
assert ffi1.typeof("struct foo_s*(*)()") is not ( |
| 399 |
ffi2.typeof("struct foo_s*(*)()")) |
| 400 |
assert ffi1.typeof("void(*)(struct foo_s*)") is not ( |
| 401 |
ffi2.typeof("void(*)(struct foo_s*)")) |
| 402 |
|
| 403 |
def test_verify_enum(self): |
| 404 |
import sys |
| 405 |
ffi, lib = self.prepare( |
| 406 |
"""enum e1 { B1, A1, ... }; enum e2 { B2, A2, ... };""", |
| 407 |
'test_verify_enum', |
| 408 |
"enum e1 { A1, B1, C1=%d };" % sys.maxsize + |
| 409 |
"enum e2 { A2, B2, C2 };") |
| 410 |
ffi.typeof("enum e1") |
| 411 |
ffi.typeof("enum e2") |
| 412 |
assert lib.A1 == 0 |
| 413 |
assert lib.B1 == 1 |
| 414 |
assert lib.A2 == 0 |
| 415 |
assert lib.B2 == 1 |
| 416 |
assert ffi.sizeof("enum e1") == ffi.sizeof("long") |
| 417 |
assert ffi.sizeof("enum e2") == ffi.sizeof("int") |
| 418 |
assert repr(ffi.cast("enum e1", 0)) == "<cdata 'enum e1' 0: A1>" |
| 419 |
|
| 420 |
def test_dotdotdot_length_of_array_field(self): |
| 421 |
ffi, lib = self.prepare( |
| 422 |
"struct foo_s { int a[...]; int b[...]; };", |
| 423 |
'test_dotdotdot_length_of_array_field', |
| 424 |
"struct foo_s { int a[42]; int b[11]; };") |
| 425 |
assert ffi.sizeof("struct foo_s") == (42 + 11) * 4 |
| 426 |
p = ffi.new("struct foo_s *") |
| 427 |
assert p.a[41] == p.b[10] == 0 |
| 428 |
raises(IndexError, "p.a[42]") |
| 429 |
raises(IndexError, "p.b[11]") |
| 430 |
|
| 431 |
def test_dotdotdot_global_array(self): |
| 432 |
ffi, lib = self.prepare( |
| 433 |
"int aa[...]; int bb[...];", |
| 434 |
'test_dotdotdot_global_array', |
| 435 |
"int aa[41]; int bb[12];") |
| 436 |
assert ffi.sizeof(lib.aa) == 41 * 4 |
| 437 |
assert ffi.sizeof(lib.bb) == 12 * 4 |
| 438 |
assert lib.aa[40] == lib.bb[11] == 0 |
| 439 |
raises(IndexError, "lib.aa[41]") |
| 440 |
raises(IndexError, "lib.bb[12]") |
| 441 |
|
| 442 |
def test_misdeclared_field_1(self): |
| 443 |
ffi, lib = self.prepare( |
| 444 |
"struct foo_s { int a[5]; };", |
| 445 |
'test_misdeclared_field_1', |
| 446 |
"struct foo_s { int a[6]; };") |
| 447 |
assert ffi.sizeof("struct foo_s") == 24 |
| 448 |
try: |
| 449 |
|
| 450 |
p = ffi.new("struct foo_s *") |
| 451 |
p.a |
| 452 |
assert False, "should have raised" |
| 453 |
except ffi.error as e: |
| 454 |
assert str(e).startswith("struct foo_s: wrong size for field 'a' " |
| 455 |
"(cdef says 20, but C compiler says 24)") |
| 456 |
|
| 457 |
def test_open_array_in_struct(self): |
| 458 |
ffi, lib = self.prepare( |
| 459 |
"struct foo_s { int b; int a[]; };", |
| 460 |
'test_open_array_in_struct', |
| 461 |
"struct foo_s { int b; int a[]; };") |
| 462 |
assert ffi.sizeof("struct foo_s") == 4 |
| 463 |
p = ffi.new("struct foo_s *", [5, [10, 20, 30, 40]]) |
| 464 |
assert p.a[2] == 30 |
| 465 |
assert ffi.sizeof(p) == ffi.sizeof("void *") |
| 466 |
assert ffi.sizeof(p[0]) == 5 * ffi.sizeof("int") |
| 467 |
|
| 468 |
def test_math_sin_type(self): |
| 469 |
ffi, lib = self.prepare( |
| 470 |
"double sin(double); void *xxtestfunc();", |
| 471 |
'test_math_sin_type', |
| 472 |
"""#include <math.h> |
| 473 |
void *xxtestfunc(void) { return 0; } |
| 474 |
""") |
| 475 |
|
| 476 |
assert ffi.typeof(lib.sin).cname == "double(*)(double)" |
| 477 |
|
| 478 |
x = type(lib).__dir__.__get__(lib) |
| 479 |
raises(TypeError, ffi.typeof, x) |
| 480 |
|
| 481 |
|
| 482 |
assert lib.sin.__name__ == 'sin' |
| 483 |
assert lib.sin.__module__ == '_CFFI_test_math_sin_type' |
| 484 |
assert lib.sin.__doc__ == ( |
| 485 |
"double sin(double);\n" |
| 486 |
"\n" |
| 487 |
"CFFI C function from _CFFI_test_math_sin_type.lib") |
| 488 |
|
| 489 |
assert ffi.typeof(lib.xxtestfunc).cname == "void *(*)()" |
| 490 |
assert lib.xxtestfunc.__doc__ == ( |
| 491 |
"void *xxtestfunc();\n" |
| 492 |
"\n" |
| 493 |
"CFFI C function from _CFFI_test_math_sin_type.lib") |
| 494 |
|
| 495 |
def test_verify_anonymous_struct_with_typedef(self): |
| 496 |
ffi, lib = self.prepare( |
| 497 |
"typedef struct { int a; long b; ...; } foo_t;", |
| 498 |
'test_verify_anonymous_struct_with_typedef', |
| 499 |
"typedef struct { long b; int hidden, a; } foo_t;") |
| 500 |
p = ffi.new("foo_t *", {'b': 42}) |
| 501 |
assert p.b == 42 |
| 502 |
assert repr(p).startswith("<cdata 'foo_t *' ") |
| 503 |
|
| 504 |
def test_verify_anonymous_struct_with_star_typedef(self): |
| 505 |
ffi, lib = self.prepare( |
| 506 |
"typedef struct { int a; long b; } *foo_t;", |
| 507 |
'test_verify_anonymous_struct_with_star_typedef', |
| 508 |
"typedef struct { int a; long b; } *foo_t;") |
| 509 |
p = ffi.new("foo_t", {'b': 42}) |
| 510 |
assert p.b == 42 |
| 511 |
|
| 512 |
def test_verify_anonymous_enum_with_typedef(self): |
| 513 |
ffi, lib = self.prepare( |
| 514 |
"typedef enum { AA, ... } e1;", |
| 515 |
'test_verify_anonymous_enum_with_typedef1', |
| 516 |
"typedef enum { BB, CC, AA } e1;") |
| 517 |
assert lib.AA == 2 |
| 518 |
assert ffi.sizeof("e1") == ffi.sizeof("int") |
| 519 |
assert repr(ffi.cast("e1", 2)) == "<cdata 'e1' 2: AA>" |
| 520 |
|
| 521 |
import sys |
| 522 |
ffi, lib = self.prepare( |
| 523 |
"typedef enum { AA=%d } e1;" % sys.maxsize, |
| 524 |
'test_verify_anonymous_enum_with_typedef2', |
| 525 |
"typedef enum { AA=%d } e1;" % sys.maxsize) |
| 526 |
assert lib.AA == sys.maxsize |
| 527 |
assert ffi.sizeof("e1") == ffi.sizeof("long") |
| 528 |
|
| 529 |
def test_unique_types(self): |
| 530 |
CDEF = "struct foo_s; union foo_u; enum foo_e { AA };" |
| 531 |
ffi1, lib1 = self.prepare(CDEF, "test_unique_types_1", CDEF) |
| 532 |
ffi2, lib2 = self.prepare(CDEF, "test_unique_types_2", CDEF) |
| 533 |
|
| 534 |
assert ffi1.typeof("char") is ffi2.typeof("char ") |
| 535 |
assert ffi1.typeof("long") is ffi2.typeof("signed long int") |
| 536 |
assert ffi1.typeof("double *") is ffi2.typeof("double*") |
| 537 |
assert ffi1.typeof("int ***") is ffi2.typeof(" int * * *") |
| 538 |
assert ffi1.typeof("int[]") is ffi2.typeof("signed int[]") |
| 539 |
assert ffi1.typeof("signed int*[17]") is ffi2.typeof("int *[17]") |
| 540 |
assert ffi1.typeof("void") is ffi2.typeof("void") |
| 541 |
assert ffi1.typeof("int(*)(int,int)") is ffi2.typeof("int(*)(int,int)") |
| 542 |
|
| 543 |
|
| 544 |
for name in ["struct foo_s", |
| 545 |
"union foo_u *", |
| 546 |
"enum foo_e", |
| 547 |
"struct foo_s *(*)()", |
| 548 |
"void(*)(struct foo_s *)", |
| 549 |
"struct foo_s *(*[5])[8]", |
| 550 |
]: |
| 551 |
assert ffi1.typeof(name) is not ffi2.typeof(name) |
| 552 |
|
| 553 |
assert ffi1.typeof("struct foo_s*") is ffi1.typeof("struct foo_s *") |
| 554 |
|
| 555 |
def test_module_name_in_package(self): |
| 556 |
ffi, lib = self.prepare( |
| 557 |
"int foo(int);", |
| 558 |
'test_module_name_in_package.mymod', |
| 559 |
"int foo(int x) { return x + 32; }") |
| 560 |
assert lib.foo(10) == 42 |
| 561 |
|
| 562 |
def test_unspecified_size_of_global_1(self): |
| 563 |
ffi, lib = self.prepare( |
| 564 |
"int glob[];", |
| 565 |
"test_unspecified_size_of_global_1", |
| 566 |
"int glob[10];") |
| 567 |
assert ffi.typeof(lib.glob) == ffi.typeof("int *") |
| 568 |
|
| 569 |
def test_unspecified_size_of_global_2(self): |
| 570 |
ffi, lib = self.prepare( |
| 571 |
"int glob[][5];", |
| 572 |
"test_unspecified_size_of_global_2", |
| 573 |
"int glob[10][5];") |
| 574 |
assert ffi.typeof(lib.glob) == ffi.typeof("int(*)[5]") |
| 575 |
|
| 576 |
def test_unspecified_size_of_global_3(self): |
| 577 |
ffi, lib = self.prepare( |
| 578 |
"int glob[][...];", |
| 579 |
"test_unspecified_size_of_global_3", |
| 580 |
"int glob[10][5];") |
| 581 |
assert ffi.typeof(lib.glob) == ffi.typeof("int(*)[5]") |
| 582 |
|
| 583 |
def test_unspecified_size_of_global_4(self): |
| 584 |
ffi, lib = self.prepare( |
| 585 |
"int glob[...][...];", |
| 586 |
"test_unspecified_size_of_global_4", |
| 587 |
"int glob[10][5];") |
| 588 |
assert ffi.typeof(lib.glob) == ffi.typeof("int[10][5]") |
| 589 |
|
| 590 |
def test_include_1(self): |
| 591 |
ffi1, lib1 = self.prepare( |
| 592 |
"typedef double foo_t;", |
| 593 |
"test_include_1_parent", |
| 594 |
"typedef double foo_t;") |
| 595 |
ffi, lib = self.prepare( |
| 596 |
"foo_t ff1(foo_t);", |
| 597 |
"test_include_1", |
| 598 |
"double ff1(double x) { return 42.5; }", |
| 599 |
includes=[ffi1]) |
| 600 |
assert lib.ff1(0) == 42.5 |
| 601 |
assert ffi1.typeof("foo_t") is ffi.typeof("foo_t") \ |
| 602 |
is ffi.typeof("double") |
| 603 |
|
| 604 |
def test_include_1b(self): |
| 605 |
ffi1, lib1 = self.prepare( |
| 606 |
"int foo1(int);", |
| 607 |
"test_include_1b_parent", |
| 608 |
"int foo1(int x) { return x + 10; }") |
| 609 |
ffi, lib = self.prepare( |
| 610 |
"int foo2(int);", |
| 611 |
"test_include_1b", |
| 612 |
"int foo2(int x) { return x - 5; }", |
| 613 |
includes=[ffi1]) |
| 614 |
assert lib.foo2(42) == 37 |
| 615 |
assert lib.foo1(42) == 52 |
| 616 |
assert lib.foo1 is lib1.foo1 |
| 617 |
|
| 618 |
def test_include_2(self): |
| 619 |
ffi1, lib1 = self.prepare( |
| 620 |
"struct foo_s { int x, y; };", |
| 621 |
"test_include_2_parent", |
| 622 |
"struct foo_s { int x, y; };") |
| 623 |
ffi, lib = self.prepare( |
| 624 |
"struct foo_s *ff2(struct foo_s *);", |
| 625 |
"test_include_2", |
| 626 |
"struct foo_s { int x, y; }; //usually from a #include\n" |
| 627 |
"struct foo_s *ff2(struct foo_s *p) { p->y++; return p; }", |
| 628 |
includes=[ffi1]) |
| 629 |
p = ffi.new("struct foo_s *") |
| 630 |
p.y = 41 |
| 631 |
q = lib.ff2(p) |
| 632 |
assert q == p |
| 633 |
assert p.y == 42 |
| 634 |
assert ffi1.typeof("struct foo_s") is ffi.typeof("struct foo_s") |
| 635 |
|
| 636 |
def test_include_3(self): |
| 637 |
ffi1, lib1 = self.prepare( |
| 638 |
"typedef short sshort_t;", |
| 639 |
"test_include_3_parent", |
| 640 |
"typedef short sshort_t;") |
| 641 |
ffi, lib = self.prepare( |
| 642 |
"sshort_t ff3(sshort_t);", |
| 643 |
"test_include_3", |
| 644 |
"typedef short sshort_t; //usually from a #include\n" |
| 645 |
"sshort_t ff3(sshort_t x) { return x + 42; }", |
| 646 |
includes=[ffi1]) |
| 647 |
assert lib.ff3(10) == 52 |
| 648 |
assert ffi.typeof(ffi.cast("sshort_t", 42)) is ffi.typeof("short") |
| 649 |
assert ffi1.typeof("sshort_t") is ffi.typeof("sshort_t") |
| 650 |
|
| 651 |
def test_include_4(self): |
| 652 |
ffi1, lib1 = self.prepare( |
| 653 |
"typedef struct { int x; } mystruct_t;", |
| 654 |
"test_include_4_parent", |
| 655 |
"typedef struct { int x; } mystruct_t;") |
| 656 |
ffi, lib = self.prepare( |
| 657 |
"mystruct_t *ff4(mystruct_t *);", |
| 658 |
"test_include_4", |
| 659 |
"typedef struct {int x; } mystruct_t; //usually from a #include\n" |
| 660 |
"mystruct_t *ff4(mystruct_t *p) { p->x += 42; return p; }", |
| 661 |
includes=[ffi1]) |
| 662 |
p = ffi.new("mystruct_t *", [10]) |
| 663 |
q = lib.ff4(p) |
| 664 |
assert q == p |
| 665 |
assert p.x == 52 |
| 666 |
assert ffi1.typeof("mystruct_t") is ffi.typeof("mystruct_t") |
| 667 |
|
| 668 |
def test_include_5(self): |
| 669 |
ffi1, lib1 = self.prepare( |
| 670 |
"typedef struct { int x[2]; int y; } *mystruct_p;", |
| 671 |
"test_include_5_parent", |
| 672 |
"typedef struct { int x[2]; int y; } *mystruct_p;") |
| 673 |
ffi, lib = self.prepare( |
| 674 |
"mystruct_p ff5(mystruct_p);", |
| 675 |
"test_include_5", |
| 676 |
"typedef struct {int x[2]; int y; } *mystruct_p; //#include\n" |
| 677 |
"mystruct_p ff5(mystruct_p p) { p->x[1] += 42; return p; }", |
| 678 |
includes=[ffi1]) |
| 679 |
assert ffi.alignof(ffi.typeof("mystruct_p").item) == 4 |
| 680 |
assert ffi1.typeof("mystruct_p") is ffi.typeof("mystruct_p") |
| 681 |
p = ffi.new("mystruct_p", [[5, 10], -17]) |
| 682 |
q = lib.ff5(p) |
| 683 |
assert q == p |
| 684 |
assert p.x[0] == 5 |
| 685 |
assert p.x[1] == 52 |
| 686 |
assert p.y == -17 |
| 687 |
assert ffi.alignof(ffi.typeof(p[0])) == 4 |
| 688 |
|
| 689 |
def test_include_6(self): |
| 690 |
ffi1, lib1 = self.prepare( |
| 691 |
"typedef ... mystruct_t;", |
| 692 |
"test_include_6_parent", |
| 693 |
"typedef struct _mystruct_s mystruct_t;") |
| 694 |
ffi, lib = self.prepare( |
| 695 |
"mystruct_t *ff6(void); int ff6b(mystruct_t *);", |
| 696 |
"test_include_6", |
| 697 |
"typedef struct _mystruct_s mystruct_t; //usually from a #include\n" |
| 698 |
"struct _mystruct_s { int x; };\n" |
| 699 |
"static mystruct_t result_struct = { 42 };\n" |
| 700 |
"mystruct_t *ff6(void) { return &result_struct; }\n" |
| 701 |
"int ff6b(mystruct_t *p) { return p->x; }", |
| 702 |
includes=[ffi1]) |
| 703 |
p = lib.ff6() |
| 704 |
assert ffi.cast("int *", p)[0] == 42 |
| 705 |
assert lib.ff6b(p) == 42 |
| 706 |
|
| 707 |
def test_include_7(self): |
| 708 |
ffi1, lib1 = self.prepare( |
| 709 |
"typedef ... mystruct_t; int ff7b(mystruct_t *);", |
| 710 |
"test_include_7_parent", |
| 711 |
"typedef struct { int x; } mystruct_t;\n" |
| 712 |
"int ff7b(mystruct_t *p) { return p->x; }") |
| 713 |
ffi, lib = self.prepare( |
| 714 |
"mystruct_t *ff7(void);", |
| 715 |
"test_include_7", |
| 716 |
"typedef struct { int x; } mystruct_t; //usually from a #include\n" |
| 717 |
"static mystruct_t result_struct = { 42 };" |
| 718 |
"mystruct_t *ff7(void) { return &result_struct; }", |
| 719 |
includes=[ffi1]) |
| 720 |
p = lib.ff7() |
| 721 |
assert ffi.cast("int *", p)[0] == 42 |
| 722 |
assert lib.ff7b(p) == 42 |
| 723 |
|
| 724 |
def test_include_8(self): |
| 725 |
ffi1, lib1 = self.prepare( |
| 726 |
"struct foo_s;", |
| 727 |
"test_include_8_parent", |
| 728 |
"struct foo_s;") |
| 729 |
ffi, lib = self.prepare( |
| 730 |
"struct foo_s { int x, y; };", |
| 731 |
"test_include_8", |
| 732 |
"struct foo_s { int x, y; };", |
| 733 |
includes=[ffi1]) |
| 734 |
e = raises(NotImplementedError, ffi.new, "struct foo_s *") |
| 735 |
assert str(e.value) == ( |
| 736 |
"'struct foo_s' is opaque in the ffi.include(), but no longer in " |
| 737 |
"the ffi doing the include (workaround: don't use ffi.include() but" |
| 738 |
" duplicate the declarations of everything using struct foo_s)") |
| 739 |
|
| 740 |
def test_bitfield_basic(self): |
| 741 |
ffi, lib = self.prepare( |
| 742 |
"struct bitfield { int a:10, b:25; };", |
| 743 |
"test_bitfield_basic", |
| 744 |
"struct bitfield { int a:10, b:25; };") |
| 745 |
assert ffi.sizeof("struct bitfield") == 8 |
| 746 |
s = ffi.new("struct bitfield *") |
| 747 |
s.a = -512 |
| 748 |
raises(OverflowError, "s.a = -513") |
| 749 |
assert s.a == -512 |
| 750 |
|
| 751 |
def test_incomplete_struct_as_arg(self): |
| 752 |
ffi, lib = self.prepare( |
| 753 |
"struct foo_s { int x; ...; }; int f(int, struct foo_s);", |
| 754 |
"test_incomplete_struct_as_arg", |
| 755 |
"struct foo_s { int a, x, z; };\n" |
| 756 |
"int f(int b, struct foo_s s) { return s.x * b; }") |
| 757 |
s = ffi.new("struct foo_s *", [21]) |
| 758 |
assert s.x == 21 |
| 759 |
assert ffi.sizeof(s[0]) == 12 |
| 760 |
assert ffi.offsetof(ffi.typeof(s), 'x') == 4 |
| 761 |
assert lib.f(2, s[0]) == 42 |
| 762 |
assert ffi.typeof(lib.f) == ffi.typeof("int(*)(int, struct foo_s)") |
| 763 |
|
| 764 |
def test_incomplete_struct_as_result(self): |
| 765 |
ffi, lib = self.prepare( |
| 766 |
"struct foo_s { int x; ...; }; struct foo_s f(int);", |
| 767 |
"test_incomplete_struct_as_result", |
| 768 |
"struct foo_s { int a, x, z; };\n" |
| 769 |
"struct foo_s f(int x) { struct foo_s r; r.x = x * 2; return r; }") |
| 770 |
s = lib.f(21) |
| 771 |
assert s.x == 42 |
| 772 |
assert ffi.typeof(lib.f) == ffi.typeof("struct foo_s(*)(int)") |
| 773 |
|
| 774 |
def test_incomplete_struct_as_both(self): |
| 775 |
ffi, lib = self.prepare( |
| 776 |
"struct foo_s { int x; ...; }; struct bar_s { int y; ...; };\n" |
| 777 |
"struct foo_s f(int, struct bar_s);", |
| 778 |
"test_incomplete_struct_as_both", |
| 779 |
"struct foo_s { int a, x, z; };\n" |
| 780 |
"struct bar_s { int b, c, y, d; };\n" |
| 781 |
"struct foo_s f(int x, struct bar_s b) {\n" |
| 782 |
" struct foo_s r; r.x = x * b.y; return r;\n" |
| 783 |
"}") |
| 784 |
b = ffi.new("struct bar_s *", [7]) |
| 785 |
s = lib.f(6, b[0]) |
| 786 |
assert s.x == 42 |
| 787 |
assert ffi.typeof(lib.f) == ffi.typeof( |
| 788 |
"struct foo_s(*)(int, struct bar_s)") |
| 789 |
s = lib.f(14, {'y': -3}) |
| 790 |
assert s.x == -42 |
| 791 |
|
| 792 |
def test_name_of_unnamed_struct(self): |
| 793 |
ffi, lib = self.prepare( |
| 794 |
"typedef struct { int x; } foo_t;\n" |
| 795 |
"typedef struct { int y; } *bar_p;\n" |
| 796 |
"typedef struct { int y; } **baz_pp;\n", |
| 797 |
"test_name_of_unnamed_struct", |
| 798 |
"typedef struct { int x; } foo_t;\n" |
| 799 |
"typedef struct { int y; } *bar_p;\n" |
| 800 |
"typedef struct { int y; } **baz_pp;\n") |
| 801 |
assert repr(ffi.typeof("foo_t")) == "<ctype 'foo_t'>" |
| 802 |
assert repr(ffi.typeof("bar_p")) == "<ctype 'struct $1 *'>" |
| 803 |
assert repr(ffi.typeof("baz_pp")) == "<ctype 'struct $2 * *'>" |
| 804 |
|
| 805 |
def test_address_of_global_var(self): |
| 806 |
ffi, lib = self.prepare(""" |
| 807 |
long bottom, bottoms[2]; |
| 808 |
long FetchRectBottom(void); |
| 809 |
long FetchRectBottoms1(void); |
| 810 |
#define FOOBAR 42 |
| 811 |
""", "test_address_of_global_var", """ |
| 812 |
long bottom, bottoms[2]; |
| 813 |
long FetchRectBottom(void) { return bottom; } |
| 814 |
long FetchRectBottoms1(void) { return bottoms[1]; } |
| 815 |
#define FOOBAR 42 |
| 816 |
""") |
| 817 |
lib.bottom = 300 |
| 818 |
assert lib.FetchRectBottom() == 300 |
| 819 |
lib.bottom += 1 |
| 820 |
assert lib.FetchRectBottom() == 301 |
| 821 |
lib.bottoms[1] = 500 |
| 822 |
assert lib.FetchRectBottoms1() == 500 |
| 823 |
lib.bottoms[1] += 2 |
| 824 |
assert lib.FetchRectBottoms1() == 502 |
| 825 |
|
| 826 |
p = ffi.addressof(lib, 'bottom') |
| 827 |
assert ffi.typeof(p) == ffi.typeof("long *") |
| 828 |
assert p[0] == 301 |
| 829 |
p[0] += 1 |
| 830 |
assert lib.FetchRectBottom() == 302 |
| 831 |
p = ffi.addressof(lib, 'bottoms') |
| 832 |
assert ffi.typeof(p) == ffi.typeof("long(*)[2]") |
| 833 |
assert p[0] == lib.bottoms |
| 834 |
|
| 835 |
raises(AttributeError, ffi.addressof, lib, 'unknown_var') |
| 836 |
raises(AttributeError, ffi.addressof, lib, "FOOBAR") |
| 837 |
|
| 838 |
def test_defines__CFFI_(self): |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
ffi, lib = self.prepare(""" |
| 843 |
#define CORRECT 1 |
| 844 |
""", "test_defines__CFFI_", """ |
| 845 |
#ifdef _CFFI_ |
| 846 |
# define CORRECT 1 |
| 847 |
#endif |
| 848 |
""") |
| 849 |
assert lib.CORRECT == 1 |
| 850 |
|
| 851 |
def test_unpack_args(self): |
| 852 |
ffi, lib = self.prepare( |
| 853 |
"void foo0(void); void foo1(int); void foo2(int, int);", |
| 854 |
"test_unpack_args", """ |
| 855 |
void foo0(void) { } |
| 856 |
void foo1(int x) { } |
| 857 |
void foo2(int x, int y) { } |
| 858 |
""") |
| 859 |
assert 'foo0' in repr(lib.foo0) |
| 860 |
assert 'foo1' in repr(lib.foo1) |
| 861 |
assert 'foo2' in repr(lib.foo2) |
| 862 |
lib.foo0() |
| 863 |
lib.foo1(42) |
| 864 |
lib.foo2(43, 44) |
| 865 |
e1 = raises(TypeError, lib.foo0, 42) |
| 866 |
e2 = raises(TypeError, lib.foo0, 43, 44) |
| 867 |
e3 = raises(TypeError, lib.foo1) |
| 868 |
e4 = raises(TypeError, lib.foo1, 43, 44) |
| 869 |
e5 = raises(TypeError, lib.foo2) |
| 870 |
e6 = raises(TypeError, lib.foo2, 42) |
| 871 |
e7 = raises(TypeError, lib.foo2, 45, 46, 47) |
| 872 |
assert str(e1.value) == "foo0() takes no arguments (1 given)" |
| 873 |
assert str(e2.value) == "foo0() takes no arguments (2 given)" |
| 874 |
assert str(e3.value) == "foo1() takes exactly one argument (0 given)" |
| 875 |
assert str(e4.value) == "foo1() takes exactly one argument (2 given)" |
| 876 |
assert str(e5.value) == "foo2() takes exactly 2 arguments (0 given)" |
| 877 |
assert str(e6.value) == "foo2() takes exactly 2 arguments (1 given)" |
| 878 |
assert str(e7.value) == "foo2() takes exactly 2 arguments (3 given)" |
| 879 |
|
| 880 |
def test_address_of_function(self): |
| 881 |
ffi, lib = self.prepare( |
| 882 |
"long myfunc(long x);", |
| 883 |
"test_addressof_function", |
| 884 |
"char myfunc(char x) { return (char)(x + 42); }") |
| 885 |
assert lib.myfunc(5) == 47 |
| 886 |
assert lib.myfunc(0xABC05) == 47 |
| 887 |
assert not isinstance(lib.myfunc, ffi.CData) |
| 888 |
assert ffi.typeof(lib.myfunc) == ffi.typeof("long(*)(long)") |
| 889 |
addr = ffi.addressof(lib, 'myfunc') |
| 890 |
assert addr(5) == 47 |
| 891 |
assert addr(0xABC05) == 47 |
| 892 |
assert isinstance(addr, ffi.CData) |
| 893 |
assert ffi.typeof(addr) == ffi.typeof("long(*)(long)") |
| 894 |
|
| 895 |
def test_address_of_function_with_struct(self): |
| 896 |
ffi, lib = self.prepare( |
| 897 |
"struct foo_s { int x; }; long myfunc(struct foo_s);", |
| 898 |
"test_addressof_function_with_struct", """ |
| 899 |
struct foo_s { int x; }; |
| 900 |
char myfunc(struct foo_s input) { return (char)(input.x + 42); } |
| 901 |
""") |
| 902 |
s = ffi.new("struct foo_s *", [5])[0] |
| 903 |
assert lib.myfunc(s) == 47 |
| 904 |
assert not isinstance(lib.myfunc, ffi.CData) |
| 905 |
assert ffi.typeof(lib.myfunc) == ffi.typeof("long(*)(struct foo_s)") |
| 906 |
addr = ffi.addressof(lib, 'myfunc') |
| 907 |
assert addr(s) == 47 |
| 908 |
assert isinstance(addr, ffi.CData) |
| 909 |
assert ffi.typeof(addr) == ffi.typeof("long(*)(struct foo_s)") |
| 910 |
|
| 911 |
def test_issue198(self): |
| 912 |
ffi, lib = self.prepare(""" |
| 913 |
typedef struct{...;} opaque_t; |
| 914 |
const opaque_t CONSTANT; |
| 915 |
int toint(opaque_t); |
| 916 |
""", 'test_issue198', """ |
| 917 |
typedef int opaque_t; |
| 918 |
#define CONSTANT ((opaque_t)42) |
| 919 |
static int toint(opaque_t o) { return o; } |
| 920 |
""") |
| 921 |
def random_stuff(): |
| 922 |
pass |
| 923 |
assert lib.toint(lib.CONSTANT) == 42 |
| 924 |
random_stuff() |
| 925 |
assert lib.toint(lib.CONSTANT) == 42 |
| 926 |
|
| 927 |
def test_constant_is_not_a_compiler_constant(self): |
| 928 |
ffi, lib = self.prepare( |
| 929 |
"static const float almost_forty_two;", |
| 930 |
'test_constant_is_not_a_compiler_constant', """ |
| 931 |
static float f(void) { return 42.25; } |
| 932 |
#define almost_forty_two (f()) |
| 933 |
""") |
| 934 |
assert lib.almost_forty_two == 42.25 |
| 935 |
|
| 936 |
def test_constant_of_unknown_size(self): |
| 937 |
ffi, lib = self.prepare( |
| 938 |
"typedef ... opaque_t;" |
| 939 |
"const opaque_t CONSTANT;", |
| 940 |
'test_constant_of_unknown_size', |
| 941 |
"typedef int opaque_t;" |
| 942 |
"const int CONSTANT = 42;") |
| 943 |
e = raises(ffi.error, getattr, lib, 'CONSTANT') |
| 944 |
assert str(e.value) == ("constant 'CONSTANT' is of " |
| 945 |
"type 'opaque_t', whose size is not known") |
| 946 |
|
| 947 |
def test_variable_of_unknown_size(self): |
| 948 |
ffi, lib = self.prepare(""" |
| 949 |
typedef ... opaque_t; |
| 950 |
opaque_t globvar; |
| 951 |
""", 'test_variable_of_unknown_size', """ |
| 952 |
typedef char opaque_t[6]; |
| 953 |
opaque_t globvar = "hello"; |
| 954 |
""") |
| 955 |
|
| 956 |
e = raises(TypeError, getattr, lib, 'globvar') |
| 957 |
assert str(e.value) == "'opaque_t' is opaque or not completed yet" |
| 958 |
e = raises(TypeError, setattr, lib, 'globvar', []) |
| 959 |
assert str(e.value) == "'opaque_t' is opaque or not completed yet" |
| 960 |
|
| 961 |
p = ffi.addressof(lib, 'globvar') |
| 962 |
assert ffi.typeof(p) == ffi.typeof('opaque_t *') |
| 963 |
assert ffi.string(ffi.cast("char *", p), 8) == b"hello" |
| 964 |
|
| 965 |
def test_constant_of_value_unknown_to_the_compiler(self): |
| 966 |
extra_c_source = self.udir + self.os_sep + ( |
| 967 |
'extra_test_constant_of_value_unknown_to_the_compiler.c') |
| 968 |
with open(extra_c_source, 'wb') as f: |
| 969 |
f.write(b'const int external_foo = 42;\n') |
| 970 |
ffi, lib = self.prepare( |
| 971 |
"const int external_foo;", |
| 972 |
'test_constant_of_value_unknown_to_the_compiler', |
| 973 |
"extern const int external_foo;", |
| 974 |
extra_source=extra_c_source) |
| 975 |
assert lib.external_foo == 42 |
| 976 |
|
| 977 |
def test_call_with_incomplete_structs(self): |
| 978 |
ffi, lib = self.prepare( |
| 979 |
"typedef struct {...;} foo_t; " |
| 980 |
"foo_t myglob; " |
| 981 |
"foo_t increment(foo_t s); " |
| 982 |
"double getx(foo_t s);", |
| 983 |
'test_call_with_incomplete_structs', """ |
| 984 |
typedef double foo_t; |
| 985 |
double myglob = 42.5; |
| 986 |
double getx(double x) { return x; } |
| 987 |
double increment(double x) { return x + 1; } |
| 988 |
""") |
| 989 |
assert lib.getx(lib.myglob) == 42.5 |
| 990 |
assert lib.getx(lib.increment(lib.myglob)) == 43.5 |
| 991 |
|
| 992 |
def test_struct_array_guess_length_2(self): |
| 993 |
ffi, lib = self.prepare( |
| 994 |
"struct foo_s { int a[...][...]; };", |
| 995 |
'test_struct_array_guess_length_2', |
| 996 |
"struct foo_s { int x; int a[5][8]; int y; };") |
| 997 |
assert ffi.sizeof('struct foo_s') == 42 * ffi.sizeof('int') |
| 998 |
s = ffi.new("struct foo_s *") |
| 999 |
assert ffi.typeof(s.a) == ffi.typeof("int[5][8]") |
| 1000 |
assert ffi.sizeof(s.a) == 40 * ffi.sizeof('int') |
| 1001 |
assert s.a[4][7] == 0 |
| 1002 |
raises(IndexError, 's.a[4][8]') |
| 1003 |
raises(IndexError, 's.a[5][0]') |
| 1004 |
assert ffi.typeof(s.a) == ffi.typeof("int[5][8]") |
| 1005 |
assert ffi.typeof(s.a[0]) == ffi.typeof("int[8]") |
| 1006 |
|
| 1007 |
def test_struct_array_guess_length_3(self): |
| 1008 |
ffi, lib = self.prepare( |
| 1009 |
"struct foo_s { int a[][...]; };", |
| 1010 |
'test_struct_array_guess_length_3', |
| 1011 |
"struct foo_s { int x; int a[5][7]; int y; };") |
| 1012 |
assert ffi.sizeof('struct foo_s') == 37 * ffi.sizeof('int') |
| 1013 |
s = ffi.new("struct foo_s *") |
| 1014 |
assert ffi.typeof(s.a) == ffi.typeof("int[][7]") |
| 1015 |
assert s.a[4][6] == 0 |
| 1016 |
raises(IndexError, 's.a[4][7]') |
| 1017 |
assert ffi.typeof(s.a[0]) == ffi.typeof("int[7]") |
| 1018 |
|
| 1019 |
def test_global_var_array_2(self): |
| 1020 |
ffi, lib = self.prepare( |
| 1021 |
"int a[...][...];", |
| 1022 |
'test_global_var_array_2', |
| 1023 |
'int a[10][8];') |
| 1024 |
lib.a[9][7] = 123456 |
| 1025 |
assert lib.a[9][7] == 123456 |
| 1026 |
raises(IndexError, 'lib.a[0][8]') |
| 1027 |
raises(IndexError, 'lib.a[10][0]') |
| 1028 |
assert ffi.typeof(lib.a) == ffi.typeof("int[10][8]") |
| 1029 |
assert ffi.typeof(lib.a[0]) == ffi.typeof("int[8]") |
| 1030 |
|
| 1031 |
def test_some_integer_type(self): |
| 1032 |
ffi, lib = self.prepare(""" |
| 1033 |
typedef int... foo_t; |
| 1034 |
typedef unsigned long... bar_t; |
| 1035 |
typedef struct { foo_t a, b; } mystruct_t; |
| 1036 |
foo_t foobar(bar_t, mystruct_t); |
| 1037 |
static const bar_t mu = -20; |
| 1038 |
static const foo_t nu = 20; |
| 1039 |
""", 'test_some_integer_type', """ |
| 1040 |
typedef unsigned long long foo_t; |
| 1041 |
typedef short bar_t; |
| 1042 |
typedef struct { foo_t a, b; } mystruct_t; |
| 1043 |
static foo_t foobar(bar_t x, mystruct_t s) { |
| 1044 |
return (foo_t)x + s.a + s.b; |
| 1045 |
} |
| 1046 |
static const bar_t mu = -20; |
| 1047 |
static const foo_t nu = 20; |
| 1048 |
""") |
| 1049 |
assert ffi.sizeof("foo_t") == ffi.sizeof("unsigned long long") |
| 1050 |
assert ffi.sizeof("bar_t") == ffi.sizeof("short") |
| 1051 |
maxulonglong = 2 ** 64 - 1 |
| 1052 |
assert int(ffi.cast("foo_t", -1)) == maxulonglong |
| 1053 |
assert int(ffi.cast("bar_t", -1)) == -1 |
| 1054 |
assert lib.foobar(-1, [0, 0]) == maxulonglong |
| 1055 |
assert lib.foobar(2 ** 15 - 1, [0, 0]) == 2 ** 15 - 1 |
| 1056 |
assert lib.foobar(10, [20, 31]) == 61 |
| 1057 |
assert lib.foobar(0, [0, maxulonglong]) == maxulonglong |
| 1058 |
raises(OverflowError, lib.foobar, 2 ** 15, [0, 0]) |
| 1059 |
raises(OverflowError, lib.foobar, -(2 ** 15) - 1, [0, 0]) |
| 1060 |
raises(OverflowError, ffi.new, "mystruct_t *", [0, -1]) |
| 1061 |
assert lib.mu == -20 |
| 1062 |
assert lib.nu == 20 |
| 1063 |
|
| 1064 |
def test_issue200(self): |
| 1065 |
ffi, lib = self.prepare(""" |
| 1066 |
typedef void (function_t)(void*); |
| 1067 |
void function(void *); |
| 1068 |
""", 'test_issue200', """ |
| 1069 |
static void function(void *p) { (void)p; } |
| 1070 |
""") |
| 1071 |
ffi.typeof('function_t*') |
| 1072 |
lib.function(ffi.NULL) |
| 1073 |
|
| 1074 |
|
| 1075 |
def test_alignment_of_longlong(self): |
| 1076 |
import _cffi_backend |
| 1077 |
BULongLong = _cffi_backend.new_primitive_type('unsigned long long') |
| 1078 |
x1 = _cffi_backend.alignof(BULongLong) |
| 1079 |
assert x1 in [4, 8] |
| 1080 |
|
| 1081 |
ffi, lib = self.prepare( |
| 1082 |
"struct foo_s { unsigned long long x; };", |
| 1083 |
'test_alignment_of_longlong', |
| 1084 |
"struct foo_s { unsigned long long x; };") |
| 1085 |
assert ffi.alignof('unsigned long long') == x1 |
| 1086 |
assert ffi.alignof('struct foo_s') == x1 |
| 1087 |
|
| 1088 |
def test_import_from_lib(self): |
| 1089 |
import sys |
| 1090 |
ffi, lib = self.prepare( |
| 1091 |
"int mybar(int); int myvar;\n#define MYFOO ...", |
| 1092 |
'test_import_from_lib', |
| 1093 |
"#define MYFOO 42\n" |
| 1094 |
"static int mybar(int x) { return x + 1; }\n" |
| 1095 |
"static int myvar = -5;") |
| 1096 |
assert sys.modules['_CFFI_test_import_from_lib'].lib is lib |
| 1097 |
assert sys.modules['_CFFI_test_import_from_lib.lib'] is lib |
| 1098 |
from _CFFI_test_import_from_lib.lib import MYFOO |
| 1099 |
assert MYFOO == 42 |
| 1100 |
assert hasattr(lib, '__dict__') |
| 1101 |
assert lib.__all__ == ['MYFOO', 'mybar'] |
| 1102 |
assert lib.__name__ == '_CFFI_test_import_from_lib.lib' |
| 1103 |
assert lib.__class__ is type(sys) |
| 1104 |
|
| 1105 |
def test_macro_var_callback(self): |
| 1106 |
ffi, lib = self.prepare( |
| 1107 |
"int my_value; int *(*get_my_value)(void);", |
| 1108 |
'test_macro_var_callback', |
| 1109 |
"int *(*get_my_value)(void);\n" |
| 1110 |
"#define my_value (*get_my_value())") |
| 1111 |
|
| 1112 |
values = ffi.new("int[50]") |
| 1113 |
def it(): |
| 1114 |
for i in range(50): |
| 1115 |
yield i |
| 1116 |
it = it() |
| 1117 |
|
| 1118 |
@ffi.callback("int *(*)(void)") |
| 1119 |
def get_my_value(): |
| 1120 |
return values + next(it) |
| 1121 |
lib.get_my_value = get_my_value |
| 1122 |
|
| 1123 |
values[0] = 41 |
| 1124 |
assert lib.my_value == 41 |
| 1125 |
p = ffi.addressof(lib, 'my_value') |
| 1126 |
assert p == values + 1 |
| 1127 |
assert p[-1] == 41 |
| 1128 |
assert p[+1] == 0 |
| 1129 |
lib.my_value = 42 |
| 1130 |
assert values[2] == 42 |
| 1131 |
assert p[-1] == 41 |
| 1132 |
assert p[+1] == 42 |
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
@ffi.callback("int *(*)(void)") |
| 1139 |
def get_my_value(): |
| 1140 |
raise LookupError |
| 1141 |
lib.get_my_value = get_my_value |
| 1142 |
raises(ffi.error, getattr, lib, 'my_value') |
| 1143 |
raises(ffi.error, setattr, lib, 'my_value', 50) |
| 1144 |
raises(ffi.error, ffi.addressof, lib, 'my_value') |
| 1145 |
@ffi.callback("int *(*)(void)") |
| 1146 |
def get_my_value(): |
| 1147 |
return "hello" |
| 1148 |
lib.get_my_value = get_my_value |
| 1149 |
raises(ffi.error, getattr, lib, 'my_value') |
| 1150 |
e = raises(ffi.error, setattr, lib, 'my_value', 50) |
| 1151 |
assert str(e.value) == "global variable 'my_value' is at address NULL" |
| 1152 |
|
| 1153 |
def test_const_fields(self): |
| 1154 |
ffi, lib = self.prepare( |
| 1155 |
"""struct foo_s { const int a; void *const b; };""", |
| 1156 |
'test_const_fields', |
| 1157 |
"""struct foo_s { const int a; void *const b; };""") |
| 1158 |
foo_s = ffi.typeof("struct foo_s") |
| 1159 |
assert foo_s.fields[0][0] == 'a' |
| 1160 |
assert foo_s.fields[0][1].type is ffi.typeof("int") |
| 1161 |
assert foo_s.fields[1][0] == 'b' |
| 1162 |
assert foo_s.fields[1][1].type is ffi.typeof("void *") |
| 1163 |
|
| 1164 |
def test_restrict_fields(self): |
| 1165 |
ffi, lib = self.prepare( |
| 1166 |
"""struct foo_s { void * restrict b; };""", |
| 1167 |
'test_restrict_fields', |
| 1168 |
"""struct foo_s { void * __restrict b; };""") |
| 1169 |
foo_s = ffi.typeof("struct foo_s") |
| 1170 |
assert foo_s.fields[0][0] == 'b' |
| 1171 |
assert foo_s.fields[0][1].type is ffi.typeof("void *") |
| 1172 |
|
| 1173 |
def test_volatile_fields(self): |
| 1174 |
ffi, lib = self.prepare( |
| 1175 |
"""struct foo_s { void * volatile b; };""", |
| 1176 |
'test_volatile_fields', |
| 1177 |
"""struct foo_s { void * volatile b; };""") |
| 1178 |
foo_s = ffi.typeof("struct foo_s") |
| 1179 |
assert foo_s.fields[0][0] == 'b' |
| 1180 |
assert foo_s.fields[0][1].type is ffi.typeof("void *") |
| 1181 |
|
| 1182 |
def test_const_array_fields(self): |
| 1183 |
ffi, lib = self.prepare( |
| 1184 |
"""struct foo_s { const int a[4]; };""", |
| 1185 |
'test_const_array_fields', |
| 1186 |
"""struct foo_s { const int a[4]; };""") |
| 1187 |
foo_s = ffi.typeof("struct foo_s") |
| 1188 |
assert foo_s.fields[0][0] == 'a' |
| 1189 |
assert foo_s.fields[0][1].type is ffi.typeof("int[4]") |
| 1190 |
|
| 1191 |
def test_const_array_fields_varlength(self): |
| 1192 |
ffi, lib = self.prepare( |
| 1193 |
"""struct foo_s { const int a[]; ...; };""", |
| 1194 |
'test_const_array_fields_varlength', |
| 1195 |
"""struct foo_s { const int a[4]; };""") |
| 1196 |
foo_s = ffi.typeof("struct foo_s") |
| 1197 |
assert foo_s.fields[0][0] == 'a' |
| 1198 |
assert foo_s.fields[0][1].type is ffi.typeof("int[]") |
| 1199 |
|
| 1200 |
def test_const_array_fields_unknownlength(self): |
| 1201 |
ffi, lb = self.prepare( |
| 1202 |
"""struct foo_s { const int a[...]; ...; };""", |
| 1203 |
'test_const_array_fields_unknownlength', |
| 1204 |
"""struct foo_s { const int a[4]; };""") |
| 1205 |
foo_s = ffi.typeof("struct foo_s") |
| 1206 |
assert foo_s.fields[0][0] == 'a' |
| 1207 |
assert foo_s.fields[0][1].type is ffi.typeof("int[4]") |
| 1208 |
|
| 1209 |
def test_const_function_args(self): |
| 1210 |
ffi, lib = self.prepare( |
| 1211 |
"""int foobar(const int a, const int *b, const int c[]);""", |
| 1212 |
'test_const_function_args', """ |
| 1213 |
int foobar(const int a, const int *b, const int c[]) { |
| 1214 |
return a + *b + *c; |
| 1215 |
} |
| 1216 |
""") |
| 1217 |
assert lib.foobar(100, ffi.new("int *", 40), ffi.new("int *", 2)) == 142 |
| 1218 |
|
| 1219 |
def test_const_function_type_args(self): |
| 1220 |
ffi, lib = self.prepare( |
| 1221 |
"""int (*foobar)(const int a, const int *b, const int c[]);""", |
| 1222 |
'test_const_function_type_args', """ |
| 1223 |
int (*foobar)(const int a, const int *b, const int c[]); |
| 1224 |
""") |
| 1225 |
t = ffi.typeof(lib.foobar) |
| 1226 |
assert t.args[0] is ffi.typeof("int") |
| 1227 |
assert t.args[1] is ffi.typeof("int *") |
| 1228 |
assert t.args[2] is ffi.typeof("int *") |
| 1229 |
|
| 1230 |
def test_const_constant(self): |
| 1231 |
ffi, lib = self.prepare( |
| 1232 |
"""struct foo_s { int x,y; }; const struct foo_s myfoo;""", |
| 1233 |
'test_const_constant', """ |
| 1234 |
struct foo_s { int x,y; }; const struct foo_s myfoo = { 40, 2 }; |
| 1235 |
""") |
| 1236 |
assert lib.myfoo.x == 40 |
| 1237 |
assert lib.myfoo.y == 2 |
| 1238 |
|
| 1239 |
def test_const_via_typedef(self): |
| 1240 |
ffi, lib = self.prepare( |
| 1241 |
"""typedef const int const_t; const_t aaa;""", |
| 1242 |
'test_const_via_typedef', """ |
| 1243 |
typedef const int const_t; |
| 1244 |
#define aaa 42 |
| 1245 |
""") |
| 1246 |
assert lib.aaa == 42 |
| 1247 |
raises(AttributeError, "lib.aaa = 43") |
| 1248 |
|
| 1249 |
def test_win32_calling_convention_0(self): |
| 1250 |
import sys |
| 1251 |
ffi, lib = self.prepare( |
| 1252 |
""" |
| 1253 |
int call1(int(__cdecl *cb)(int)); |
| 1254 |
int (*const call2)(int(__stdcall *cb)(int)); |
| 1255 |
""", |
| 1256 |
'test_win32_calling_convention_0', r""" |
| 1257 |
#ifndef _MSC_VER |
| 1258 |
# define __stdcall /* nothing */ |
| 1259 |
#endif |
| 1260 |
int call1(int(*cb)(int)) { |
| 1261 |
int i, result = 0; |
| 1262 |
//printf("call1: cb = %p\n", cb); |
| 1263 |
for (i = 0; i < 1000; i++) |
| 1264 |
result += cb(i); |
| 1265 |
//printf("result = %d\n", result); |
| 1266 |
return result; |
| 1267 |
} |
| 1268 |
int call2(int(__stdcall *cb)(int)) { |
| 1269 |
int i, result = 0; |
| 1270 |
//printf("call2: cb = %p\n", cb); |
| 1271 |
for (i = 0; i < 1000; i++) |
| 1272 |
result += cb(-i); |
| 1273 |
//printf("result = %d\n", result); |
| 1274 |
return result; |
| 1275 |
} |
| 1276 |
""") |
| 1277 |
@ffi.callback("int(int)") |
| 1278 |
def cb1(x): |
| 1279 |
return x * 2 |
| 1280 |
@ffi.callback("int __stdcall(int)") |
| 1281 |
def cb2(x): |
| 1282 |
return x * 3 |
| 1283 |
res = lib.call1(cb1) |
| 1284 |
assert res == 500*999*2 |
| 1285 |
assert res == ffi.addressof(lib, 'call1')(cb1) |
| 1286 |
res = lib.call2(cb2) |
| 1287 |
assert res == -500*999*3 |
| 1288 |
assert res == ffi.addressof(lib, 'call2')(cb2) |
| 1289 |
if sys.platform == 'win32' and not sys.maxsize > 2**32: |
| 1290 |
assert '__stdcall' in str(ffi.typeof(cb2)) |
| 1291 |
assert '__stdcall' not in str(ffi.typeof(cb1)) |
| 1292 |
raises(TypeError, lib.call1, cb2) |
| 1293 |
raises(TypeError, lib.call2, cb1) |
| 1294 |
else: |
| 1295 |
assert '__stdcall' not in str(ffi.typeof(cb2)) |
| 1296 |
assert ffi.typeof(cb2) is ffi.typeof(cb1) |
| 1297 |
|
| 1298 |
def test_win32_calling_convention_1(self): |
| 1299 |
ffi, lib = self.prepare(""" |
| 1300 |
int __cdecl call1(int(__cdecl *cb)(int)); |
| 1301 |
int __stdcall call2(int(__stdcall *cb)(int)); |
| 1302 |
int (__cdecl *const cb1)(int); |
| 1303 |
int (__stdcall *const cb2)(int); |
| 1304 |
""", 'test_win32_calling_convention_1', r""" |
| 1305 |
#ifndef _MSC_VER |
| 1306 |
# define __cdecl |
| 1307 |
# define __stdcall |
| 1308 |
#endif |
| 1309 |
int __cdecl cb1(int x) { return x * 2; } |
| 1310 |
int __stdcall cb2(int x) { return x * 3; } |
| 1311 |
|
| 1312 |
int __cdecl call1(int(__cdecl *cb)(int)) { |
| 1313 |
int i, result = 0; |
| 1314 |
//printf("here1\n"); |
| 1315 |
//printf("cb = %p, cb1 = %p\n", cb, (void *)cb1); |
| 1316 |
for (i = 0; i < 1000; i++) |
| 1317 |
result += cb(i); |
| 1318 |
//printf("result = %d\n", result); |
| 1319 |
return result; |
| 1320 |
} |
| 1321 |
int __stdcall call2(int(__stdcall *cb)(int)) { |
| 1322 |
int i, result = 0; |
| 1323 |
//printf("here1\n"); |
| 1324 |
//printf("cb = %p, cb2 = %p\n", cb, (void *)cb2); |
| 1325 |
for (i = 0; i < 1000; i++) |
| 1326 |
result += cb(-i); |
| 1327 |
//printf("result = %d\n", result); |
| 1328 |
return result; |
| 1329 |
} |
| 1330 |
""") |
| 1331 |
|
| 1332 |
ptr_call1 = ffi.addressof(lib, 'call1') |
| 1333 |
assert lib.call1(ffi.addressof(lib, 'cb1')) == 500*999*2 |
| 1334 |
assert ptr_call1(ffi.addressof(lib, 'cb1')) == 500*999*2 |
| 1335 |
|
| 1336 |
ptr_call2 = ffi.addressof(lib, 'call2') |
| 1337 |
assert lib.call2(ffi.addressof(lib, 'cb2')) == -500*999*3 |
| 1338 |
assert ptr_call2(ffi.addressof(lib, 'cb2')) == -500*999*3 |
| 1339 |
|
| 1340 |
|
| 1341 |
def test_win32_calling_convention_2(self): |
| 1342 |
import sys |
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
ffi, lib = self.prepare(""" |
| 1348 |
int __stdcall call1(int(__cdecl *cb)(int)); |
| 1349 |
int __cdecl call2(int(__stdcall *cb)(int)); |
| 1350 |
int (__cdecl *const cb1)(int); |
| 1351 |
int (__stdcall *const cb2)(int); |
| 1352 |
""", 'test_win32_calling_convention_2', """ |
| 1353 |
#ifndef _MSC_VER |
| 1354 |
# define __cdecl |
| 1355 |
# define __stdcall |
| 1356 |
#endif |
| 1357 |
int __cdecl call1(int(__cdecl *cb)(int)) { |
| 1358 |
int i, result = 0; |
| 1359 |
for (i = 0; i < 1000; i++) |
| 1360 |
result += cb(i); |
| 1361 |
return result; |
| 1362 |
} |
| 1363 |
int __stdcall call2(int(__stdcall *cb)(int)) { |
| 1364 |
int i, result = 0; |
| 1365 |
for (i = 0; i < 1000; i++) |
| 1366 |
result += cb(-i); |
| 1367 |
return result; |
| 1368 |
} |
| 1369 |
int __cdecl cb1(int x) { return x * 2; } |
| 1370 |
int __stdcall cb2(int x) { return x * 3; } |
| 1371 |
""") |
| 1372 |
ptr_call1 = ffi.addressof(lib, 'call1') |
| 1373 |
ptr_call2 = ffi.addressof(lib, 'call2') |
| 1374 |
if sys.platform == 'win32' and not sys.maxsize > 2**32: |
| 1375 |
raises(TypeError, lib.call1, ffi.addressof(lib, 'cb2')) |
| 1376 |
raises(TypeError, ptr_call1, ffi.addressof(lib, 'cb2')) |
| 1377 |
raises(TypeError, lib.call2, ffi.addressof(lib, 'cb1')) |
| 1378 |
raises(TypeError, ptr_call2, ffi.addressof(lib, 'cb1')) |
| 1379 |
assert lib.call1(ffi.addressof(lib, 'cb1')) == 500*999*2 |
| 1380 |
assert ptr_call1(ffi.addressof(lib, 'cb1')) == 500*999*2 |
| 1381 |
assert lib.call2(ffi.addressof(lib, 'cb2')) == -500*999*3 |
| 1382 |
assert ptr_call2(ffi.addressof(lib, 'cb2')) == -500*999*3 |
| 1383 |
|
| 1384 |
def test_win32_calling_convention_3(self): |
| 1385 |
import sys |
| 1386 |
ffi, lib = self.prepare(""" |
| 1387 |
struct point { int x, y; }; |
| 1388 |
|
| 1389 |
int (*const cb1)(struct point); |
| 1390 |
int (__stdcall *const cb2)(struct point); |
| 1391 |
|
| 1392 |
struct point __stdcall call1(int(*cb)(struct point)); |
| 1393 |
struct point call2(int(__stdcall *cb)(struct point)); |
| 1394 |
""", 'test_win32_calling_convention_3', r""" |
| 1395 |
#ifndef _MSC_VER |
| 1396 |
# define __cdecl |
| 1397 |
# define __stdcall |
| 1398 |
#endif |
| 1399 |
struct point { int x, y; }; |
| 1400 |
int cb1(struct point pt) { return pt.x + 10 * pt.y; } |
| 1401 |
int __stdcall cb2(struct point pt) { return pt.x + 100 * pt.y; } |
| 1402 |
struct point __stdcall call1(int(__cdecl *cb)(struct point)) { |
| 1403 |
int i; |
| 1404 |
struct point result = { 0, 0 }; |
| 1405 |
//printf("here1\n"); |
| 1406 |
//printf("cb = %p, cb1 = %p\n", cb, (void *)cb1); |
| 1407 |
for (i = 0; i < 1000; i++) { |
| 1408 |
struct point p = { i, -i }; |
| 1409 |
int r = cb(p); |
| 1410 |
result.x += r; |
| 1411 |
result.y -= r; |
| 1412 |
} |
| 1413 |
return result; |
| 1414 |
} |
| 1415 |
struct point __cdecl call2(int(__stdcall *cb)(struct point)) { |
| 1416 |
int i; |
| 1417 |
struct point result = { 0, 0 }; |
| 1418 |
for (i = 0; i < 1000; i++) { |
| 1419 |
struct point p = { -i, i }; |
| 1420 |
int r = cb(p); |
| 1421 |
result.x += r; |
| 1422 |
result.y -= r; |
| 1423 |
} |
| 1424 |
return result; |
| 1425 |
} |
| 1426 |
""") |
| 1427 |
ptr_call1 = ffi.addressof(lib, 'call1') |
| 1428 |
ptr_call2 = ffi.addressof(lib, 'call2') |
| 1429 |
if sys.platform == 'win32' and not sys.maxsize > 2**32: |
| 1430 |
raises(TypeError, lib.call1, ffi.addressof(lib, 'cb2')) |
| 1431 |
raises(TypeError, ptr_call1, ffi.addressof(lib, 'cb2')) |
| 1432 |
raises(TypeError, lib.call2, ffi.addressof(lib, 'cb1')) |
| 1433 |
raises(TypeError, ptr_call2, ffi.addressof(lib, 'cb1')) |
| 1434 |
pt = lib.call1(ffi.addressof(lib, 'cb1')) |
| 1435 |
assert (pt.x, pt.y) == (-9*500*999, 9*500*999) |
| 1436 |
pt = ptr_call1(ffi.addressof(lib, 'cb1')) |
| 1437 |
assert (pt.x, pt.y) == (-9*500*999, 9*500*999) |
| 1438 |
pt = lib.call2(ffi.addressof(lib, 'cb2')) |
| 1439 |
assert (pt.x, pt.y) == (99*500*999, -99*500*999) |
| 1440 |
pt = ptr_call2(ffi.addressof(lib, 'cb2')) |
| 1441 |
assert (pt.x, pt.y) == (99*500*999, -99*500*999) |
| 1442 |
|
| 1443 |
def test_share_FILE(self): |
| 1444 |
ffi1, lib1 = self.prepare("void do_stuff(FILE *);", |
| 1445 |
'test_share_FILE_a', |
| 1446 |
"void do_stuff(FILE *f) { (void)f; }") |
| 1447 |
ffi2, lib2 = self.prepare("FILE *barize(void);", |
| 1448 |
'test_share_FILE_b', |
| 1449 |
"FILE *barize(void) { return NULL; }") |
| 1450 |
lib1.do_stuff(lib2.barize()) |
| 1451 |
|
| 1452 |
def w_StdErrCapture(self, fd=False): |
| 1453 |
if fd: |
| 1454 |
|
| 1455 |
|
| 1456 |
import os |
| 1457 |
class MiniStringIO(object): |
| 1458 |
def __init__(self): |
| 1459 |
self._rd, self._wr = os.pipe() |
| 1460 |
self._result = None |
| 1461 |
def getvalue(self): |
| 1462 |
if self._result is None: |
| 1463 |
os.close(self._wr) |
| 1464 |
self._result = os.read(self._rd, 4096).decode() |
| 1465 |
os.close(self._rd) |
| 1466 |
|
| 1467 |
while self._result.startswith('[platform:execute]'): |
| 1468 |
self._result = ''.join( |
| 1469 |
self._result.splitlines(True)[1:]) |
| 1470 |
return self._result |
| 1471 |
class StdErrCapture(object): |
| 1472 |
def __enter__(self): |
| 1473 |
f = MiniStringIO() |
| 1474 |
self.old_fd2 = os.dup(2) |
| 1475 |
os.dup2(f._wr, 2) |
| 1476 |
return f |
| 1477 |
def __exit__(self, *args): |
| 1478 |
os.dup2(self.old_fd2, 2) |
| 1479 |
os.close(self.old_fd2) |
| 1480 |
return StdErrCapture() |
| 1481 |
else: |
| 1482 |
import sys |
| 1483 |
class MiniStringIO(object): |
| 1484 |
def __init__(self): |
| 1485 |
self._lst = [] |
| 1486 |
self.write = self._lst.append |
| 1487 |
def getvalue(self): |
| 1488 |
return ''.join(self._lst) |
| 1489 |
class StdErrCapture(object): |
| 1490 |
def __enter__(self): |
| 1491 |
self.old_stderr = sys.stderr |
| 1492 |
sys.stderr = f = MiniStringIO() |
| 1493 |
return f |
| 1494 |
def __exit__(self, *args): |
| 1495 |
sys.stderr = self.old_stderr |
| 1496 |
return StdErrCapture() |
| 1497 |
|
| 1498 |
def test_extern_python_1(self): |
| 1499 |
ffi, lib = self.prepare(""" |
| 1500 |
extern "Python" { |
| 1501 |
int bar(int, int); |
| 1502 |
void baz(int, int); |
| 1503 |
int bok(void); |
| 1504 |
void boz(void); |
| 1505 |
} |
| 1506 |
""", 'test_extern_python_1', "") |
| 1507 |
assert ffi.typeof(lib.bar) == ffi.typeof("int(*)(int, int)") |
| 1508 |
with self.StdErrCapture(fd=True) as f: |
| 1509 |
res = lib.bar(4, 5) |
| 1510 |
assert res == 0 |
| 1511 |
assert f.getvalue() in ( |
| 1512 |
|
| 1513 |
"extern \"Python\": function bar() called, but no code was attached " |
| 1514 |
"to it yet with @ffi.def_extern(). Returning 0.\n", |
| 1515 |
|
| 1516 |
"extern \"Python\": function _CFFI_test_extern_python_1.bar() " |
| 1517 |
"called, but no code was attached " |
| 1518 |
"to it yet with @ffi.def_extern(). Returning 0.\n") |
| 1519 |
|
| 1520 |
@ffi.def_extern("bar") |
| 1521 |
def my_bar(x, y): |
| 1522 |
seen.append(("Bar", x, y)) |
| 1523 |
return x * y |
| 1524 |
assert my_bar != lib.bar |
| 1525 |
seen = [] |
| 1526 |
res = lib.bar(6, 7) |
| 1527 |
assert seen == [("Bar", 6, 7)] |
| 1528 |
assert res == 42 |
| 1529 |
|
| 1530 |
def baz(x, y): |
| 1531 |
seen.append(("Baz", x, y)) |
| 1532 |
baz1 = ffi.def_extern()(baz) |
| 1533 |
assert baz1 is baz |
| 1534 |
seen = [] |
| 1535 |
baz(40, 4) |
| 1536 |
res = lib.baz(50, 8) |
| 1537 |
assert res is None |
| 1538 |
assert seen == [("Baz", 40, 4), ("Baz", 50, 8)] |
| 1539 |
assert type(seen[0][1]) is type(seen[0][2]) is int |
| 1540 |
assert type(seen[1][1]) is type(seen[1][2]) is int |
| 1541 |
|
| 1542 |
@ffi.def_extern(name="bok") |
| 1543 |
def bokk(): |
| 1544 |
seen.append("Bok") |
| 1545 |
return 42 |
| 1546 |
seen = [] |
| 1547 |
assert lib.bok() == 42 |
| 1548 |
assert seen == ["Bok"] |
| 1549 |
|
| 1550 |
@ffi.def_extern() |
| 1551 |
def boz(): |
| 1552 |
seen.append("Boz") |
| 1553 |
seen = [] |
| 1554 |
assert lib.boz() is None |
| 1555 |
assert seen == ["Boz"] |
| 1556 |
|
| 1557 |
def test_extern_python_bogus_name(self): |
| 1558 |
ffi, lib = self.prepare("int abc;", |
| 1559 |
'test_extern_python_bogus_name', |
| 1560 |
"int abc;") |
| 1561 |
def fn(): |
| 1562 |
pass |
| 1563 |
raises(ffi.error, ffi.def_extern("unknown_name"), fn) |
| 1564 |
raises(ffi.error, ffi.def_extern("abc"), fn) |
| 1565 |
assert lib.abc == 0 |
| 1566 |
e = raises(ffi.error, ffi.def_extern("abc"), fn) |
| 1567 |
assert str(e.value) == ("ffi.def_extern('abc'): no 'extern \"Python\"' " |
| 1568 |
"function with this name") |
| 1569 |
e = raises(ffi.error, ffi.def_extern(), fn) |
| 1570 |
assert str(e.value) == ("ffi.def_extern('fn'): no 'extern \"Python\"' " |
| 1571 |
"function with this name") |
| 1572 |
|
| 1573 |
raises(TypeError, ffi.def_extern(42), fn) |
| 1574 |
raises((TypeError, AttributeError), ffi.def_extern(), "foo") |
| 1575 |
class X: |
| 1576 |
pass |
| 1577 |
x = X() |
| 1578 |
x.__name__ = x |
| 1579 |
raises(TypeError, ffi.def_extern(), x) |
| 1580 |
|
| 1581 |
def test_extern_python_bogus_result_type(self): |
| 1582 |
ffi, lib = self.prepare("""extern "Python" void bar(int);""", |
| 1583 |
'test_extern_python_bogus_result_type', |
| 1584 |
"") |
| 1585 |
@ffi.def_extern() |
| 1586 |
def bar(n): |
| 1587 |
return n * 10 |
| 1588 |
with self.StdErrCapture() as f: |
| 1589 |
res = lib.bar(321) |
| 1590 |
msg = f.getvalue() |
| 1591 |
assert res is None |
| 1592 |
assert "rom cffi callback %r" % (bar,) in msg |
| 1593 |
assert "rying to convert the result back to C:\n" in msg |
| 1594 |
assert msg.endswith( |
| 1595 |
"TypeError: callback with the return type 'void' must return None\n") |
| 1596 |
|
| 1597 |
def test_extern_python_redefine(self): |
| 1598 |
ffi, lib = self.prepare("""extern "Python" int bar(int);""", |
| 1599 |
'test_extern_python_redefine', |
| 1600 |
"") |
| 1601 |
@ffi.def_extern() |
| 1602 |
def bar(n): |
| 1603 |
return n * 10 |
| 1604 |
assert lib.bar(42) == 420 |
| 1605 |
|
| 1606 |
@ffi.def_extern() |
| 1607 |
def bar(n): |
| 1608 |
return -n |
| 1609 |
assert lib.bar(42) == -42 |
| 1610 |
|
| 1611 |
def test_extern_python_struct(self): |
| 1612 |
ffi, lib = self.prepare(""" |
| 1613 |
struct foo_s { int a, b, c; }; |
| 1614 |
extern "Python" int bar(int, struct foo_s, int); |
| 1615 |
extern "Python" { struct foo_s baz(int, int); |
| 1616 |
struct foo_s bok(void); } |
| 1617 |
""", 'test_extern_python_struct', |
| 1618 |
"struct foo_s { int a, b, c; };") |
| 1619 |
|
| 1620 |
@ffi.def_extern() |
| 1621 |
def bar(x, s, z): |
| 1622 |
return x + s.a + s.b + s.c + z |
| 1623 |
res = lib.bar(1000, [1001, 1002, 1004], 1008) |
| 1624 |
assert res == 5015 |
| 1625 |
|
| 1626 |
@ffi.def_extern() |
| 1627 |
def baz(x, y): |
| 1628 |
return [x + y, x - y, x * y] |
| 1629 |
res = lib.baz(1000, 42) |
| 1630 |
assert res.a == 1042 |
| 1631 |
assert res.b == 958 |
| 1632 |
assert res.c == 42000 |
| 1633 |
|
| 1634 |
@ffi.def_extern() |
| 1635 |
def bok(): |
| 1636 |
return [10, 20, 30] |
| 1637 |
res = lib.bok() |
| 1638 |
assert [res.a, res.b, res.c] == [10, 20, 30] |
| 1639 |
|
| 1640 |
@pytest.mark.skipif("not config.option.runappdirect", reason="no longdouble in _rawrffi/alt") |
| 1641 |
def test_extern_python_long_double(self): |
| 1642 |
ffi, lib = self.prepare(""" |
| 1643 |
extern "Python" int bar(int, long double, int); |
| 1644 |
extern "Python" long double baz(int, int); |
| 1645 |
extern "Python" long double bok(void); |
| 1646 |
""", 'test_extern_python_long_double', "") |
| 1647 |
|
| 1648 |
@ffi.def_extern() |
| 1649 |
def bar(x, l, z): |
| 1650 |
seen.append((x, l, z)) |
| 1651 |
return 6 |
| 1652 |
seen = [] |
| 1653 |
lib.bar(10, 3.5, 20) |
| 1654 |
expected = ffi.cast("long double", 3.5) |
| 1655 |
assert repr(seen) == repr([(10, expected, 20)]) |
| 1656 |
|
| 1657 |
@ffi.def_extern() |
| 1658 |
def baz(x, z): |
| 1659 |
assert x == 10 and z == 20 |
| 1660 |
return expected |
| 1661 |
res = lib.baz(10, 20) |
| 1662 |
assert repr(res) == repr(expected) |
| 1663 |
|
| 1664 |
@ffi.def_extern() |
| 1665 |
def bok(): |
| 1666 |
return expected |
| 1667 |
res = lib.bok() |
| 1668 |
assert repr(res) == repr(expected) |
| 1669 |
|
| 1670 |
def test_extern_python_signature(self): |
| 1671 |
ffi, lib = self.prepare("", 'test_extern_python_signature', "") |
| 1672 |
raises(TypeError, ffi.def_extern(425), None) |
| 1673 |
raises(TypeError, ffi.def_extern, 'a', 'b', 'c', 'd') |
| 1674 |
|
| 1675 |
def test_extern_python_errors(self): |
| 1676 |
ffi, lib = self.prepare(""" |
| 1677 |
extern "Python" int bar(int); |
| 1678 |
""", 'test_extern_python_errors', "") |
| 1679 |
|
| 1680 |
seen = [] |
| 1681 |
def oops(*args): |
| 1682 |
seen.append(args) |
| 1683 |
|
| 1684 |
@ffi.def_extern(onerror=oops) |
| 1685 |
def bar(x): |
| 1686 |
return x + "" |
| 1687 |
assert lib.bar(10) == 0 |
| 1688 |
|
| 1689 |
@ffi.def_extern(name="bar", onerror=oops, error=-66) |
| 1690 |
def bar2(x): |
| 1691 |
return x + "" |
| 1692 |
assert lib.bar(10) == -66 |
| 1693 |
|
| 1694 |
assert len(seen) == 2 |
| 1695 |
exc, val, tb = seen[0] |
| 1696 |
assert exc is TypeError |
| 1697 |
assert isinstance(val, TypeError) |
| 1698 |
assert tb.tb_frame.f_code.co_name == "bar" |
| 1699 |
exc, val, tb = seen[1] |
| 1700 |
assert exc is TypeError |
| 1701 |
assert isinstance(val, TypeError) |
| 1702 |
assert tb.tb_frame.f_code.co_name == "bar2" |
| 1703 |
|
| 1704 |
|
| 1705 |
raises(TypeError, ffi.def_extern(name='bar', onerror=42), |
| 1706 |
lambda x: x) |
| 1707 |
|
| 1708 |
@pytest.mark.skipif("not config.option.runappdirect") |
| 1709 |
def test_extern_python_stdcall(self): |
| 1710 |
ffi, lib = self.prepare(""" |
| 1711 |
extern "Python" int __stdcall foo(int); |
| 1712 |
extern "Python" int WINAPI bar(int); |
| 1713 |
int (__stdcall * mycb1)(int); |
| 1714 |
int indirect_call(int); |
| 1715 |
""", 'test_extern_python_stdcall', """ |
| 1716 |
#ifndef _MSC_VER |
| 1717 |
# define __stdcall |
| 1718 |
#endif |
| 1719 |
static int (__stdcall * mycb1)(int); |
| 1720 |
static int indirect_call(int x) { |
| 1721 |
return mycb1(x); |
| 1722 |
} |
| 1723 |
""") |
| 1724 |
|
| 1725 |
@ffi.def_extern() |
| 1726 |
def foo(x): |
| 1727 |
return x + 42 |
| 1728 |
@ffi.def_extern() |
| 1729 |
def bar(x): |
| 1730 |
return x + 43 |
| 1731 |
assert lib.foo(100) == 142 |
| 1732 |
assert lib.bar(100) == 143 |
| 1733 |
lib.mycb1 = lib.foo |
| 1734 |
assert lib.mycb1(200) == 242 |
| 1735 |
assert lib.indirect_call(300) == 342 |
| 1736 |
|
| 1737 |
def test_introspect_function(self): |
| 1738 |
ffi, lib = self.prepare(""" |
| 1739 |
float f1(double); |
| 1740 |
""", 'test_introspect_function', """ |
| 1741 |
float f1(double x) { return x; } |
| 1742 |
""") |
| 1743 |
assert dir(lib) == ['f1'] |
| 1744 |
FUNC = ffi.typeof(lib.f1) |
| 1745 |
assert FUNC.kind == 'function' |
| 1746 |
assert FUNC.args[0].cname == 'double' |
| 1747 |
assert FUNC.result.cname == 'float' |
| 1748 |
assert ffi.typeof(ffi.addressof(lib, 'f1')) is FUNC |
| 1749 |
|
| 1750 |
def test_introspect_global_var(self): |
| 1751 |
ffi, lib = self.prepare(""" |
| 1752 |
float g1; |
| 1753 |
""", 'test_introspect_global_var', """ |
| 1754 |
float g1; |
| 1755 |
""") |
| 1756 |
assert dir(lib) == ['g1'] |
| 1757 |
FLOATPTR = ffi.typeof(ffi.addressof(lib, 'g1')) |
| 1758 |
assert FLOATPTR.kind == 'pointer' |
| 1759 |
assert FLOATPTR.item.cname == 'float' |
| 1760 |
|
| 1761 |
def test_introspect_global_var_array(self): |
| 1762 |
ffi, lib = self.prepare(""" |
| 1763 |
float g1[100]; |
| 1764 |
""", 'test_introspect_global_var_array', """ |
| 1765 |
float g1[100]; |
| 1766 |
""") |
| 1767 |
assert dir(lib) == ['g1'] |
| 1768 |
FLOATARRAYPTR = ffi.typeof(ffi.addressof(lib, 'g1')) |
| 1769 |
assert FLOATARRAYPTR.kind == 'pointer' |
| 1770 |
assert FLOATARRAYPTR.item.kind == 'array' |
| 1771 |
assert FLOATARRAYPTR.item.length == 100 |
| 1772 |
assert ffi.typeof(lib.g1) is FLOATARRAYPTR.item |
| 1773 |
|
| 1774 |
def test_introspect_integer_const(self): |
| 1775 |
ffi, lib = self.prepare("#define FOO 42", |
| 1776 |
'test_introspect_integer_const', """ |
| 1777 |
#define FOO 42 |
| 1778 |
""") |
| 1779 |
assert dir(lib) == ['FOO'] |
| 1780 |
assert lib.FOO == ffi.integer_const('FOO') == 42 |
| 1781 |
|
| 1782 |
def test_introspect_typedef(self): |
| 1783 |
ffi, lib = self.prepare("typedef int foo_t;", |
| 1784 |
'test_introspect_typedef', """ |
| 1785 |
typedef int foo_t; |
| 1786 |
""") |
| 1787 |
assert ffi.list_types() == (['foo_t'], [], []) |
| 1788 |
assert ffi.typeof('foo_t').kind == 'primitive' |
| 1789 |
assert ffi.typeof('foo_t').cname == 'int' |
| 1790 |
|
| 1791 |
def test_introspect_typedef_multiple(self): |
| 1792 |
ffi, lib = self.prepare(""" |
| 1793 |
typedef signed char a_t, c_t, g_t, b_t; |
| 1794 |
""", 'test_introspect_typedef_multiple', """ |
| 1795 |
typedef signed char a_t, c_t, g_t, b_t; |
| 1796 |
""") |
| 1797 |
assert ffi.list_types() == (['a_t', 'b_t', 'c_t', 'g_t'], [], []) |
| 1798 |
|
| 1799 |
def test_introspect_struct(self): |
| 1800 |
ffi, lib = self.prepare(""" |
| 1801 |
struct foo_s { int a; }; |
| 1802 |
""", 'test_introspect_struct', """ |
| 1803 |
struct foo_s { int a; }; |
| 1804 |
""") |
| 1805 |
assert ffi.list_types() == ([], ['foo_s'], []) |
| 1806 |
assert ffi.typeof('struct foo_s').kind == 'struct' |
| 1807 |
assert ffi.typeof('struct foo_s').cname == 'struct foo_s' |
| 1808 |
|
| 1809 |
def test_introspect_union(self): |
| 1810 |
ffi, lib = self.prepare(""" |
| 1811 |
union foo_s { int a; }; |
| 1812 |
""", 'test_introspect_union', """ |
| 1813 |
union foo_s { int a; }; |
| 1814 |
""") |
| 1815 |
assert ffi.list_types() == ([], [], ['foo_s']) |
| 1816 |
assert ffi.typeof('union foo_s').kind == 'union' |
| 1817 |
assert ffi.typeof('union foo_s').cname == 'union foo_s' |
| 1818 |
|
| 1819 |
def test_introspect_struct_and_typedef(self): |
| 1820 |
ffi, lib = self.prepare(""" |
| 1821 |
typedef struct { int a; } foo_t; |
| 1822 |
""", 'test_introspect_struct_and_typedef', """ |
| 1823 |
typedef struct { int a; } foo_t; |
| 1824 |
""") |
| 1825 |
assert ffi.list_types() == (['foo_t'], [], []) |
| 1826 |
assert ffi.typeof('foo_t').kind == 'struct' |
| 1827 |
assert ffi.typeof('foo_t').cname == 'foo_t' |
| 1828 |
|
| 1829 |
def test_introspect_included_type(self): |
| 1830 |
SOURCE = """ |
| 1831 |
typedef signed char schar_t; |
| 1832 |
struct sint_t { int x; }; |
| 1833 |
""" |
| 1834 |
ffi1, lib1 = self.prepare(SOURCE, |
| 1835 |
"test_introspect_included_type_parent", SOURCE) |
| 1836 |
ffi2, lib2 = self.prepare("", |
| 1837 |
"test_introspect_included_type", SOURCE, |
| 1838 |
includes=[ffi1]) |
| 1839 |
assert ffi1.list_types() == ffi2.list_types() == ( |
| 1840 |
['schar_t'], ['sint_t'], []) |
| 1841 |
|
| 1842 |
def test_introspect_order(self): |
| 1843 |
ffi, lib = self.prepare(""" |
| 1844 |
union CFFIaaa { int a; }; typedef struct CFFIccc { int a; } CFFIb; |
| 1845 |
union CFFIg { int a; }; typedef struct CFFIcc { int a; } CFFIbbb; |
| 1846 |
union CFFIaa { int a; }; typedef struct CFFIa { int a; } CFFIbb; |
| 1847 |
""", "test_introspect_order", """ |
| 1848 |
union CFFIaaa { int a; }; typedef struct CFFIccc { int a; } CFFIb; |
| 1849 |
union CFFIg { int a; }; typedef struct CFFIcc { int a; } CFFIbbb; |
| 1850 |
union CFFIaa { int a; }; typedef struct CFFIa { int a; } CFFIbb; |
| 1851 |
""") |
| 1852 |
assert ffi.list_types() == (['CFFIb', 'CFFIbb', 'CFFIbbb'], |
| 1853 |
['CFFIa', 'CFFIcc', 'CFFIccc'], |
| 1854 |
['CFFIaa', 'CFFIaaa', 'CFFIg']) |
| 1855 |
|
| 1856 |
def test_FFIFunctionWrapper(self): |
| 1857 |
ffi, lib = self.prepare("void f(void);", "test_FFIFunctionWrapper", |
| 1858 |
"void f(void) { }") |
| 1859 |
assert lib.f.__get__(42) is lib.f |
| 1860 |
assert lib.f.__get__(42, int) is lib.f |
| 1861 |
|
| 1862 |
def test_function_returns_float_complex(self): |
| 1863 |
import sys |
| 1864 |
if sys.platform == 'win32': |
| 1865 |
skip("MSVC may not support _Complex") |
| 1866 |
ffi, lib = self.prepare( |
| 1867 |
"float _Complex f1(float a, float b);", |
| 1868 |
"test_function_returns_float_complex", """ |
| 1869 |
#include <complex.h> |
| 1870 |
static float _Complex f1(float a, float b) { return a + I*2.0*b; } |
| 1871 |
""", min_version=(1, 11, 0)) |
| 1872 |
result = lib.f1(1.25, 5.1) |
| 1873 |
assert type(result) == complex |
| 1874 |
assert result.real == 1.25 |
| 1875 |
assert (result.imag != 2*5.1) and (abs(result.imag - 2*5.1) < 1e-5) |
| 1876 |
|
| 1877 |
def test_function_returns_double_complex(self): |
| 1878 |
import sys |
| 1879 |
if sys.platform == 'win32': |
| 1880 |
skip("MSVC may not support _Complex") |
| 1881 |
ffi, lib = self.prepare( |
| 1882 |
"double _Complex f1(double a, double b);", |
| 1883 |
"test_function_returns_double_complex", """ |
| 1884 |
#include <complex.h> |
| 1885 |
static double _Complex f1(double a, double b) { return a + I*2.0*b; } |
| 1886 |
""", min_version=(1, 11, 0)) |
| 1887 |
result = lib.f1(1.25, 5.1) |
| 1888 |
assert type(result) == complex |
| 1889 |
assert result.real == 1.25 |
| 1890 |
assert result.imag == 2*5.1 |
| 1891 |
|
| 1892 |
def test_function_argument_float_complex(self): |
| 1893 |
import sys |
| 1894 |
if sys.platform == 'win32': |
| 1895 |
skip("MSVC may not support _Complex") |
| 1896 |
ffi, lib = self.prepare( |
| 1897 |
"float f1(float _Complex x);", |
| 1898 |
"test_function_argument_float_complex", """ |
| 1899 |
#include <complex.h> |
| 1900 |
static float f1(float _Complex x) { return cabsf(x); } |
| 1901 |
""", min_version=(1, 11, 0)) |
| 1902 |
x = complex(12.34, 56.78) |
| 1903 |
result = lib.f1(x) |
| 1904 |
assert abs(result - abs(x)) < 1e-5 |
| 1905 |
result2 = lib.f1(ffi.cast("float _Complex", x)) |
| 1906 |
assert result2 == result |
| 1907 |
|
| 1908 |
def test_function_argument_double_complex(self): |
| 1909 |
import sys |
| 1910 |
if sys.platform == 'win32': |
| 1911 |
skip("MSVC may not support _Complex") |
| 1912 |
ffi, lib = self.prepare( |
| 1913 |
"double f1(double _Complex);", |
| 1914 |
"test_function_argument_double_complex", """ |
| 1915 |
#include <complex.h> |
| 1916 |
static double f1(double _Complex x) { return cabs(x); } |
| 1917 |
""", min_version=(1, 11, 0)) |
| 1918 |
x = complex(12.34, 56.78) |
| 1919 |
result = lib.f1(x) |
| 1920 |
assert abs(result - abs(x)) < 1e-11 |
| 1921 |
result2 = lib.f1(ffi.cast("double _Complex", x)) |
| 1922 |
assert result2 == result |
| 1923 |
|
| 1924 |
def test_typedef_array_dotdotdot(self): |
| 1925 |
ffi, lib = self.prepare(""" |
| 1926 |
typedef int foo_t[...], bar_t[...]; |
| 1927 |
int gv[...]; |
| 1928 |
typedef int mat_t[...][...]; |
| 1929 |
typedef int vmat_t[][...]; |
| 1930 |
""", |
| 1931 |
"test_typedef_array_dotdotdot", """ |
| 1932 |
typedef int foo_t[50], bar_t[50]; |
| 1933 |
int gv[23]; |
| 1934 |
typedef int mat_t[6][7]; |
| 1935 |
typedef int vmat_t[][8]; |
| 1936 |
""", min_version=(1, 8, 4)) |
| 1937 |
assert ffi.sizeof("foo_t") == 50 * ffi.sizeof("int") |
| 1938 |
assert ffi.sizeof("bar_t") == 50 * ffi.sizeof("int") |
| 1939 |
assert len(ffi.new("foo_t")) == 50 |
| 1940 |
assert len(ffi.new("bar_t")) == 50 |
| 1941 |
assert ffi.sizeof(lib.gv) == 23 * ffi.sizeof("int") |
| 1942 |
assert ffi.sizeof("mat_t") == 6 * 7 * ffi.sizeof("int") |
| 1943 |
assert len(ffi.new("mat_t")) == 6 |
| 1944 |
assert len(ffi.new("mat_t")[3]) == 7 |
| 1945 |
raises(ffi.error, ffi.sizeof, "vmat_t") |
| 1946 |
p = ffi.new("vmat_t", 4) |
| 1947 |
assert ffi.sizeof(p[3]) == 8 * ffi.sizeof("int") |
| 1948 |
|
| 1949 |
def test_call_with_custom_field_pos(self): |
| 1950 |
ffi, lib = self.prepare(""" |
| 1951 |
struct foo { int x; ...; }; |
| 1952 |
struct foo f(void); |
| 1953 |
struct foo g(int, ...); |
| 1954 |
""", "test_call_with_custom_field_pos", """ |
| 1955 |
struct foo { int y, x; }; |
| 1956 |
struct foo f(void) { |
| 1957 |
struct foo s = { 40, 200 }; |
| 1958 |
return s; |
| 1959 |
} |
| 1960 |
struct foo g(int a, ...) { } |
| 1961 |
""") |
| 1962 |
assert lib.f().x == 200 |
| 1963 |
e = raises(NotImplementedError, lib.g, 0) |
| 1964 |
assert str(e.value) == ( |
| 1965 |
'ctype \'struct foo\' not supported as return value. It is a ' |
| 1966 |
'struct declared with "...;", but the C calling convention may ' |
| 1967 |
'depend on the missing fields; or, it contains anonymous ' |
| 1968 |
'struct/unions. Such structs are only supported ' |
| 1969 |
'as return value if the function is \'API mode\' and non-variadic ' |
| 1970 |
'(i.e. declared inside ffibuilder.cdef()+ffibuilder.set_source() ' |
| 1971 |
'and not taking a final \'...\' argument)') |
| 1972 |
|
| 1973 |
def test_call_with_nested_anonymous_struct(self): |
| 1974 |
import sys |
| 1975 |
if sys.platform == 'win32': |
| 1976 |
skip("needs a GCC extension") |
| 1977 |
ffi, lib = self.prepare(""" |
| 1978 |
struct foo { int a; union { int b, c; }; }; |
| 1979 |
struct foo f(void); |
| 1980 |
struct foo g(int, ...); |
| 1981 |
""", "test_call_with_nested_anonymous_struct", """ |
| 1982 |
struct foo { int a; union { int b, c; }; }; |
| 1983 |
struct foo f(void) { |
| 1984 |
struct foo s = { 40 }; |
| 1985 |
s.b = 200; |
| 1986 |
return s; |
| 1987 |
} |
| 1988 |
struct foo g(int a, ...) { } |
| 1989 |
""") |
| 1990 |
assert lib.f().b == 200 |
| 1991 |
e = raises(NotImplementedError, lib.g, 0) |
| 1992 |
assert str(e.value) == ( |
| 1993 |
'ctype \'struct foo\' not supported as return value. It is a ' |
| 1994 |
'struct declared with "...;", but the C calling convention may ' |
| 1995 |
'depend on the missing fields; or, it contains anonymous ' |
| 1996 |
'struct/unions. Such structs are only supported ' |
| 1997 |
'as return value if the function is \'API mode\' and non-variadic ' |
| 1998 |
'(i.e. declared inside ffibuilder.cdef()+ffibuilder.set_source() ' |
| 1999 |
'and not taking a final \'...\' argument)') |
| 2000 |
|
| 2001 |
def test_call_with_bitfield(self): |
| 2002 |
ffi, lib = self.prepare(""" |
| 2003 |
struct foo { int x:5; }; |
| 2004 |
struct foo f(void); |
| 2005 |
struct foo g(int, ...); |
| 2006 |
""", "test_call_with_bitfield", """ |
| 2007 |
struct foo { int x:5; }; |
| 2008 |
struct foo f(void) { |
| 2009 |
struct foo s = { 11 }; |
| 2010 |
return s; |
| 2011 |
} |
| 2012 |
struct foo g(int a, ...) { } |
| 2013 |
""") |
| 2014 |
assert lib.f().x == 11 |
| 2015 |
e = raises(NotImplementedError, lib.g, 0) |
| 2016 |
assert str(e.value) == ( |
| 2017 |
"ctype 'struct foo' not supported as return value. It is a struct " |
| 2018 |
"with bit fields, which libffi does not support. Such structs are " |
| 2019 |
"only supported as return value if the function is 'API mode' and " |
| 2020 |
"non-variadic (i.e. declared inside ffibuilder.cdef()+ffibuilder." |
| 2021 |
"set_source() and not taking a final '...' argument)") |
| 2022 |
|
| 2023 |
def test_call_with_zero_length_field(self): |
| 2024 |
import sys |
| 2025 |
if sys.platform == 'win32': |
| 2026 |
skip("zero-length field not supported by MSVC") |
| 2027 |
ffi, lib = self.prepare(""" |
| 2028 |
struct foo { int a; int x[0]; }; |
| 2029 |
struct foo f(void); |
| 2030 |
struct foo g(int, ...); |
| 2031 |
""", "test_call_with_zero_length_field", """ |
| 2032 |
struct foo { int a; int x[0]; }; |
| 2033 |
struct foo f(void) { |
| 2034 |
struct foo s = { 42 }; |
| 2035 |
return s; |
| 2036 |
} |
| 2037 |
struct foo g(int a, ...) { } |
| 2038 |
""") |
| 2039 |
assert lib.f().a == 42 |
| 2040 |
e = raises(NotImplementedError, lib.g, 0) |
| 2041 |
assert str(e.value) == ( |
| 2042 |
"ctype 'struct foo' not supported as return value. It is a " |
| 2043 |
"struct with a zero-length array, which libffi does not support. " |
| 2044 |
"Such structs are only supported as return value if the function is " |
| 2045 |
"'API mode' and non-variadic (i.e. declared inside ffibuilder.cdef()" |
| 2046 |
"+ffibuilder.set_source() and not taking a final '...' argument)") |
| 2047 |
|
| 2048 |
def test_call_with_union(self): |
| 2049 |
ffi, lib = self.prepare(""" |
| 2050 |
union foo { int a; char b; }; |
| 2051 |
union foo f(void); |
| 2052 |
union foo g(int, ...); |
| 2053 |
""", "test_call_with_union", """ |
| 2054 |
union foo { int a; char b; }; |
| 2055 |
union foo f(void) { |
| 2056 |
union foo s = { 42 }; |
| 2057 |
return s; |
| 2058 |
} |
| 2059 |
union foo g(int a, ...) { } |
| 2060 |
""") |
| 2061 |
assert lib.f().a == 42 |
| 2062 |
e = raises(NotImplementedError, lib.g, 0) |
| 2063 |
assert str(e.value) == ( |
| 2064 |
"ctype 'union foo' not supported as return value by libffi. " |
| 2065 |
"Unions are only supported as return value if the function is " |
| 2066 |
"'API mode' and non-variadic (i.e. declared inside ffibuilder.cdef()" |
| 2067 |
"+ffibuilder.set_source() and not taking a final '...' argument)") |
| 2068 |
|
| 2069 |
def test_call_with_packed_struct(self): |
| 2070 |
import sys |
| 2071 |
if sys.platform == 'win32': |
| 2072 |
skip("needs a GCC extension") |
| 2073 |
ffi, lib = self.prepare(""" |
| 2074 |
struct foo { char y; int x; }; |
| 2075 |
struct foo f(void); |
| 2076 |
struct foo g(int, ...); |
| 2077 |
""", "test_call_with_packed_struct", """ |
| 2078 |
struct foo { char y; int x; } __attribute__((packed)); |
| 2079 |
struct foo f(void) { |
| 2080 |
struct foo s = { 40, 200 }; |
| 2081 |
return s; |
| 2082 |
} |
| 2083 |
struct foo g(int a, ...) { |
| 2084 |
struct foo s = { 41, 201 }; |
| 2085 |
return s; |
| 2086 |
} |
| 2087 |
""", packed=True, min_version=(1, 8, 3)) |
| 2088 |
assert ord(lib.f().y) == 40 |
| 2089 |
assert lib.f().x == 200 |
| 2090 |
e = raises(NotImplementedError, lib.g, 0) |
| 2091 |
assert str(e.value) == ( |
| 2092 |
"ctype 'struct foo' not supported as return value. It is a 'packed'" |
| 2093 |
" structure, with a different layout than expected by libffi. " |
| 2094 |
"Such structs are only supported as return value if the function is " |
| 2095 |
"'API mode' and non-variadic (i.e. declared inside ffibuilder.cdef()" |
| 2096 |
"+ffibuilder.set_source() and not taking a final '...' argument)") |
| 2097 |
|
| 2098 |
def test_gcc_visibility_hidden(self): |
| 2099 |
import sys |
| 2100 |
if sys.platform == 'win32': |
| 2101 |
skip("test for gcc/clang") |
| 2102 |
ffi, lib = self.prepare(""" |
| 2103 |
int f(int); |
| 2104 |
""", "test_gcc_visibility_hidden", """ |
| 2105 |
int f(int a) { return a + 40; } |
| 2106 |
""", extra_compile_args=['-fvisibility=hidden']) |
| 2107 |
assert lib.f(2) == 42 |
| 2108 |
|
| 2109 |
def test_override_default_definition(self): |
| 2110 |
ffi, lib = self.prepare(""" |
| 2111 |
typedef long int16_t, char16_t; |
| 2112 |
""", "test_override_default_definition", """ |
| 2113 |
""") |
| 2114 |
assert ffi.typeof("int16_t") is ffi.typeof("char16_t") is ffi.typeof("long") |
| 2115 |
|
| 2116 |
def test_char16_char32_plain_c(self): |
| 2117 |
ffi, lib = self.prepare(""" |
| 2118 |
char16_t foo_2bytes(char16_t); |
| 2119 |
char32_t foo_4bytes(char32_t); |
| 2120 |
""", "test_char16_char32_type_nocpp", """ |
| 2121 |
#if !defined(__cplusplus) || (!defined(_LIBCPP_VERSION) && __cplusplus < 201103L) |
| 2122 |
typedef uint_least16_t char16_t; |
| 2123 |
typedef uint_least32_t char32_t; |
| 2124 |
#endif |
| 2125 |
|
| 2126 |
char16_t foo_2bytes(char16_t a) { return (char16_t)(a + 42); } |
| 2127 |
char32_t foo_4bytes(char32_t a) { return (char32_t)(a + 42); } |
| 2128 |
""", min_version=(1, 11, 0)) |
| 2129 |
assert lib.foo_2bytes(u'\u1234') == u'\u125e' |
| 2130 |
assert lib.foo_4bytes(u'\u1234') == u'\u125e' |
| 2131 |
assert lib.foo_4bytes(u'\U00012345') == u'\U0001236f' |
| 2132 |
raises(TypeError, lib.foo_2bytes, u'\U00012345') |
| 2133 |
raises(TypeError, lib.foo_2bytes, 1234) |
| 2134 |
raises(TypeError, lib.foo_4bytes, 1234) |
| 2135 |
|
| 2136 |
def test_loader_spec(self): |
| 2137 |
import sys |
| 2138 |
ffi, lib = self.prepare("", "test_loader_spec", "") |
| 2139 |
if sys.version_info < (3,): |
| 2140 |
assert not hasattr(lib, '__loader__') |
| 2141 |
assert not hasattr(lib, '__spec__') |
| 2142 |
else: |
| 2143 |
assert lib.__loader__ is None |
| 2144 |
assert lib.__spec__ is None |
| 2145 |
|
| 2146 |
def test_release(self): |
| 2147 |
ffi, lib = self.prepare("", "test_release", "") |
| 2148 |
p = ffi.new("int[]", 123) |
| 2149 |
ffi.release(p) |
| 2150 |
|
| 2151 |
ffi.release(p) |
| 2152 |
|
| 2153 |
def test_release_new_allocator(self): |
| 2154 |
ffi, lib = self.prepare("struct ab { int a, b; };", |
| 2155 |
"test_release_new_allocator", |
| 2156 |
"struct ab { int a, b; };") |
| 2157 |
seen = [] |
| 2158 |
def myalloc(size): |
| 2159 |
seen.append(size) |
| 2160 |
return ffi.new("char[]", b"X" * size) |
| 2161 |
def myfree(raw): |
| 2162 |
seen.append(raw) |
| 2163 |
alloc2 = ffi.new_allocator(alloc=myalloc, free=myfree) |
| 2164 |
p = alloc2("int[]", 15) |
| 2165 |
assert seen == [15 * 4] |
| 2166 |
ffi.release(p) |
| 2167 |
assert seen == [15 * 4, p] |
| 2168 |
ffi.release(p) |
| 2169 |
assert seen == [15 * 4, p] |
| 2170 |
|
| 2171 |
del seen[:] |
| 2172 |
p = alloc2("struct ab *") |
| 2173 |
assert seen == [2 * 4] |
| 2174 |
ffi.release(p) |
| 2175 |
assert seen == [2 * 4, p] |
| 2176 |
ffi.release(p) |
| 2177 |
assert seen == [2 * 4, p] |
| 2178 |
|
| 2179 |
def test_struct_with_func_with_struct_arg(self): |
| 2180 |
ffi, lib = self.prepare("""struct BinaryTree { |
| 2181 |
int (* CompareKey)(struct BinaryTree tree); |
| 2182 |
};""", |
| 2183 |
"test_struct_with_func_with_struct_arg", """ |
| 2184 |
struct BinaryTree { |
| 2185 |
int (* CompareKey)(struct BinaryTree tree); |
| 2186 |
}; |
| 2187 |
""") |
| 2188 |
e = raises(RuntimeError, ffi.new, "struct BinaryTree *") |
| 2189 |
|
| 2190 |
|
| 2191 |
|
| 2192 |
|
| 2193 |
|
| 2194 |
|
| 2195 |
def test_call_function_offset_in_bytes(self): |
| 2196 |
from _cffi_backend import _offset_in_bytes |
| 2197 |
ffi, lib = self.prepare(""" |
| 2198 |
int foo(char* arg); |
| 2199 |
""", "test_call_function_offset_in_bytes", """ |
| 2200 |
int foo(char* arg) { |
| 2201 |
return(arg[0] * 10); |
| 2202 |
} |
| 2203 |
""") |
| 2204 |
assert lib.foo(_offset_in_bytes(b"foo", 0)) == ord(b"f") * 10 |
| 2205 |
assert lib.foo(_offset_in_bytes(b"foobxo", 4)) == ord(b"x") * 10 |