| 1 |
|
| 2 |
import os, sys, math |
| 3 |
import pytest |
| 4 |
from cffi import FFI, FFIError, VerificationError, VerificationMissing, model |
| 5 |
from cffi import CDefError |
| 6 |
from cffi import recompiler |
| 7 |
from extra_tests.cffi_tests.support import * |
| 8 |
from extra_tests.cffi_tests.support import _verify, extra_compile_args, is_musl |
| 9 |
import _cffi_backend |
| 10 |
|
| 11 |
lib_m = ['m'] |
| 12 |
if sys.platform == 'win32': |
| 13 |
|
| 14 |
import distutils.ccompiler |
| 15 |
if distutils.ccompiler.get_default_compiler() == 'msvc': |
| 16 |
lib_m = ['msvcrt'] |
| 17 |
|
| 18 |
class FFI(FFI): |
| 19 |
error = _cffi_backend.FFI.error |
| 20 |
_extra_compile_args = extra_compile_args |
| 21 |
_verify_counter = 0 |
| 22 |
|
| 23 |
def verify(self, preamble='', *args, **kwds): |
| 24 |
|
| 25 |
FFI._verify_counter += 1 |
| 26 |
module_name = 'verify%d' % FFI._verify_counter |
| 27 |
try: |
| 28 |
del self._assigned_source |
| 29 |
except AttributeError: |
| 30 |
pass |
| 31 |
self.set_source(module_name, preamble) |
| 32 |
return _verify(self, module_name, preamble, *args, |
| 33 |
extra_compile_args=self._extra_compile_args, **kwds) |
| 34 |
|
| 35 |
class FFI_warnings_not_error(FFI): |
| 36 |
_extra_compile_args = [] |
| 37 |
|
| 38 |
|
| 39 |
def test_missing_function(ffi=None): |
| 40 |
|
| 41 |
if ffi is None: |
| 42 |
ffi = FFI() |
| 43 |
ffi.cdef("void some_completely_unknown_function();") |
| 44 |
try: |
| 45 |
lib = ffi.verify() |
| 46 |
except (VerificationError, OSError, ImportError): |
| 47 |
pass |
| 48 |
else: |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
pass |
| 54 |
|
| 55 |
def test_missing_function_import_error(): |
| 56 |
|
| 57 |
test_missing_function(ffi=FFI_warnings_not_error()) |
| 58 |
|
| 59 |
def test_simple_case(): |
| 60 |
ffi = FFI() |
| 61 |
ffi.cdef("double sin(double x);") |
| 62 |
lib = ffi.verify('#include <math.h>', libraries=lib_m) |
| 63 |
assert lib.sin(1.23) == math.sin(1.23) |
| 64 |
|
| 65 |
def _Wconversion(cdef, source, **kargs): |
| 66 |
if sys.platform in ('win32', 'darwin'): |
| 67 |
pytest.skip("needs GCC") |
| 68 |
if '-Wno-error=sign-conversion' in extra_compile_args: |
| 69 |
pytest.skip("gcc 9.2.0 compiler bug exposed by Python 3.12+ prevents compilation with sign-conversion warnings-as-errors") |
| 70 |
ffi = FFI() |
| 71 |
ffi.cdef(cdef) |
| 72 |
pytest.raises(VerificationError, ffi.verify, source, **kargs) |
| 73 |
extra_compile_args_orig = extra_compile_args[:] |
| 74 |
extra_compile_args.remove('-Wconversion') |
| 75 |
try: |
| 76 |
lib = ffi.verify(source, **kargs) |
| 77 |
finally: |
| 78 |
extra_compile_args[:] = extra_compile_args_orig |
| 79 |
return lib |
| 80 |
|
| 81 |
def test_Wconversion_unsigned(): |
| 82 |
_Wconversion("unsigned foo(void);", |
| 83 |
"int foo(void) { return -1;}") |
| 84 |
|
| 85 |
def test_Wconversion_integer(): |
| 86 |
_Wconversion("short foo(void);", |
| 87 |
"long long foo(void) { return 1<<sizeof(short);}") |
| 88 |
|
| 89 |
def test_Wconversion_floating(): |
| 90 |
lib = _Wconversion("float sin(double);", |
| 91 |
"#include <math.h>", libraries=lib_m) |
| 92 |
res = lib.sin(1.23) |
| 93 |
assert res != math.sin(1.23) |
| 94 |
assert abs(res - math.sin(1.23)) < 1E-5 |
| 95 |
|
| 96 |
def test_Wconversion_float2int(): |
| 97 |
_Wconversion("int sinf(float);", |
| 98 |
"#include <math.h>", libraries=lib_m) |
| 99 |
|
| 100 |
def test_Wconversion_double2int(): |
| 101 |
_Wconversion("int sin(double);", |
| 102 |
"#include <math.h>", libraries=lib_m) |
| 103 |
|
| 104 |
def test_rounding_1(): |
| 105 |
ffi = FFI() |
| 106 |
ffi.cdef("double sinf(float x);") |
| 107 |
lib = ffi.verify('#include <math.h>', libraries=lib_m) |
| 108 |
res = lib.sinf(1.23) |
| 109 |
assert res != math.sin(1.23) |
| 110 |
assert abs(res - math.sin(1.23)) < 1E-5 |
| 111 |
|
| 112 |
def test_rounding_2(): |
| 113 |
ffi = FFI() |
| 114 |
ffi.cdef("double sin(float x);") |
| 115 |
lib = ffi.verify('#include <math.h>', libraries=lib_m) |
| 116 |
res = lib.sin(1.23) |
| 117 |
assert res != math.sin(1.23) |
| 118 |
assert abs(res - math.sin(1.23)) < 1E-5 |
| 119 |
|
| 120 |
def test_strlen_exact(): |
| 121 |
ffi = FFI() |
| 122 |
ffi.cdef("size_t strlen(const char *s);") |
| 123 |
lib = ffi.verify("#include <string.h>") |
| 124 |
assert lib.strlen(b"hi there!") == 9 |
| 125 |
|
| 126 |
def test_strlen_approximate(): |
| 127 |
lib = _Wconversion("int strlen(char *s);", |
| 128 |
"#include <string.h>") |
| 129 |
assert lib.strlen(b"hi there!") == 9 |
| 130 |
|
| 131 |
def test_return_approximate(): |
| 132 |
for typename in ['short', 'int', 'long', 'long long']: |
| 133 |
ffi = FFI() |
| 134 |
ffi.cdef("%s foo(signed char x);" % typename) |
| 135 |
lib = ffi.verify("signed char foo(signed char x) { return x;}") |
| 136 |
assert lib.foo(-128) == -128 |
| 137 |
assert lib.foo(+127) == +127 |
| 138 |
|
| 139 |
def test_strlen_array_of_char(): |
| 140 |
ffi = FFI() |
| 141 |
ffi.cdef("size_t strlen(char[]);") |
| 142 |
lib = ffi.verify("#include <string.h>") |
| 143 |
assert lib.strlen(b"hello") == 5 |
| 144 |
|
| 145 |
def test_longdouble(): |
| 146 |
ffi = FFI() |
| 147 |
ffi.cdef("long double sinl(long double x);") |
| 148 |
lib = ffi.verify('#include <math.h>', libraries=lib_m) |
| 149 |
for input in [1.23, |
| 150 |
ffi.cast("double", 1.23), |
| 151 |
ffi.cast("long double", 1.23)]: |
| 152 |
x = lib.sinl(input) |
| 153 |
assert repr(x).startswith("<cdata 'long double'") |
| 154 |
assert (float(x) - math.sin(1.23)) < 1E-10 |
| 155 |
|
| 156 |
def test_longdouble_precision(): |
| 157 |
|
| 158 |
|
| 159 |
ffi = FFI() |
| 160 |
ffi.cdef("long double step1(long double x);") |
| 161 |
SAME_SIZE = ffi.sizeof("long double") == ffi.sizeof("double") |
| 162 |
lib = ffi.verify(""" |
| 163 |
long double step1(long double x) |
| 164 |
{ |
| 165 |
return 4*x-x*x; |
| 166 |
} |
| 167 |
""") |
| 168 |
def do(cast_to_double): |
| 169 |
x = 0.9789 |
| 170 |
for i in range(10000): |
| 171 |
x = lib.step1(x) |
| 172 |
if cast_to_double: |
| 173 |
x = float(x) |
| 174 |
return float(x) |
| 175 |
|
| 176 |
more_precise = do(False) |
| 177 |
less_precise = do(True) |
| 178 |
if SAME_SIZE: |
| 179 |
assert more_precise == less_precise |
| 180 |
else: |
| 181 |
assert abs(more_precise - less_precise) > 0.1 |
| 182 |
|
| 183 |
import platform |
| 184 |
if (platform.machine().startswith('i386') or |
| 185 |
platform.machine().startswith('i486') or |
| 186 |
platform.machine().startswith('i586') or |
| 187 |
platform.machine().startswith('i686') or |
| 188 |
platform.machine().startswith('x86')): |
| 189 |
assert abs(more_precise - 0.656769) < 0.001 |
| 190 |
assert abs(less_precise - 3.99091) < 0.001 |
| 191 |
else: |
| 192 |
pytest.skip("don't know the very exact precision of 'long double'") |
| 193 |
|
| 194 |
|
| 195 |
all_primitive_types = model.PrimitiveType.ALL_PRIMITIVE_TYPES |
| 196 |
if sys.platform == 'win32': |
| 197 |
all_primitive_types = all_primitive_types.copy() |
| 198 |
del all_primitive_types['ssize_t'] |
| 199 |
all_integer_types = sorted(tp for tp in all_primitive_types |
| 200 |
if all_primitive_types[tp] == 'i') |
| 201 |
all_float_types = sorted(tp for tp in all_primitive_types |
| 202 |
if all_primitive_types[tp] == 'f') |
| 203 |
|
| 204 |
def all_signed_integer_types(ffi): |
| 205 |
return [x for x in all_integer_types if int(ffi.cast(x, -1)) < 0] |
| 206 |
|
| 207 |
def all_unsigned_integer_types(ffi): |
| 208 |
return [x for x in all_integer_types if int(ffi.cast(x, -1)) > 0] |
| 209 |
|
| 210 |
|
| 211 |
def test_primitive_category(): |
| 212 |
for typename in all_primitive_types: |
| 213 |
tp = model.PrimitiveType(typename) |
| 214 |
C = tp.is_char_type() |
| 215 |
F = tp.is_float_type() |
| 216 |
X = tp.is_complex_type() |
| 217 |
I = tp.is_integer_type() |
| 218 |
assert C == (typename in ('char', 'wchar_t', 'char16_t', 'char32_t')) |
| 219 |
assert F == (typename in ('float', 'double', 'long double')) |
| 220 |
assert X == (typename in ('_cffi_float_complex_t', '_cffi_double_complex_t')) |
| 221 |
assert I + F + C + X == 1 |
| 222 |
|
| 223 |
def test_all_integer_and_float_types(): |
| 224 |
typenames = [] |
| 225 |
for typename in all_primitive_types: |
| 226 |
if (all_primitive_types[typename] == 'c' or |
| 227 |
all_primitive_types[typename] == 'j' or |
| 228 |
typename == '_Bool' or typename == 'long double'): |
| 229 |
pass |
| 230 |
else: |
| 231 |
typenames.append(typename) |
| 232 |
|
| 233 |
ffi = FFI() |
| 234 |
ffi.cdef('\n'.join(["%s foo_%s(%s);" % (tp, tp.replace(' ', '_'), tp) |
| 235 |
for tp in typenames])) |
| 236 |
lib = ffi.verify('\n'.join(["%s foo_%s(%s x) { return (%s)(x+1); }" % |
| 237 |
(tp, tp.replace(' ', '_'), tp, tp) |
| 238 |
for tp in typenames])) |
| 239 |
for typename in typenames: |
| 240 |
foo = getattr(lib, 'foo_%s' % typename.replace(' ', '_')) |
| 241 |
assert foo(42) == 43 |
| 242 |
if sys.version < '3': |
| 243 |
assert foo(long(44)) == 45 |
| 244 |
assert foo(ffi.cast(typename, 46)) == 47 |
| 245 |
pytest.raises(TypeError, foo, ffi.NULL) |
| 246 |
|
| 247 |
|
| 248 |
if all_primitive_types[typename] == 'f': |
| 249 |
continue |
| 250 |
for value in [-2**80, -2**40, -2**20, -2**10, -2**5, -1, |
| 251 |
2**5, 2**10, 2**20, 2**40, 2**80]: |
| 252 |
overflows = int(ffi.cast(typename, value)) != value |
| 253 |
if overflows: |
| 254 |
pytest.raises(OverflowError, foo, value) |
| 255 |
else: |
| 256 |
assert foo(value) == value + 1 |
| 257 |
|
| 258 |
def test_all_complex_types(): |
| 259 |
if sys.platform == 'win32': |
| 260 |
typenames = ['_Fcomplex', '_Dcomplex'] |
| 261 |
header = '#include <complex.h>\n' |
| 262 |
else: |
| 263 |
typenames = ['float _Complex', 'double _Complex'] |
| 264 |
header = '' |
| 265 |
|
| 266 |
ffi = FFI() |
| 267 |
ffi.cdef('\n'.join(["%s foo_%s(%s);" % (tp, tp.replace(' ', '_'), tp) |
| 268 |
for tp in typenames])) |
| 269 |
lib = ffi.verify( |
| 270 |
header + '\n'.join(["%s foo_%s(%s x) { return x; }" % |
| 271 |
(tp, tp.replace(' ', '_'), tp) |
| 272 |
for tp in typenames])) |
| 273 |
for typename in typenames: |
| 274 |
foo = getattr(lib, 'foo_%s' % typename.replace(' ', '_')) |
| 275 |
assert foo(42 + 1j) == 42 + 1j |
| 276 |
assert foo(ffi.cast(typename, 46 - 3j)) == 46 - 3j |
| 277 |
pytest.raises(TypeError, foo, ffi.NULL) |
| 278 |
|
| 279 |
def test_var_signed_integer_types(): |
| 280 |
ffi = FFI() |
| 281 |
lst = all_signed_integer_types(ffi) |
| 282 |
csource = "\n".join(["static %s somevar_%s;" % (tp, tp.replace(' ', '_')) |
| 283 |
for tp in lst]) |
| 284 |
ffi.cdef(csource) |
| 285 |
lib = ffi.verify(csource) |
| 286 |
for tp in lst: |
| 287 |
varname = 'somevar_%s' % tp.replace(' ', '_') |
| 288 |
sz = ffi.sizeof(tp) |
| 289 |
max = (1 << (8*sz-1)) - 1 |
| 290 |
min = -(1 << (8*sz-1)) |
| 291 |
setattr(lib, varname, max) |
| 292 |
assert getattr(lib, varname) == max |
| 293 |
setattr(lib, varname, min) |
| 294 |
assert getattr(lib, varname) == min |
| 295 |
pytest.raises(OverflowError, setattr, lib, varname, max+1) |
| 296 |
pytest.raises(OverflowError, setattr, lib, varname, min-1) |
| 297 |
|
| 298 |
def test_var_unsigned_integer_types(): |
| 299 |
ffi = FFI() |
| 300 |
lst = all_unsigned_integer_types(ffi) |
| 301 |
csource = "\n".join(["static %s somevar_%s;" % (tp, tp.replace(' ', '_')) |
| 302 |
for tp in lst]) |
| 303 |
ffi.cdef(csource) |
| 304 |
lib = ffi.verify(csource) |
| 305 |
for tp in lst: |
| 306 |
varname = 'somevar_%s' % tp.replace(' ', '_') |
| 307 |
sz = ffi.sizeof(tp) |
| 308 |
if tp != '_Bool': |
| 309 |
max = (1 << (8*sz)) - 1 |
| 310 |
else: |
| 311 |
max = 1 |
| 312 |
setattr(lib, varname, max) |
| 313 |
assert getattr(lib, varname) == max |
| 314 |
setattr(lib, varname, 0) |
| 315 |
assert getattr(lib, varname) == 0 |
| 316 |
pytest.raises(OverflowError, setattr, lib, varname, max+1) |
| 317 |
pytest.raises(OverflowError, setattr, lib, varname, -1) |
| 318 |
|
| 319 |
def test_fn_signed_integer_types(): |
| 320 |
ffi = FFI() |
| 321 |
lst = all_signed_integer_types(ffi) |
| 322 |
cdefsrc = "\n".join(["%s somefn_%s(%s);" % (tp, tp.replace(' ', '_'), tp) |
| 323 |
for tp in lst]) |
| 324 |
ffi.cdef(cdefsrc) |
| 325 |
verifysrc = "\n".join(["%s somefn_%s(%s x) { return x; }" % |
| 326 |
(tp, tp.replace(' ', '_'), tp) for tp in lst]) |
| 327 |
lib = ffi.verify(verifysrc) |
| 328 |
for tp in lst: |
| 329 |
fnname = 'somefn_%s' % tp.replace(' ', '_') |
| 330 |
sz = ffi.sizeof(tp) |
| 331 |
max = (1 << (8*sz-1)) - 1 |
| 332 |
min = -(1 << (8*sz-1)) |
| 333 |
fn = getattr(lib, fnname) |
| 334 |
assert fn(max) == max |
| 335 |
assert fn(min) == min |
| 336 |
pytest.raises(OverflowError, fn, max + 1) |
| 337 |
pytest.raises(OverflowError, fn, min - 1) |
| 338 |
|
| 339 |
def test_fn_unsigned_integer_types(): |
| 340 |
ffi = FFI() |
| 341 |
lst = all_unsigned_integer_types(ffi) |
| 342 |
cdefsrc = "\n".join(["%s somefn_%s(%s);" % (tp, tp.replace(' ', '_'), tp) |
| 343 |
for tp in lst]) |
| 344 |
ffi.cdef(cdefsrc) |
| 345 |
verifysrc = "\n".join(["%s somefn_%s(%s x) { return x; }" % |
| 346 |
(tp, tp.replace(' ', '_'), tp) for tp in lst]) |
| 347 |
lib = ffi.verify(verifysrc) |
| 348 |
for tp in lst: |
| 349 |
fnname = 'somefn_%s' % tp.replace(' ', '_') |
| 350 |
sz = ffi.sizeof(tp) |
| 351 |
if tp != '_Bool': |
| 352 |
max = (1 << (8*sz)) - 1 |
| 353 |
else: |
| 354 |
max = 1 |
| 355 |
fn = getattr(lib, fnname) |
| 356 |
assert fn(max) == max |
| 357 |
assert fn(0) == 0 |
| 358 |
pytest.raises(OverflowError, fn, max + 1) |
| 359 |
pytest.raises(OverflowError, fn, -1) |
| 360 |
|
| 361 |
def test_char_type(): |
| 362 |
ffi = FFI() |
| 363 |
ffi.cdef("char foo(char);") |
| 364 |
lib = ffi.verify("char foo(char x) { return ++x; }") |
| 365 |
assert lib.foo(b"A") == b"B" |
| 366 |
pytest.raises(TypeError, lib.foo, b"bar") |
| 367 |
pytest.raises(TypeError, lib.foo, "bar") |
| 368 |
|
| 369 |
def test_wchar_type(): |
| 370 |
ffi = FFI() |
| 371 |
if ffi.sizeof('wchar_t') == 2: |
| 372 |
uniexample1 = u+'\u1234' |
| 373 |
uniexample2 = u+'\u1235' |
| 374 |
else: |
| 375 |
uniexample1 = u+'\U00012345' |
| 376 |
uniexample2 = u+'\U00012346' |
| 377 |
|
| 378 |
ffi.cdef("wchar_t foo(wchar_t);") |
| 379 |
lib = ffi.verify("wchar_t foo(wchar_t x) { return x+1; }") |
| 380 |
assert lib.foo(uniexample1) == uniexample2 |
| 381 |
|
| 382 |
def test_no_argument(): |
| 383 |
ffi = FFI() |
| 384 |
ffi.cdef("int foo(void);") |
| 385 |
lib = ffi.verify("int foo(void) { return 42; }") |
| 386 |
assert lib.foo() == 42 |
| 387 |
|
| 388 |
def test_two_arguments(): |
| 389 |
ffi = FFI() |
| 390 |
ffi.cdef("int foo(int, int);") |
| 391 |
lib = ffi.verify("int foo(int a, int b) { return a - b; }") |
| 392 |
assert lib.foo(40, -2) == 42 |
| 393 |
|
| 394 |
def test_macro(): |
| 395 |
ffi = FFI() |
| 396 |
ffi.cdef("int foo(int, int);") |
| 397 |
lib = ffi.verify("#define foo(a, b) ((a) * (b))") |
| 398 |
assert lib.foo(-6, -7) == 42 |
| 399 |
|
| 400 |
def test_ptr(): |
| 401 |
ffi = FFI() |
| 402 |
ffi.cdef("int *foo(int *);") |
| 403 |
lib = ffi.verify("int *foo(int *a) { return a; }") |
| 404 |
assert lib.foo(ffi.NULL) == ffi.NULL |
| 405 |
p = ffi.new("int *", 42) |
| 406 |
q = ffi.new("int *", 42) |
| 407 |
assert lib.foo(p) == p |
| 408 |
assert lib.foo(q) != p |
| 409 |
|
| 410 |
def test_bogus_ptr(): |
| 411 |
ffi = FFI() |
| 412 |
ffi.cdef("int *foo(int *);") |
| 413 |
lib = ffi.verify("int *foo(int *a) { return a; }") |
| 414 |
pytest.raises(TypeError, lib.foo, ffi.new("short *", 42)) |
| 415 |
|
| 416 |
|
| 417 |
def test_verify_typedefs(): |
| 418 |
pytest.skip("ignored so far") |
| 419 |
types = ['signed char', 'unsigned char', 'int', 'long'] |
| 420 |
for cdefed in types: |
| 421 |
for real in types: |
| 422 |
ffi = FFI() |
| 423 |
ffi.cdef("typedef %s foo_t;" % cdefed) |
| 424 |
if cdefed == real: |
| 425 |
ffi.verify("typedef %s foo_t;" % real) |
| 426 |
else: |
| 427 |
pytest.raises(VerificationError, ffi.verify, |
| 428 |
"typedef %s foo_t;" % real) |
| 429 |
|
| 430 |
def test_nondecl_struct(): |
| 431 |
ffi = FFI() |
| 432 |
ffi.cdef("typedef struct foo_s foo_t; int bar(foo_t *);") |
| 433 |
lib = ffi.verify("typedef struct foo_s foo_t;\n" |
| 434 |
"int bar(foo_t *f) { (void)f; return 42; }\n") |
| 435 |
assert lib.bar(ffi.NULL) == 42 |
| 436 |
|
| 437 |
def test_ffi_full_struct(): |
| 438 |
def check(verified_code): |
| 439 |
ffi = FFI() |
| 440 |
ffi.cdef("struct foo_s { char x; int y; long *z; };") |
| 441 |
ffi.verify(verified_code) |
| 442 |
ffi.new("struct foo_s *", {}) |
| 443 |
|
| 444 |
check("struct foo_s { char x; int y; long *z; };") |
| 445 |
|
| 446 |
if sys.platform != 'win32': |
| 447 |
pytest.raises(VerificationError, check, |
| 448 |
"struct foo_s { char x; int y; int *z; };") |
| 449 |
|
| 450 |
pytest.raises(VerificationError, check, |
| 451 |
"struct foo_s { int y; long *z; };") |
| 452 |
|
| 453 |
e = pytest.raises(FFI.error, check, |
| 454 |
"struct foo_s { int y; char x; long *z; };") |
| 455 |
assert str(e.value).startswith( |
| 456 |
"struct foo_s: wrong offset for field 'x'" |
| 457 |
" (cdef says 0, but C compiler says 4)") |
| 458 |
|
| 459 |
e = pytest.raises(FFI.error, check, |
| 460 |
"struct foo_s { char x; int y; long *z; char extra; };") |
| 461 |
assert str(e.value).startswith( |
| 462 |
"struct foo_s: wrong total size" |
| 463 |
" (cdef says %d, but C compiler says %d)" % ( |
| 464 |
8 + FFI().sizeof('long *'), |
| 465 |
8 + FFI().sizeof('long *') * 2)) |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
check("struct foo_s { char x, extra; int y; long *z; };") |
| 471 |
|
| 472 |
e = pytest.raises(FFI.error, check, |
| 473 |
"struct foo_s { char x; short pad; short y; long *z; };") |
| 474 |
assert str(e.value).startswith( |
| 475 |
"struct foo_s: wrong size for field 'y'" |
| 476 |
" (cdef says 4, but C compiler says 2)") |
| 477 |
|
| 478 |
def test_ffi_nonfull_struct(): |
| 479 |
ffi = FFI() |
| 480 |
ffi.cdef(""" |
| 481 |
struct foo_s { |
| 482 |
int x; |
| 483 |
...; |
| 484 |
}; |
| 485 |
""") |
| 486 |
pytest.raises(VerificationMissing, ffi.sizeof, 'struct foo_s') |
| 487 |
pytest.raises(VerificationMissing, ffi.offsetof, 'struct foo_s', 'x') |
| 488 |
pytest.raises(VerificationMissing, ffi.new, 'struct foo_s *') |
| 489 |
ffi.verify(""" |
| 490 |
struct foo_s { |
| 491 |
int a, b, x, c, d, e; |
| 492 |
}; |
| 493 |
""") |
| 494 |
assert ffi.sizeof('struct foo_s') == 6 * ffi.sizeof('int') |
| 495 |
assert ffi.offsetof('struct foo_s', 'x') == 2 * ffi.sizeof('int') |
| 496 |
|
| 497 |
def test_ffi_nonfull_alignment(): |
| 498 |
ffi = FFI() |
| 499 |
ffi.cdef("struct foo_s { char x; ...; };") |
| 500 |
ffi.verify("struct foo_s { int a, b; char x; };") |
| 501 |
assert ffi.sizeof('struct foo_s') == 3 * ffi.sizeof('int') |
| 502 |
assert ffi.alignof('struct foo_s') == ffi.sizeof('int') |
| 503 |
|
| 504 |
def _check_field_match(typename, real, expect_mismatch): |
| 505 |
ffi = FFI() |
| 506 |
testing_by_size = (expect_mismatch == 'by_size') |
| 507 |
if testing_by_size: |
| 508 |
expect_mismatch = ffi.sizeof(typename) != ffi.sizeof(real) |
| 509 |
ffi.cdef("struct foo_s { %s x; ...; };" % typename) |
| 510 |
try: |
| 511 |
ffi.verify("struct foo_s { %s x; };" % real) |
| 512 |
ffi.new("struct foo_s *", []) |
| 513 |
except (VerificationError, ffi.error): |
| 514 |
if not expect_mismatch: |
| 515 |
if testing_by_size and typename != real: |
| 516 |
print("ignoring mismatch between %s* and %s* even though " |
| 517 |
"they have the same size" % (typename, real)) |
| 518 |
return |
| 519 |
raise AssertionError("unexpected mismatch: %s should be accepted " |
| 520 |
"as equal to %s" % (typename, real)) |
| 521 |
else: |
| 522 |
if expect_mismatch: |
| 523 |
raise AssertionError("mismatch not detected: " |
| 524 |
"%s != %s" % (typename, real)) |
| 525 |
|
| 526 |
def test_struct_bad_sized_integer(): |
| 527 |
for typename in ['int8_t', 'int16_t', 'int32_t', 'int64_t']: |
| 528 |
for real in ['int8_t', 'int16_t', 'int32_t', 'int64_t']: |
| 529 |
_check_field_match(typename, real, "by_size") |
| 530 |
|
| 531 |
def test_struct_bad_sized_float(): |
| 532 |
for typename in all_float_types: |
| 533 |
for real in all_float_types: |
| 534 |
_check_field_match(typename, real, "by_size") |
| 535 |
|
| 536 |
def test_struct_signedness_ignored(): |
| 537 |
_check_field_match("int", "unsigned int", expect_mismatch=False) |
| 538 |
_check_field_match("unsigned short", "signed short", expect_mismatch=False) |
| 539 |
|
| 540 |
def test_struct_float_vs_int(): |
| 541 |
if sys.platform == 'win32': |
| 542 |
pytest.skip("XXX fixme: only gives warnings") |
| 543 |
ffi = FFI() |
| 544 |
for typename in all_signed_integer_types(ffi): |
| 545 |
for real in all_float_types: |
| 546 |
_check_field_match(typename, real, expect_mismatch=True) |
| 547 |
for typename in all_float_types: |
| 548 |
for real in all_signed_integer_types(ffi): |
| 549 |
_check_field_match(typename, real, expect_mismatch=True) |
| 550 |
|
| 551 |
def test_struct_array_field(): |
| 552 |
ffi = FFI() |
| 553 |
ffi.cdef("struct foo_s { int a[17]; ...; };") |
| 554 |
ffi.verify("struct foo_s { int x; int a[17]; int y; };") |
| 555 |
assert ffi.sizeof('struct foo_s') == 19 * ffi.sizeof('int') |
| 556 |
s = ffi.new("struct foo_s *") |
| 557 |
assert ffi.sizeof(s.a) == 17 * ffi.sizeof('int') |
| 558 |
|
| 559 |
def test_struct_array_no_length(): |
| 560 |
ffi = FFI() |
| 561 |
ffi.cdef("struct foo_s { int a[]; int y; ...; };\n" |
| 562 |
"int bar(struct foo_s *);\n") |
| 563 |
lib = ffi.verify("struct foo_s { int x; int a[17]; int y; };\n" |
| 564 |
"int bar(struct foo_s *f) { return f->a[14]; }\n") |
| 565 |
assert ffi.sizeof('struct foo_s') == 19 * ffi.sizeof('int') |
| 566 |
s = ffi.new("struct foo_s *") |
| 567 |
assert ffi.typeof(s.a) is ffi.typeof('int[]') |
| 568 |
assert len(s.a) == 18 |
| 569 |
s.a[14] = 4242 |
| 570 |
assert lib.bar(s) == 4242 |
| 571 |
|
| 572 |
s.a[17] = -521 |
| 573 |
assert s.y == s.a[17] == -521 |
| 574 |
|
| 575 |
s = ffi.new("struct foo_s *", {'a': list(range(17))}) |
| 576 |
assert s.a[16] == 16 |
| 577 |
|
| 578 |
s = ffi.new("struct foo_s *", {'a': list(range(18))}) |
| 579 |
assert s.y == s.a[17] == 17 |
| 580 |
|
| 581 |
def test_struct_array_guess_length(): |
| 582 |
ffi = FFI() |
| 583 |
ffi.cdef("struct foo_s { int a[...]; };") |
| 584 |
ffi.verify("struct foo_s { int x; int a[17]; int y; };") |
| 585 |
assert ffi.sizeof('struct foo_s') == 19 * ffi.sizeof('int') |
| 586 |
s = ffi.new("struct foo_s *") |
| 587 |
assert ffi.sizeof(s.a) == 17 * ffi.sizeof('int') |
| 588 |
with pytest.raises(IndexError): |
| 589 |
s.a[17] |
| 590 |
|
| 591 |
def test_struct_array_c99_1(): |
| 592 |
if sys.platform == 'win32': |
| 593 |
pytest.skip("requires C99") |
| 594 |
ffi = FFI() |
| 595 |
ffi.cdef("struct foo_s { int x; int a[]; };") |
| 596 |
ffi.verify("struct foo_s { int x; int a[]; };") |
| 597 |
assert ffi.sizeof('struct foo_s') == 1 * ffi.sizeof('int') |
| 598 |
s = ffi.new("struct foo_s *", [424242, 4]) |
| 599 |
assert ffi.sizeof(ffi.typeof(s[0])) == 1 * ffi.sizeof('int') |
| 600 |
assert ffi.sizeof(s[0]) == 5 * ffi.sizeof('int') |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
assert s.a[3] == 0 |
| 606 |
s = ffi.new("struct foo_s *", [424242, [-40, -30, -20, -10]]) |
| 607 |
assert ffi.sizeof(s[0]) == 5 * ffi.sizeof('int') |
| 608 |
assert s.a[3] == -10 |
| 609 |
s = ffi.new("struct foo_s *") |
| 610 |
assert ffi.sizeof(s[0]) == 1 * ffi.sizeof('int') |
| 611 |
s = ffi.new("struct foo_s *", [424242]) |
| 612 |
assert ffi.sizeof(s[0]) == 1 * ffi.sizeof('int') |
| 613 |
|
| 614 |
def test_struct_array_c99_2(): |
| 615 |
if sys.platform == 'win32': |
| 616 |
pytest.skip("requires C99") |
| 617 |
ffi = FFI() |
| 618 |
ffi.cdef("struct foo_s { int x; int a[]; ...; };") |
| 619 |
ffi.verify("struct foo_s { int x, y; int a[]; };") |
| 620 |
assert ffi.sizeof('struct foo_s') == 2 * ffi.sizeof('int') |
| 621 |
s = ffi.new("struct foo_s *", [424242, 4]) |
| 622 |
assert ffi.sizeof(s[0]) == 6 * ffi.sizeof('int') |
| 623 |
assert s.a[3] == 0 |
| 624 |
s = ffi.new("struct foo_s *", [424242, [-40, -30, -20, -10]]) |
| 625 |
assert ffi.sizeof(s[0]) == 6 * ffi.sizeof('int') |
| 626 |
assert s.a[3] == -10 |
| 627 |
s = ffi.new("struct foo_s *") |
| 628 |
assert ffi.sizeof(s[0]) == 2 * ffi.sizeof('int') |
| 629 |
s = ffi.new("struct foo_s *", [424242]) |
| 630 |
assert ffi.sizeof(s[0]) == 2 * ffi.sizeof('int') |
| 631 |
|
| 632 |
def test_struct_ptr_to_array_field(): |
| 633 |
ffi = FFI() |
| 634 |
ffi.cdef("struct foo_s { int (*a)[17]; ...; }; struct bar_s { ...; };") |
| 635 |
ffi.verify("struct foo_s { int x; int (*a)[17]; int y; };\n" |
| 636 |
"struct bar_s { int x; int *a; int y; };") |
| 637 |
assert ffi.sizeof('struct foo_s') == ffi.sizeof("struct bar_s") |
| 638 |
s = ffi.new("struct foo_s *") |
| 639 |
assert ffi.sizeof(s.a) == ffi.sizeof('int(*)[17]') == ffi.sizeof("int *") |
| 640 |
|
| 641 |
def test_struct_with_bitfield_exact(): |
| 642 |
ffi = FFI() |
| 643 |
ffi.cdef("struct foo_s { int a:2, b:3; };") |
| 644 |
ffi.verify("struct foo_s { int a:2, b:3; };") |
| 645 |
s = ffi.new("struct foo_s *") |
| 646 |
s.b = 3 |
| 647 |
with pytest.raises(OverflowError): |
| 648 |
s.b = 4 |
| 649 |
assert s.b == 3 |
| 650 |
|
| 651 |
def test_struct_with_bitfield_enum(): |
| 652 |
ffi = FFI() |
| 653 |
code = """ |
| 654 |
typedef enum { AA, BB, CC } foo_e; |
| 655 |
typedef struct { foo_e f:2; } foo_s; |
| 656 |
""" |
| 657 |
ffi.cdef(code) |
| 658 |
ffi.verify(code) |
| 659 |
s = ffi.new("foo_s *") |
| 660 |
s.f = 1 |
| 661 |
assert s.f == 1 |
| 662 |
if int(ffi.cast("foo_e", -1)) < 0: |
| 663 |
two = -2 |
| 664 |
else: |
| 665 |
two = 2 |
| 666 |
s.f = two |
| 667 |
assert s.f == two |
| 668 |
|
| 669 |
def test_unsupported_struct_with_bitfield_ellipsis(): |
| 670 |
ffi = FFI() |
| 671 |
pytest.raises(NotImplementedError, ffi.cdef, |
| 672 |
"struct foo_s { int a:2, b:3; ...; };") |
| 673 |
|
| 674 |
def test_global_constants(): |
| 675 |
ffi = FFI() |
| 676 |
|
| 677 |
|
| 678 |
ffi.cdef("static const int AA, BB, CC, DD;") |
| 679 |
lib = ffi.verify("#define AA 42\n" |
| 680 |
"#define BB (-43) // blah\n" |
| 681 |
"#define CC (22*2) /* foobar */\n" |
| 682 |
"#define DD ((unsigned int)142) /* foo\nbar */\n") |
| 683 |
assert lib.AA == 42 |
| 684 |
assert lib.BB == -43 |
| 685 |
assert lib.CC == 44 |
| 686 |
assert lib.DD == 142 |
| 687 |
|
| 688 |
def test_global_const_int_size(): |
| 689 |
|
| 690 |
for value in [-2**63, -2**31, -2**15, |
| 691 |
2**15-1, 2**15, 2**31-1, 2**31, 2**32-1, 2**32, |
| 692 |
2**63-1, 2**63, 2**64-1]: |
| 693 |
ffi = FFI() |
| 694 |
if value == int(ffi.cast("long long", value)): |
| 695 |
if value < 0: |
| 696 |
vstr = '(-%dLL-1)' % (~value,) |
| 697 |
else: |
| 698 |
vstr = '%dLL' % value |
| 699 |
elif value == int(ffi.cast("unsigned long long", value)): |
| 700 |
vstr = '%dULL' % value |
| 701 |
else: |
| 702 |
raise AssertionError(value) |
| 703 |
ffi.cdef("static const unsigned short AA;") |
| 704 |
lib = ffi.verify("#define AA %s\n" % vstr) |
| 705 |
assert lib.AA == value |
| 706 |
assert type(lib.AA) is type(int(lib.AA)) |
| 707 |
|
| 708 |
def test_global_constants_non_int(): |
| 709 |
ffi = FFI() |
| 710 |
ffi.cdef("static char *const PP;") |
| 711 |
lib = ffi.verify('static char *const PP = "testing!";\n') |
| 712 |
assert ffi.typeof(lib.PP) == ffi.typeof("char *") |
| 713 |
assert ffi.string(lib.PP) == b"testing!" |
| 714 |
|
| 715 |
def test_nonfull_enum(): |
| 716 |
ffi = FFI() |
| 717 |
ffi.cdef("enum ee { EE1, EE2, EE3, ... \n \t };") |
| 718 |
pytest.raises(VerificationMissing, ffi.cast, 'enum ee', 'EE2') |
| 719 |
ffi.verify("enum ee { EE1=10, EE2, EE3=-10, EE4 };") |
| 720 |
assert ffi.string(ffi.cast('enum ee', 11)) == "EE2" |
| 721 |
assert ffi.string(ffi.cast('enum ee', -10)) == "EE3" |
| 722 |
|
| 723 |
assert ffi.typeof("enum ee").relements == {'EE1': 10, 'EE2': 11, 'EE3': -10} |
| 724 |
assert ffi.typeof("enum ee").elements == {10: 'EE1', 11: 'EE2', -10: 'EE3'} |
| 725 |
|
| 726 |
def test_full_enum(): |
| 727 |
ffi = FFI() |
| 728 |
ffi.cdef("enum ee { EE1, EE2, EE3 };") |
| 729 |
lib = ffi.verify("enum ee { EE1, EE2, EE3 };") |
| 730 |
assert [lib.EE1, lib.EE2, lib.EE3] == [0, 1, 2] |
| 731 |
|
| 732 |
def test_enum_usage(): |
| 733 |
ffi = FFI() |
| 734 |
ffi.cdef("enum ee { EE1,EE2 }; typedef struct { enum ee x; } *sp;") |
| 735 |
lib = ffi.verify("enum ee { EE1,EE2 }; typedef struct { enum ee x; } *sp;") |
| 736 |
assert lib.EE2 == 1 |
| 737 |
s = ffi.new("sp", [lib.EE2]) |
| 738 |
assert s.x == 1 |
| 739 |
s.x = 17 |
| 740 |
assert s.x == 17 |
| 741 |
|
| 742 |
def test_anonymous_enum(): |
| 743 |
ffi = FFI() |
| 744 |
ffi.cdef("enum { EE1 }; enum { EE2, EE3 };") |
| 745 |
lib = ffi.verify("enum { EE1 }; enum { EE2, EE3 };") |
| 746 |
assert lib.EE1 == 0 |
| 747 |
assert lib.EE2 == 0 |
| 748 |
assert lib.EE3 == 1 |
| 749 |
|
| 750 |
def test_nonfull_anonymous_enum(): |
| 751 |
ffi = FFI() |
| 752 |
ffi.cdef("enum { EE1, ... }; enum { EE3, ... };") |
| 753 |
lib = ffi.verify("enum { EE2, EE1 }; enum { EE3 };") |
| 754 |
assert lib.EE1 == 1 |
| 755 |
assert lib.EE3 == 0 |
| 756 |
|
| 757 |
def test_nonfull_enum_syntax2(): |
| 758 |
ffi = FFI() |
| 759 |
ffi.cdef("enum ee { EE1, EE2=\t..., EE3 };") |
| 760 |
pytest.raises(VerificationMissing, ffi.cast, 'enum ee', 'EE1') |
| 761 |
ffi.verify("enum ee { EE1=10, EE2, EE3=-10, EE4 };") |
| 762 |
assert ffi.string(ffi.cast('enum ee', 11)) == 'EE2' |
| 763 |
assert ffi.string(ffi.cast('enum ee', -10)) == 'EE3' |
| 764 |
|
| 765 |
ffi = FFI() |
| 766 |
ffi.cdef("enum ee { EE1, EE2=\t... };") |
| 767 |
pytest.raises(VerificationMissing, ffi.cast, 'enum ee', 'EE1') |
| 768 |
ffi.verify("enum ee { EE1=10, EE2, EE3=-10, EE4 };") |
| 769 |
assert ffi.string(ffi.cast('enum ee', 11)) == 'EE2' |
| 770 |
|
| 771 |
ffi = FFI() |
| 772 |
ffi.cdef("enum ee2 { EE4=..., EE5=..., ... };") |
| 773 |
ffi.verify("enum ee2 { EE4=-1234-5, EE5 }; ") |
| 774 |
assert ffi.string(ffi.cast('enum ee2', -1239)) == 'EE4' |
| 775 |
assert ffi.string(ffi.cast('enum ee2', -1238)) == 'EE5' |
| 776 |
|
| 777 |
def test_get_set_errno(): |
| 778 |
ffi = FFI() |
| 779 |
ffi.cdef("int foo(int);") |
| 780 |
lib = ffi.verify(""" |
| 781 |
static int foo(int x) |
| 782 |
{ |
| 783 |
errno += 1; |
| 784 |
return x * 7; |
| 785 |
} |
| 786 |
""") |
| 787 |
ffi.errno = 15 |
| 788 |
assert lib.foo(6) == 42 |
| 789 |
assert ffi.errno == 16 |
| 790 |
|
| 791 |
def test_define_int(): |
| 792 |
ffi = FFI() |
| 793 |
ffi.cdef("#define FOO ...\n" |
| 794 |
"\t#\tdefine\tBAR\t...\t\n" |
| 795 |
"#define BAZ ...\n") |
| 796 |
lib = ffi.verify("#define FOO 42\n" |
| 797 |
"#define BAR (-44)\n" |
| 798 |
"#define BAZ 0xffffffffffffffffULL\n") |
| 799 |
assert lib.FOO == 42 |
| 800 |
assert lib.BAR == -44 |
| 801 |
assert lib.BAZ == 0xffffffffffffffff |
| 802 |
|
| 803 |
def test_access_variable(): |
| 804 |
ffi = FFI() |
| 805 |
ffi.cdef("static int foo(void);\n" |
| 806 |
"static int somenumber;") |
| 807 |
lib = ffi.verify(""" |
| 808 |
static int somenumber = 2; |
| 809 |
static int foo(void) { |
| 810 |
return somenumber * 7; |
| 811 |
} |
| 812 |
""") |
| 813 |
assert lib.somenumber == 2 |
| 814 |
assert lib.foo() == 14 |
| 815 |
lib.somenumber = -6 |
| 816 |
assert lib.foo() == -42 |
| 817 |
assert lib.somenumber == -6 |
| 818 |
lib.somenumber = 2 |
| 819 |
|
| 820 |
def test_access_address_of_variable(): |
| 821 |
|
| 822 |
ffi = FFI() |
| 823 |
ffi.cdef("static int somenumber; static int *const somenumberptr;") |
| 824 |
lib = ffi.verify(""" |
| 825 |
static int somenumber = 2; |
| 826 |
#define somenumberptr (&somenumber) |
| 827 |
""") |
| 828 |
assert lib.somenumber == 2 |
| 829 |
lib.somenumberptr[0] = 42 |
| 830 |
assert lib.somenumber == 42 |
| 831 |
lib.somenumber = 2 |
| 832 |
|
| 833 |
def test_access_array_variable(length=5): |
| 834 |
ffi = FFI() |
| 835 |
ffi.cdef("static int foo(int);\n" |
| 836 |
"static int somenumber[%s];" % (length,)) |
| 837 |
lib = ffi.verify(""" |
| 838 |
static int somenumber[] = {2, 2, 3, 4, 5}; |
| 839 |
static int foo(int i) { |
| 840 |
return somenumber[i] * 7; |
| 841 |
} |
| 842 |
""") |
| 843 |
if length == '': |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
assert repr(lib.somenumber).startswith("<cdata 'int *' 0x") |
| 849 |
pytest.raises(TypeError, len, lib.somenumber) |
| 850 |
else: |
| 851 |
assert repr(lib.somenumber).startswith("<cdata 'int[%s]' 0x" % length) |
| 852 |
assert len(lib.somenumber) == 5 |
| 853 |
assert lib.somenumber[3] == 4 |
| 854 |
assert lib.foo(3) == 28 |
| 855 |
lib.somenumber[3] = -6 |
| 856 |
assert lib.foo(3) == -42 |
| 857 |
assert lib.somenumber[3] == -6 |
| 858 |
assert lib.somenumber[4] == 5 |
| 859 |
lib.somenumber[3] = 4 |
| 860 |
|
| 861 |
def test_access_array_variable_length_hidden(): |
| 862 |
test_access_array_variable(length='') |
| 863 |
|
| 864 |
def test_access_struct_variable(): |
| 865 |
ffi = FFI() |
| 866 |
ffi.cdef("struct foo { int x; ...; };\n" |
| 867 |
"static int foo(int);\n" |
| 868 |
"static struct foo stuff;") |
| 869 |
lib = ffi.verify(""" |
| 870 |
struct foo { int x, y, z; }; |
| 871 |
static struct foo stuff = {2, 5, 8}; |
| 872 |
static int foo(int i) { |
| 873 |
switch (i) { |
| 874 |
case 0: return stuff.x * 7; |
| 875 |
case 1: return stuff.y * 7; |
| 876 |
case 2: return stuff.z * 7; |
| 877 |
} |
| 878 |
return -1; |
| 879 |
} |
| 880 |
""") |
| 881 |
assert lib.stuff.x == 2 |
| 882 |
assert lib.foo(0) == 14 |
| 883 |
assert lib.foo(1) == 35 |
| 884 |
assert lib.foo(2) == 56 |
| 885 |
lib.stuff.x = -6 |
| 886 |
assert lib.foo(0) == -42 |
| 887 |
assert lib.foo(1) == 35 |
| 888 |
lib.stuff.x = 2 |
| 889 |
|
| 890 |
def test_access_callback(): |
| 891 |
ffi = FFI() |
| 892 |
ffi.cdef("static int (*cb)(int);\n" |
| 893 |
"static int foo(int);\n" |
| 894 |
"static void reset_cb(void);") |
| 895 |
lib = ffi.verify(""" |
| 896 |
static int g(int x) { return x * 7; } |
| 897 |
static int (*cb)(int); |
| 898 |
static int foo(int i) { return cb(i) - 1; } |
| 899 |
static void reset_cb(void) { cb = g; } |
| 900 |
""") |
| 901 |
lib.reset_cb() |
| 902 |
assert lib.foo(6) == 41 |
| 903 |
my_callback = ffi.callback("int(*)(int)", lambda n: n * 222) |
| 904 |
lib.cb = my_callback |
| 905 |
assert lib.foo(4) == 887 |
| 906 |
|
| 907 |
def test_access_callback_function_typedef(): |
| 908 |
ffi = FFI() |
| 909 |
ffi.cdef("typedef int mycallback_t(int);\n" |
| 910 |
"static mycallback_t *cb;\n" |
| 911 |
"static int foo(int);\n" |
| 912 |
"static void reset_cb(void);") |
| 913 |
lib = ffi.verify(""" |
| 914 |
static int g(int x) { return x * 7; } |
| 915 |
static int (*cb)(int); |
| 916 |
static int foo(int i) { return cb(i) - 1; } |
| 917 |
static void reset_cb(void) { cb = g; } |
| 918 |
""") |
| 919 |
lib.reset_cb() |
| 920 |
assert lib.foo(6) == 41 |
| 921 |
my_callback = ffi.callback("int(*)(int)", lambda n: n * 222) |
| 922 |
lib.cb = my_callback |
| 923 |
assert lib.foo(4) == 887 |
| 924 |
|
| 925 |
def test_call_with_struct_ptr(): |
| 926 |
ffi = FFI() |
| 927 |
ffi.cdef("typedef struct { int x; ...; } foo_t; int foo(foo_t *);") |
| 928 |
lib = ffi.verify(""" |
| 929 |
typedef struct { int y, x; } foo_t; |
| 930 |
static int foo(foo_t *f) { return f->x * 7; } |
| 931 |
""") |
| 932 |
f = ffi.new("foo_t *") |
| 933 |
f.x = 6 |
| 934 |
assert lib.foo(f) == 42 |
| 935 |
|
| 936 |
def test_unknown_type(): |
| 937 |
ffi = FFI() |
| 938 |
ffi.cdef(""" |
| 939 |
typedef ... token_t; |
| 940 |
int foo(token_t *); |
| 941 |
#define TOKEN_SIZE ... |
| 942 |
""") |
| 943 |
lib = ffi.verify(""" |
| 944 |
typedef float token_t; |
| 945 |
static int foo(token_t *tk) { |
| 946 |
if (!tk) |
| 947 |
return -42; |
| 948 |
*tk += 1.601f; |
| 949 |
return (int)*tk; |
| 950 |
} |
| 951 |
#define TOKEN_SIZE sizeof(token_t) |
| 952 |
""") |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
tkmem = ffi.new("char[]", lib.TOKEN_SIZE) |
| 957 |
tk = ffi.cast("token_t *", tkmem) |
| 958 |
results = [lib.foo(tk) for i in range(6)] |
| 959 |
assert results == [1, 3, 4, 6, 8, 9] |
| 960 |
assert lib.foo(ffi.NULL) == -42 |
| 961 |
|
| 962 |
def test_unknown_type_2(): |
| 963 |
ffi = FFI() |
| 964 |
ffi.cdef("typedef ... token_t;") |
| 965 |
lib = ffi.verify("typedef struct token_s token_t;") |
| 966 |
|
| 967 |
|
| 968 |
def test_unknown_type_3(): |
| 969 |
ffi = FFI() |
| 970 |
ffi.cdef(""" |
| 971 |
typedef ... *token_p; |
| 972 |
token_p foo(token_p); |
| 973 |
""") |
| 974 |
lib = ffi.verify(""" |
| 975 |
typedef struct _token_s *token_p; |
| 976 |
token_p foo(token_p arg) { |
| 977 |
if (arg) |
| 978 |
return (token_p)0x12347; |
| 979 |
else |
| 980 |
return (token_p)0x12345; |
| 981 |
} |
| 982 |
""") |
| 983 |
p = lib.foo(ffi.NULL) |
| 984 |
assert int(ffi.cast("intptr_t", p)) == 0x12345 |
| 985 |
q = lib.foo(p) |
| 986 |
assert int(ffi.cast("intptr_t", q)) == 0x12347 |
| 987 |
|
| 988 |
def test_varargs(): |
| 989 |
ffi = FFI() |
| 990 |
ffi.cdef("int foo(int x, ...);") |
| 991 |
lib = ffi.verify(""" |
| 992 |
int foo(int x, ...) { |
| 993 |
va_list vargs; |
| 994 |
va_start(vargs, x); |
| 995 |
x -= va_arg(vargs, int); |
| 996 |
x -= va_arg(vargs, int); |
| 997 |
va_end(vargs); |
| 998 |
return x; |
| 999 |
} |
| 1000 |
""") |
| 1001 |
assert lib.foo(50, ffi.cast("int", 5), ffi.cast("int", 3)) == 42 |
| 1002 |
|
| 1003 |
def test_varargs_exact(): |
| 1004 |
if sys.platform == 'win32': |
| 1005 |
pytest.skip("XXX fixme: only gives warnings") |
| 1006 |
ffi = FFI() |
| 1007 |
ffi.cdef("int foo(int x, ...);") |
| 1008 |
pytest.raises(VerificationError, ffi.verify, """ |
| 1009 |
int foo(long long x, ...) { |
| 1010 |
return x; |
| 1011 |
} |
| 1012 |
""") |
| 1013 |
|
| 1014 |
def test_varargs_struct(): |
| 1015 |
ffi = FFI() |
| 1016 |
ffi.cdef("struct foo_s { char a; int b; }; int foo(int x, ...);") |
| 1017 |
lib = ffi.verify(""" |
| 1018 |
struct foo_s { |
| 1019 |
char a; int b; |
| 1020 |
}; |
| 1021 |
int foo(int x, ...) { |
| 1022 |
va_list vargs; |
| 1023 |
struct foo_s s; |
| 1024 |
va_start(vargs, x); |
| 1025 |
s = va_arg(vargs, struct foo_s); |
| 1026 |
va_end(vargs); |
| 1027 |
return s.a - s.b; |
| 1028 |
} |
| 1029 |
""") |
| 1030 |
s = ffi.new("struct foo_s *", [b'B', 1]) |
| 1031 |
assert lib.foo(50, s[0]) == ord('A') |
| 1032 |
|
| 1033 |
def test_autofilled_struct_as_argument(): |
| 1034 |
ffi = FFI() |
| 1035 |
ffi.cdef("struct foo_s { long a; double b; ...; };\n" |
| 1036 |
"int foo(struct foo_s);") |
| 1037 |
lib = ffi.verify(""" |
| 1038 |
struct foo_s { |
| 1039 |
double b; |
| 1040 |
long a; |
| 1041 |
}; |
| 1042 |
int foo(struct foo_s s) { |
| 1043 |
return (int)s.a - (int)s.b; |
| 1044 |
} |
| 1045 |
""") |
| 1046 |
s = ffi.new("struct foo_s *", [100, 1]) |
| 1047 |
assert lib.foo(s[0]) == 99 |
| 1048 |
assert lib.foo([100, 1]) == 99 |
| 1049 |
|
| 1050 |
def test_autofilled_struct_as_argument_dynamic(): |
| 1051 |
ffi = FFI() |
| 1052 |
ffi.cdef("struct foo_s { long a; ...; };\n" |
| 1053 |
"static int (*foo)(struct foo_s);") |
| 1054 |
lib = ffi.verify(""" |
| 1055 |
struct foo_s { |
| 1056 |
double b; |
| 1057 |
long a; |
| 1058 |
}; |
| 1059 |
int foo1(struct foo_s s) { |
| 1060 |
return (int)s.a - (int)s.b; |
| 1061 |
} |
| 1062 |
static int (*foo)(struct foo_s s) = &foo1; |
| 1063 |
""") |
| 1064 |
e = pytest.raises(NotImplementedError, lib.foo, "?") |
| 1065 |
msg = ("ctype 'struct foo_s' not supported as argument. It is a struct " |
| 1066 |
'declared with "...;", but the C calling convention may depend on ' |
| 1067 |
"the missing fields; or, it contains anonymous struct/unions. " |
| 1068 |
"Such structs are only supported as argument " |
| 1069 |
"if the function is 'API mode' and non-variadic (i.e. declared " |
| 1070 |
"inside ffibuilder.cdef()+ffibuilder.set_source() and not taking " |
| 1071 |
"a final '...' argument)") |
| 1072 |
assert str(e.value) == msg |
| 1073 |
|
| 1074 |
def test_func_returns_struct(): |
| 1075 |
ffi = FFI() |
| 1076 |
ffi.cdef(""" |
| 1077 |
struct foo_s { int aa, bb; }; |
| 1078 |
struct foo_s foo(int a, int b); |
| 1079 |
""") |
| 1080 |
lib = ffi.verify(""" |
| 1081 |
struct foo_s { int aa, bb; }; |
| 1082 |
struct foo_s foo(int a, int b) { |
| 1083 |
struct foo_s r; |
| 1084 |
r.aa = a*a; |
| 1085 |
r.bb = b*b; |
| 1086 |
return r; |
| 1087 |
} |
| 1088 |
""") |
| 1089 |
s = lib.foo(6, 7) |
| 1090 |
assert repr(s) == "<cdata 'struct foo_s' owning 8 bytes>" |
| 1091 |
assert s.aa == 36 |
| 1092 |
assert s.bb == 49 |
| 1093 |
|
| 1094 |
def test_func_as_funcptr(): |
| 1095 |
ffi = FFI() |
| 1096 |
ffi.cdef("int *(*const fooptr)(void);") |
| 1097 |
lib = ffi.verify(""" |
| 1098 |
int *foo(void) { |
| 1099 |
return (int*)"foobar"; |
| 1100 |
} |
| 1101 |
int *(*fooptr)(void) = foo; |
| 1102 |
""") |
| 1103 |
foochar = ffi.cast("char *(*)(void)", lib.fooptr) |
| 1104 |
s = foochar() |
| 1105 |
assert ffi.string(s) == b"foobar" |
| 1106 |
|
| 1107 |
def test_funcptr_as_argument(): |
| 1108 |
ffi = FFI() |
| 1109 |
ffi.cdef(""" |
| 1110 |
void qsort(void *base, size_t nel, size_t width, |
| 1111 |
int (*compar)(const void *, const void *)); |
| 1112 |
""") |
| 1113 |
ffi.verify("#include <stdlib.h>") |
| 1114 |
|
| 1115 |
def test_func_as_argument(): |
| 1116 |
ffi = FFI() |
| 1117 |
ffi.cdef(""" |
| 1118 |
void qsort(void *base, size_t nel, size_t width, |
| 1119 |
int compar(const void *, const void *)); |
| 1120 |
""") |
| 1121 |
ffi.verify("#include <stdlib.h>") |
| 1122 |
|
| 1123 |
def test_array_as_argument(): |
| 1124 |
ffi = FFI() |
| 1125 |
ffi.cdef(""" |
| 1126 |
size_t strlen(char string[]); |
| 1127 |
""") |
| 1128 |
ffi.verify("#include <string.h>") |
| 1129 |
|
| 1130 |
def test_enum_as_argument(): |
| 1131 |
ffi = FFI() |
| 1132 |
ffi.cdef(""" |
| 1133 |
enum foo_e { AA, BB, ... }; |
| 1134 |
int foo_func(enum foo_e); |
| 1135 |
""") |
| 1136 |
lib = ffi.verify(""" |
| 1137 |
enum foo_e { AA, CC, BB }; |
| 1138 |
int foo_func(enum foo_e e) { return (int)e; } |
| 1139 |
""") |
| 1140 |
assert lib.foo_func(lib.BB) == 2 |
| 1141 |
pytest.raises(TypeError, lib.foo_func, "BB") |
| 1142 |
|
| 1143 |
def test_enum_as_function_result(): |
| 1144 |
ffi = FFI() |
| 1145 |
ffi.cdef(""" |
| 1146 |
enum foo_e { AA, BB, ... }; |
| 1147 |
enum foo_e foo_func(int x); |
| 1148 |
""") |
| 1149 |
lib = ffi.verify(""" |
| 1150 |
enum foo_e { AA, CC, BB }; |
| 1151 |
enum foo_e foo_func(int x) { return (enum foo_e)x; } |
| 1152 |
""") |
| 1153 |
assert lib.foo_func(lib.BB) == lib.BB == 2 |
| 1154 |
|
| 1155 |
def test_enum_values(): |
| 1156 |
ffi = FFI() |
| 1157 |
ffi.cdef("enum enum1_e { AA, BB };") |
| 1158 |
lib = ffi.verify("enum enum1_e { AA, BB };") |
| 1159 |
assert lib.AA == 0 |
| 1160 |
assert lib.BB == 1 |
| 1161 |
assert ffi.string(ffi.cast("enum enum1_e", 1)) == 'BB' |
| 1162 |
|
| 1163 |
def test_typedef_complete_enum(): |
| 1164 |
ffi = FFI() |
| 1165 |
ffi.cdef("typedef enum { AA, BB } enum1_t;") |
| 1166 |
lib = ffi.verify("typedef enum { AA, BB } enum1_t;") |
| 1167 |
assert ffi.string(ffi.cast("enum1_t", 1)) == 'BB' |
| 1168 |
assert lib.AA == 0 |
| 1169 |
assert lib.BB == 1 |
| 1170 |
|
| 1171 |
def test_typedef_broken_complete_enum(): |
| 1172 |
|
| 1173 |
ffi = FFI() |
| 1174 |
ffi.cdef("typedef enum { AA, BB } enum1_t;") |
| 1175 |
lib = ffi.verify("typedef enum { AA, CC, BB } enum1_t;") |
| 1176 |
assert lib.AA == 0 |
| 1177 |
assert lib.BB == 2 |
| 1178 |
|
| 1179 |
def test_typedef_incomplete_enum(): |
| 1180 |
ffi = FFI() |
| 1181 |
ffi.cdef("typedef enum { AA, BB, ... } enum1_t;") |
| 1182 |
lib = ffi.verify("typedef enum { AA, CC, BB } enum1_t;") |
| 1183 |
assert ffi.string(ffi.cast("enum1_t", 1)) == '1' |
| 1184 |
assert ffi.string(ffi.cast("enum1_t", 2)) == 'BB' |
| 1185 |
assert lib.AA == 0 |
| 1186 |
assert lib.BB == 2 |
| 1187 |
|
| 1188 |
def test_typedef_enum_as_argument(): |
| 1189 |
ffi = FFI() |
| 1190 |
ffi.cdef(""" |
| 1191 |
typedef enum { AA, BB, ... } foo_t; |
| 1192 |
int foo_func(foo_t); |
| 1193 |
""") |
| 1194 |
lib = ffi.verify(""" |
| 1195 |
typedef enum { AA, CC, BB } foo_t; |
| 1196 |
int foo_func(foo_t e) { return (int)e; } |
| 1197 |
""") |
| 1198 |
assert lib.foo_func(lib.BB) == lib.BB == 2 |
| 1199 |
pytest.raises(TypeError, lib.foo_func, "BB") |
| 1200 |
|
| 1201 |
def test_typedef_enum_as_function_result(): |
| 1202 |
ffi = FFI() |
| 1203 |
ffi.cdef(""" |
| 1204 |
typedef enum { AA, BB, ... } foo_t; |
| 1205 |
foo_t foo_func(int x); |
| 1206 |
""") |
| 1207 |
lib = ffi.verify(""" |
| 1208 |
typedef enum { AA, CC, BB } foo_t; |
| 1209 |
foo_t foo_func(int x) { return (foo_t)x; } |
| 1210 |
""") |
| 1211 |
assert lib.foo_func(lib.BB) == lib.BB == 2 |
| 1212 |
|
| 1213 |
def test_function_typedef(): |
| 1214 |
ffi = FFI() |
| 1215 |
ffi.cdef(""" |
| 1216 |
typedef double func_t(double); |
| 1217 |
func_t sin; |
| 1218 |
""") |
| 1219 |
lib = ffi.verify('#include <math.h>', libraries=lib_m) |
| 1220 |
assert lib.sin(1.23) == math.sin(1.23) |
| 1221 |
|
| 1222 |
def test_opaque_integer_as_function_result(): |
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
ffi = FFI() |
| 1231 |
ffi.cdef(""" |
| 1232 |
typedef struct { ...; } myhandle_t; |
| 1233 |
myhandle_t foo(void); |
| 1234 |
""") |
| 1235 |
lib = ffi.verify(""" |
| 1236 |
typedef short myhandle_t; |
| 1237 |
myhandle_t foo(void) { return 42; } |
| 1238 |
""") |
| 1239 |
h = lib.foo() |
| 1240 |
assert ffi.sizeof(h) == ffi.sizeof("short") |
| 1241 |
|
| 1242 |
def test_return_partial_struct(): |
| 1243 |
ffi = FFI() |
| 1244 |
ffi.cdef(""" |
| 1245 |
typedef struct { int x; ...; } foo_t; |
| 1246 |
foo_t foo(void); |
| 1247 |
""") |
| 1248 |
lib = ffi.verify(""" |
| 1249 |
typedef struct { int y, x; } foo_t; |
| 1250 |
foo_t foo(void) { foo_t r = { 45, 81 }; return r; } |
| 1251 |
""") |
| 1252 |
h = lib.foo() |
| 1253 |
assert ffi.sizeof(h) == 2 * ffi.sizeof("int") |
| 1254 |
assert h.x == 81 |
| 1255 |
|
| 1256 |
def test_take_and_return_partial_structs(): |
| 1257 |
ffi = FFI() |
| 1258 |
ffi.cdef(""" |
| 1259 |
typedef struct { int x; ...; } foo_t; |
| 1260 |
foo_t foo(foo_t, foo_t); |
| 1261 |
""") |
| 1262 |
lib = ffi.verify(""" |
| 1263 |
typedef struct { int y, x; } foo_t; |
| 1264 |
foo_t foo(foo_t a, foo_t b) { |
| 1265 |
foo_t r = { 100, a.x * 5 + b.x * 7 }; |
| 1266 |
return r; |
| 1267 |
} |
| 1268 |
""") |
| 1269 |
args = ffi.new("foo_t[3]") |
| 1270 |
args[0].x = 1000 |
| 1271 |
args[2].x = -498 |
| 1272 |
h = lib.foo(args[0], args[2]) |
| 1273 |
assert ffi.sizeof(h) == 2 * ffi.sizeof("int") |
| 1274 |
assert h.x == 1000 * 5 - 498 * 7 |
| 1275 |
|
| 1276 |
def test_cannot_name_struct_type(): |
| 1277 |
ffi = FFI() |
| 1278 |
ffi.cdef("typedef struct { int x; } **sp; void foo(sp);") |
| 1279 |
e = pytest.raises(VerificationError, ffi.verify, |
| 1280 |
"typedef struct { int x; } **sp; void foo(sp x) { }") |
| 1281 |
assert 'in argument of foo: unknown type name' in str(e.value) |
| 1282 |
|
| 1283 |
def test_dont_check_unnamable_fields(): |
| 1284 |
ffi = FFI() |
| 1285 |
ffi.cdef("struct foo_s { struct { int x; } someone; };") |
| 1286 |
ffi.verify("struct foo_s { struct { int x; } someone; };") |
| 1287 |
|
| 1288 |
|
| 1289 |
def test_nested_anonymous_struct_exact(): |
| 1290 |
if sys.platform == 'win32': |
| 1291 |
pytest.skip("nested anonymous struct/union") |
| 1292 |
ffi = FFI() |
| 1293 |
ffi.cdef(""" |
| 1294 |
struct foo_s { struct { int a; char b; }; union { char c, d; }; }; |
| 1295 |
""") |
| 1296 |
assert ffi.offsetof("struct foo_s", "c") == 2 * ffi.sizeof("int") |
| 1297 |
assert ffi.sizeof("struct foo_s") == 3 * ffi.sizeof("int") |
| 1298 |
ffi.verify(""" |
| 1299 |
struct foo_s { struct { int a; char b; }; union { char c, d; }; }; |
| 1300 |
""") |
| 1301 |
p = ffi.new("struct foo_s *") |
| 1302 |
assert ffi.sizeof(p[0]) == 3 * ffi.sizeof("int") |
| 1303 |
p.a = 1234567 |
| 1304 |
p.b = b'X' |
| 1305 |
p.c = b'Y' |
| 1306 |
assert p.a == 1234567 |
| 1307 |
assert p.b == b'X' |
| 1308 |
assert p.c == b'Y' |
| 1309 |
assert p.d == b'Y' |
| 1310 |
|
| 1311 |
def test_nested_anonymous_struct_exact_error(): |
| 1312 |
if sys.platform == 'win32': |
| 1313 |
pytest.skip("nested anonymous struct/union") |
| 1314 |
ffi = FFI() |
| 1315 |
ffi.cdef(""" |
| 1316 |
struct foo_s { struct { int a; char b; }; union { char c, d; }; }; |
| 1317 |
""") |
| 1318 |
pytest.raises(VerificationError, ffi.verify, """ |
| 1319 |
struct foo_s { struct { int a; short b; }; union { char c, d; }; }; |
| 1320 |
""") |
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
def test_nested_anonymous_struct_inexact_1(): |
| 1327 |
ffi = FFI() |
| 1328 |
ffi.cdef(""" |
| 1329 |
struct foo_s { struct { char b; ...; }; union { char c, d; }; }; |
| 1330 |
""") |
| 1331 |
ffi.verify(""" |
| 1332 |
struct foo_s { int a, padding; char c, d, b; }; |
| 1333 |
""") |
| 1334 |
assert ffi.sizeof("struct foo_s") == 3 * ffi.sizeof("int") |
| 1335 |
|
| 1336 |
def test_nested_anonymous_struct_inexact_2(): |
| 1337 |
ffi = FFI() |
| 1338 |
ffi.cdef(""" |
| 1339 |
struct foo_s { union { char c, d; }; struct { int a; char b; }; ...; }; |
| 1340 |
""") |
| 1341 |
ffi.verify(""" |
| 1342 |
struct foo_s { int a, padding; char c, d, b; }; |
| 1343 |
""") |
| 1344 |
assert ffi.sizeof("struct foo_s") == 3 * ffi.sizeof("int") |
| 1345 |
|
| 1346 |
def test_ffi_union(): |
| 1347 |
ffi = FFI() |
| 1348 |
ffi.cdef("union foo_u { char x; long *z; };") |
| 1349 |
ffi.verify("union foo_u { char x; int y; long *z; };") |
| 1350 |
|
| 1351 |
def test_ffi_union_partial(): |
| 1352 |
ffi = FFI() |
| 1353 |
ffi.cdef("union foo_u { char x; ...; };") |
| 1354 |
ffi.verify("union foo_u { char x; int y; };") |
| 1355 |
assert ffi.sizeof("union foo_u") == 4 |
| 1356 |
|
| 1357 |
def test_ffi_union_with_partial_struct(): |
| 1358 |
ffi = FFI() |
| 1359 |
ffi.cdef("struct foo_s { int x; ...; }; union foo_u { struct foo_s s; };") |
| 1360 |
ffi.verify("struct foo_s { int a; int x; }; " |
| 1361 |
"union foo_u { char b[32]; struct foo_s s; };") |
| 1362 |
assert ffi.sizeof("struct foo_s") == 8 |
| 1363 |
assert ffi.sizeof("union foo_u") == 32 |
| 1364 |
|
| 1365 |
def test_ffi_union_partial_2(): |
| 1366 |
ffi = FFI() |
| 1367 |
ffi.cdef("typedef union { char x; ...; } u1;") |
| 1368 |
ffi.verify("typedef union { char x; int y; } u1;") |
| 1369 |
assert ffi.sizeof("u1") == 4 |
| 1370 |
|
| 1371 |
def test_ffi_union_with_partial_struct_2(): |
| 1372 |
ffi = FFI() |
| 1373 |
ffi.cdef("typedef struct { int x; ...; } s1;" |
| 1374 |
"typedef union { s1 s; } u1;") |
| 1375 |
ffi.verify("typedef struct { int a; int x; } s1; " |
| 1376 |
"typedef union { char b[32]; s1 s; } u1;") |
| 1377 |
assert ffi.sizeof("s1") == 8 |
| 1378 |
assert ffi.sizeof("u1") == 32 |
| 1379 |
assert ffi.offsetof("u1", "s") == 0 |
| 1380 |
|
| 1381 |
def test_ffi_struct_packed(): |
| 1382 |
if sys.platform == 'win32': |
| 1383 |
pytest.skip("needs a GCC extension") |
| 1384 |
ffi = FFI() |
| 1385 |
ffi.cdef("struct foo_s { int b; ...; };") |
| 1386 |
ffi.verify(""" |
| 1387 |
struct foo_s { |
| 1388 |
char a; |
| 1389 |
int b; |
| 1390 |
} __attribute__((packed)); |
| 1391 |
""") |
| 1392 |
|
| 1393 |
def test_tmpdir(): |
| 1394 |
import tempfile, os |
| 1395 |
from extra_tests.cffi_tests.udir import udir |
| 1396 |
tmpdir = tempfile.mkdtemp(dir=str(udir)) |
| 1397 |
ffi = FFI() |
| 1398 |
ffi.cdef("int foo(int);") |
| 1399 |
lib = ffi.verify("int foo(int a) { return a + 42; }", tmpdir=tmpdir) |
| 1400 |
assert os.listdir(tmpdir) |
| 1401 |
assert lib.foo(100) == 142 |
| 1402 |
|
| 1403 |
def test_relative_to(): |
| 1404 |
pytest.skip("not available") |
| 1405 |
import tempfile, os |
| 1406 |
from extra_tests.cffi_tests.udir import udir |
| 1407 |
tmpdir = tempfile.mkdtemp(dir=str(udir)) |
| 1408 |
ffi = FFI() |
| 1409 |
ffi.cdef("int foo(int);") |
| 1410 |
f = open(os.path.join(tmpdir, 'foo.h'), 'w') |
| 1411 |
f.write("int foo(int a) { return a + 42; }\n") |
| 1412 |
f.close() |
| 1413 |
lib = ffi.verify('#include "foo.h"', |
| 1414 |
include_dirs=['.'], |
| 1415 |
relative_to=os.path.join(tmpdir, 'x')) |
| 1416 |
assert lib.foo(100) == 142 |
| 1417 |
|
| 1418 |
def test_bug1(): |
| 1419 |
ffi = FFI() |
| 1420 |
ffi.cdef(""" |
| 1421 |
typedef struct tdlhandle_s { ...; } *tdl_handle_t; |
| 1422 |
typedef struct my_error_code_ { |
| 1423 |
tdl_handle_t *rh; |
| 1424 |
} my_error_code_t; |
| 1425 |
""") |
| 1426 |
ffi.verify(""" |
| 1427 |
typedef struct tdlhandle_s { int foo; } *tdl_handle_t; |
| 1428 |
typedef struct my_error_code_ { |
| 1429 |
tdl_handle_t *rh; |
| 1430 |
} my_error_code_t; |
| 1431 |
""") |
| 1432 |
|
| 1433 |
def test_bool(): |
| 1434 |
if sys.platform == 'win32': |
| 1435 |
pytest.skip("_Bool not in MSVC") |
| 1436 |
ffi = FFI() |
| 1437 |
ffi.cdef("struct foo_s { _Bool x; };" |
| 1438 |
"_Bool foo(_Bool); static _Bool (*foop)(_Bool);") |
| 1439 |
lib = ffi.verify(""" |
| 1440 |
struct foo_s { _Bool x; }; |
| 1441 |
int foo(int arg) { |
| 1442 |
return !arg; |
| 1443 |
} |
| 1444 |
_Bool _foofunc(_Bool x) { |
| 1445 |
return !x; |
| 1446 |
} |
| 1447 |
static _Bool (*foop)(_Bool) = _foofunc; |
| 1448 |
""") |
| 1449 |
p = ffi.new("struct foo_s *") |
| 1450 |
p.x = 1 |
| 1451 |
assert p.x is True |
| 1452 |
with pytest.raises(OverflowError): |
| 1453 |
p.x = -1 |
| 1454 |
with pytest.raises(TypeError): |
| 1455 |
p.x = 0.0 |
| 1456 |
assert lib.foop(1) is False |
| 1457 |
assert lib.foop(True) is False |
| 1458 |
assert lib.foop(0) is True |
| 1459 |
pytest.raises(OverflowError, lib.foop, 42) |
| 1460 |
pytest.raises(TypeError, lib.foop, 0.0) |
| 1461 |
assert lib.foo(1) is False |
| 1462 |
assert lib.foo(True) is False |
| 1463 |
assert lib.foo(0) is True |
| 1464 |
pytest.raises(OverflowError, lib.foo, 42) |
| 1465 |
pytest.raises(TypeError, lib.foo, 0.0) |
| 1466 |
assert int(ffi.cast("_Bool", long(1))) == 1 |
| 1467 |
assert int(ffi.cast("_Bool", long(0))) == 0 |
| 1468 |
assert int(ffi.cast("_Bool", long(-1))) == 1 |
| 1469 |
assert int(ffi.cast("_Bool", 10**200)) == 1 |
| 1470 |
assert int(ffi.cast("_Bool", 10**40000)) == 1 |
| 1471 |
|
| 1472 |
class Foo(object): |
| 1473 |
def __int__(self): |
| 1474 |
self.seen = 1 |
| 1475 |
return result |
| 1476 |
f = Foo() |
| 1477 |
f.seen = 0 |
| 1478 |
result = 42 |
| 1479 |
assert int(ffi.cast("_Bool", f)) == 1 |
| 1480 |
assert f.seen |
| 1481 |
f.seen = 0 |
| 1482 |
result = 0 |
| 1483 |
assert int(ffi.cast("_Bool", f)) == 0 |
| 1484 |
assert f.seen |
| 1485 |
|
| 1486 |
pytest.raises(TypeError, ffi.cast, "_Bool", []) |
| 1487 |
|
| 1488 |
def test_bool_on_long_double(): |
| 1489 |
if sys.platform == 'win32': |
| 1490 |
pytest.skip("_Bool not in MSVC") |
| 1491 |
f = 1E-250 |
| 1492 |
if f == 0.0 or f*f != 0.0: |
| 1493 |
pytest.skip("unexpected precision") |
| 1494 |
ffi = FFI() |
| 1495 |
ffi.cdef("long double square(long double f); _Bool opposite(_Bool);") |
| 1496 |
lib = ffi.verify("long double square(long double f) { return f*f; }\n" |
| 1497 |
"_Bool opposite(_Bool x) { return !x; }") |
| 1498 |
f0 = lib.square(0.0) |
| 1499 |
f2 = lib.square(f) |
| 1500 |
f3 = lib.square(f * 2.0) |
| 1501 |
if repr(f2) == repr(f3): |
| 1502 |
pytest.skip("long double doesn't have enough precision") |
| 1503 |
assert float(f0) == float(f2) == float(f3) == 0.0 |
| 1504 |
assert int(ffi.cast("_Bool", f2)) == 1 |
| 1505 |
assert int(ffi.cast("_Bool", f3)) == 1 |
| 1506 |
assert int(ffi.cast("_Bool", f0)) == 0 |
| 1507 |
pytest.raises(TypeError, lib.opposite, f2) |
| 1508 |
|
| 1509 |
def test_cannot_pass_float(): |
| 1510 |
for basetype in ['char', 'short', 'int', 'long', 'long long']: |
| 1511 |
for sign in ['signed', 'unsigned']: |
| 1512 |
type = '%s %s' % (sign, basetype) |
| 1513 |
ffi = FFI() |
| 1514 |
ffi.cdef("struct foo_s { %s x; };\n" |
| 1515 |
"int foo(%s);" % (type, type)) |
| 1516 |
lib = ffi.verify(""" |
| 1517 |
struct foo_s { %s x; }; |
| 1518 |
int foo(%s arg) { |
| 1519 |
return !arg; |
| 1520 |
} |
| 1521 |
""" % (type, type)) |
| 1522 |
p = ffi.new("struct foo_s *") |
| 1523 |
with pytest.raises(TypeError): |
| 1524 |
p.x = 0.0 |
| 1525 |
assert lib.foo(42) == 0 |
| 1526 |
assert lib.foo(0) == 1 |
| 1527 |
pytest.raises(TypeError, lib.foo, 0.0) |
| 1528 |
|
| 1529 |
def test_addressof(): |
| 1530 |
ffi = FFI() |
| 1531 |
ffi.cdef(""" |
| 1532 |
struct point_s { int x, y; }; |
| 1533 |
struct foo_s { int z; struct point_s point; }; |
| 1534 |
struct point_s sum_coord(struct point_s *); |
| 1535 |
""") |
| 1536 |
lib = ffi.verify(""" |
| 1537 |
struct point_s { int x, y; }; |
| 1538 |
struct foo_s { int z; struct point_s point; }; |
| 1539 |
struct point_s sum_coord(struct point_s *point) { |
| 1540 |
struct point_s r; |
| 1541 |
r.x = point->x + point->y; |
| 1542 |
r.y = point->x - point->y; |
| 1543 |
return r; |
| 1544 |
} |
| 1545 |
""") |
| 1546 |
p = ffi.new("struct foo_s *") |
| 1547 |
p.point.x = 16 |
| 1548 |
p.point.y = 9 |
| 1549 |
pytest.raises(TypeError, lib.sum_coord, p.point) |
| 1550 |
res = lib.sum_coord(ffi.addressof(p.point)) |
| 1551 |
assert res.x == 25 |
| 1552 |
assert res.y == 7 |
| 1553 |
res2 = lib.sum_coord(ffi.addressof(res)) |
| 1554 |
assert res2.x == 32 |
| 1555 |
assert res2.y == 18 |
| 1556 |
pytest.raises(TypeError, lib.sum_coord, res2) |
| 1557 |
|
| 1558 |
def test_callback_in_thread(): |
| 1559 |
pytest.xfail("adapt or remove") |
| 1560 |
if sys.platform == 'win32': |
| 1561 |
pytest.skip("pthread only") |
| 1562 |
import os, subprocess |
| 1563 |
from cffi import _imp_emulation as imp |
| 1564 |
arg = os.path.join(os.path.dirname(__file__), 'callback_in_thread.py') |
| 1565 |
g = subprocess.Popen([sys.executable, arg, |
| 1566 |
os.path.dirname(imp.find_module('cffi')[1])]) |
| 1567 |
result = g.wait() |
| 1568 |
assert result == 0 |
| 1569 |
|
| 1570 |
def test_keepalive_lib(): |
| 1571 |
pytest.xfail("adapt or remove") |
| 1572 |
ffi = FFI() |
| 1573 |
ffi.cdef("int foobar(void);") |
| 1574 |
lib = ffi.verify("int foobar(void) { return 42; }") |
| 1575 |
func = lib.foobar |
| 1576 |
ffi_r = weakref.ref(ffi) |
| 1577 |
lib_r = weakref.ref(lib) |
| 1578 |
del ffi |
| 1579 |
import gc; gc.collect() |
| 1580 |
assert lib_r() is not None |
| 1581 |
assert ffi_r() is not None |
| 1582 |
assert func() == 42 |
| 1583 |
|
| 1584 |
def test_keepalive_ffi(): |
| 1585 |
pytest.xfail("adapt or remove") |
| 1586 |
ffi = FFI() |
| 1587 |
ffi.cdef("int foobar(void);") |
| 1588 |
lib = ffi.verify("int foobar(void) { return 42; }") |
| 1589 |
func = lib.foobar |
| 1590 |
ffi_r = weakref.ref(ffi) |
| 1591 |
lib_r = weakref.ref(lib) |
| 1592 |
del lib |
| 1593 |
import gc; gc.collect() |
| 1594 |
assert ffi_r() is not None |
| 1595 |
assert lib_r() is not None |
| 1596 |
assert func() == 42 |
| 1597 |
|
| 1598 |
def test_FILE_stored_in_stdout(): |
| 1599 |
if not sys.platform.startswith('linux') or is_musl: |
| 1600 |
pytest.skip("likely, we cannot assign to stdout") |
| 1601 |
ffi = FFI() |
| 1602 |
ffi.cdef("int printf(const char *, ...); FILE *setstdout(FILE *);") |
| 1603 |
lib = ffi.verify(""" |
| 1604 |
#include <stdio.h> |
| 1605 |
FILE *setstdout(FILE *f) { |
| 1606 |
FILE *result = stdout; |
| 1607 |
stdout = f; |
| 1608 |
return result; |
| 1609 |
} |
| 1610 |
""") |
| 1611 |
import os |
| 1612 |
fdr, fdw = os.pipe() |
| 1613 |
fw1 = os.fdopen(fdw, 'wb', 256) |
| 1614 |
old_stdout = lib.setstdout(fw1) |
| 1615 |
try: |
| 1616 |
|
| 1617 |
fw1.write(b"X") |
| 1618 |
r = lib.printf(b"hello, %d!\n", ffi.cast("int", 42)) |
| 1619 |
fw1.close() |
| 1620 |
assert r == len("hello, 42!\n") |
| 1621 |
|
| 1622 |
finally: |
| 1623 |
lib.setstdout(old_stdout) |
| 1624 |
|
| 1625 |
result = os.read(fdr, 256) |
| 1626 |
os.close(fdr) |
| 1627 |
|
| 1628 |
|
| 1629 |
assert result == b"Xhello, 42!\n" or result == b"hello, 42!\nX" |
| 1630 |
|
| 1631 |
def test_FILE_stored_explicitly(): |
| 1632 |
ffi = FFI() |
| 1633 |
ffi.cdef("int myprintf11(const char *, int); extern FILE *myfile;") |
| 1634 |
lib = ffi.verify(""" |
| 1635 |
#include <stdio.h> |
| 1636 |
FILE *myfile; |
| 1637 |
int myprintf11(const char *out, int value) { |
| 1638 |
return fprintf(myfile, out, value); |
| 1639 |
} |
| 1640 |
""") |
| 1641 |
import os |
| 1642 |
fdr, fdw = os.pipe() |
| 1643 |
fw1 = os.fdopen(fdw, 'wb', 256) |
| 1644 |
lib.myfile = ffi.cast("FILE *", fw1) |
| 1645 |
|
| 1646 |
fw1.write(b"X") |
| 1647 |
r = lib.myprintf11(b"hello, %d!\n", ffi.cast("int", 42)) |
| 1648 |
fw1.close() |
| 1649 |
assert r == len("hello, 42!\n") |
| 1650 |
|
| 1651 |
result = os.read(fdr, 256) |
| 1652 |
os.close(fdr) |
| 1653 |
|
| 1654 |
|
| 1655 |
assert result == b"Xhello, 42!\n" or result == b"hello, 42!\nX" |
| 1656 |
|
| 1657 |
def test_global_array_with_missing_length(): |
| 1658 |
ffi = FFI() |
| 1659 |
ffi.cdef("extern int fooarray[];") |
| 1660 |
lib = ffi.verify("int fooarray[50];") |
| 1661 |
assert repr(lib.fooarray).startswith("<cdata 'int *'") |
| 1662 |
|
| 1663 |
def test_global_array_with_dotdotdot_length(): |
| 1664 |
ffi = FFI() |
| 1665 |
ffi.cdef("extern int fooarray[...];") |
| 1666 |
lib = ffi.verify("int fooarray[50];") |
| 1667 |
assert repr(lib.fooarray).startswith("<cdata 'int[50]'") |
| 1668 |
|
| 1669 |
def test_bad_global_array_with_dotdotdot_length(): |
| 1670 |
pytest.xfail("was detected only because 23 bytes cannot be divided by 4; " |
| 1671 |
"redo more generally") |
| 1672 |
ffi = FFI() |
| 1673 |
ffi.cdef("extern int fooarray[...];") |
| 1674 |
pytest.raises(VerificationError, ffi.verify, "char fooarray[23];") |
| 1675 |
|
| 1676 |
def test_struct_containing_struct(): |
| 1677 |
ffi = FFI() |
| 1678 |
ffi.cdef("struct foo_s { ...; }; struct bar_s { struct foo_s f; ...; };") |
| 1679 |
ffi.verify("struct foo_s { int x; }; struct bar_s { struct foo_s f; };") |
| 1680 |
|
| 1681 |
ffi = FFI() |
| 1682 |
ffi.cdef("struct foo_s { struct bar_s f; ...; }; struct bar_s { ...; };") |
| 1683 |
ffi.verify("struct bar_s { int x; }; struct foo_s { struct bar_s f; };") |
| 1684 |
|
| 1685 |
def test_struct_returned_by_func(): |
| 1686 |
ffi = FFI() |
| 1687 |
ffi.cdef("typedef ... foo_t; foo_t myfunc(void);") |
| 1688 |
e = pytest.raises(TypeError, ffi.verify, |
| 1689 |
"typedef struct { int x; } foo_t; " |
| 1690 |
"foo_t myfunc(void) { foo_t x = { 42 }; return x; }") |
| 1691 |
assert str(e.value) == ( |
| 1692 |
"function myfunc: 'foo_t' is used as result type, but is opaque") |
| 1693 |
|
| 1694 |
def test_include(): |
| 1695 |
ffi1 = FFI() |
| 1696 |
ffi1.cdef("typedef struct { int x; ...; } foo_t;") |
| 1697 |
ffi1.verify("typedef struct { int y, x; } foo_t;") |
| 1698 |
ffi2 = FFI() |
| 1699 |
ffi2.include(ffi1) |
| 1700 |
ffi2.cdef("int myfunc(foo_t *);") |
| 1701 |
lib = ffi2.verify("typedef struct { int y, x; } foo_t;" |
| 1702 |
"int myfunc(foo_t *p) { return 42 * p->x; }") |
| 1703 |
res = lib.myfunc(ffi2.new("foo_t *", {'x': 10})) |
| 1704 |
assert res == 420 |
| 1705 |
res = lib.myfunc(ffi1.new("foo_t *", {'x': -10})) |
| 1706 |
assert res == -420 |
| 1707 |
|
| 1708 |
def test_include_enum(): |
| 1709 |
ffi1 = FFI() |
| 1710 |
ffi1.cdef("enum foo_e { AA, ... };") |
| 1711 |
lib1 = ffi1.verify("enum foo_e { CC, BB, AA };") |
| 1712 |
ffi2 = FFI() |
| 1713 |
ffi2.include(ffi1) |
| 1714 |
ffi2.cdef("int myfunc(enum foo_e);") |
| 1715 |
lib2 = ffi2.verify("enum foo_e { CC, BB, AA };" |
| 1716 |
"int myfunc(enum foo_e x) { return (int)x; }") |
| 1717 |
res = lib2.myfunc(lib2.AA) |
| 1718 |
assert res == 2 |
| 1719 |
|
| 1720 |
def test_named_pointer_as_argument(): |
| 1721 |
ffi = FFI() |
| 1722 |
ffi.cdef("typedef struct { int x; } *mystruct_p;\n" |
| 1723 |
"mystruct_p ff5a(mystruct_p);") |
| 1724 |
lib = ffi.verify("typedef struct { int x; } *mystruct_p;\n" |
| 1725 |
"mystruct_p ff5a(mystruct_p p) { p->x += 40; return p; }") |
| 1726 |
p = ffi.new("mystruct_p", [-2]) |
| 1727 |
q = lib.ff5a(p) |
| 1728 |
assert q == p |
| 1729 |
assert p.x == 38 |
| 1730 |
|
| 1731 |
def test_enum_size(): |
| 1732 |
cases = [('123', 4, 4294967295), |
| 1733 |
('4294967295U', 4, 4294967295), |
| 1734 |
('-123', 4, -1), |
| 1735 |
('-2147483647-1', 4, -1), |
| 1736 |
] |
| 1737 |
if FFI().sizeof("long") == 8: |
| 1738 |
cases += [('4294967296L', 8, 2**64-1), |
| 1739 |
('%dUL' % (2**64-1), 8, 2**64-1), |
| 1740 |
('-2147483649L', 8, -1), |
| 1741 |
('%dL-1L' % (1-2**63), 8, -1)] |
| 1742 |
for hidden_value, expected_size, expected_minus1 in cases: |
| 1743 |
if sys.platform == 'win32' and 'U' in hidden_value: |
| 1744 |
continue |
| 1745 |
ffi = FFI() |
| 1746 |
ffi.cdef("enum foo_e { AA, BB, ... };") |
| 1747 |
lib = ffi.verify("enum foo_e { AA, BB=%s };" % hidden_value) |
| 1748 |
assert lib.AA == 0 |
| 1749 |
assert lib.BB == eval(hidden_value.replace('U', '').replace('L', '')) |
| 1750 |
assert ffi.sizeof("enum foo_e") == expected_size |
| 1751 |
if sys.platform != 'win32': |
| 1752 |
assert int(ffi.cast("enum foo_e", -1)) == expected_minus1 |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
def test_enum_bug118(): |
| 1764 |
maxulong = 256 ** FFI().sizeof("unsigned long") - 1 |
| 1765 |
for c2, c2c in [(-1, ''), |
| 1766 |
(-1, ''), |
| 1767 |
(0xffffffff, 'U'), |
| 1768 |
(maxulong, 'UL'), |
| 1769 |
(-int(maxulong / 3), 'L')]: |
| 1770 |
if c2c and sys.platform == 'win32': |
| 1771 |
continue |
| 1772 |
ffi = FFI() |
| 1773 |
ffi.cdef("enum foo_e { AA };") |
| 1774 |
lib = ffi.verify("enum foo_e { AA=%s%s };" % (c2, c2c)) |
| 1775 |
assert lib.AA == c2 |
| 1776 |
|
| 1777 |
def test_string_to_voidp_arg(): |
| 1778 |
ffi = FFI() |
| 1779 |
ffi.cdef("int myfunc(void *);") |
| 1780 |
lib = ffi.verify("int myfunc(void *p) { return ((signed char *)p)[0]; }") |
| 1781 |
res = lib.myfunc(b"hi!") |
| 1782 |
assert res == ord(b"h") |
| 1783 |
p = ffi.new("char[]", b"gah") |
| 1784 |
res = lib.myfunc(p) |
| 1785 |
assert res == ord(b"g") |
| 1786 |
res = lib.myfunc(ffi.cast("void *", p)) |
| 1787 |
assert res == ord(b"g") |
| 1788 |
res = lib.myfunc(ffi.cast("int *", p)) |
| 1789 |
assert res == ord(b"g") |
| 1790 |
|
| 1791 |
def test_callback_indirection(): |
| 1792 |
ffi = FFI() |
| 1793 |
ffi.cdef(""" |
| 1794 |
static int (*python_callback)(int how_many, int *values); |
| 1795 |
int (*const c_callback)(int,...); /* pass this ptr to C routines */ |
| 1796 |
int some_c_function(int(*cb)(int,...)); |
| 1797 |
""") |
| 1798 |
lib = ffi.verify(""" |
| 1799 |
#include <stdarg.h> |
| 1800 |
#ifdef _WIN32 |
| 1801 |
#include <malloc.h> |
| 1802 |
#define alloca _alloca |
| 1803 |
#else |
| 1804 |
# ifdef __FreeBSD__ |
| 1805 |
# include <stdlib.h> |
| 1806 |
# else |
| 1807 |
# include <alloca.h> |
| 1808 |
# endif |
| 1809 |
#endif |
| 1810 |
static int (*python_callback)(int how_many, int *values); |
| 1811 |
static int c_callback(int how_many, ...) { |
| 1812 |
va_list ap; |
| 1813 |
/* collect the "..." arguments into the values[] array */ |
| 1814 |
int i, *values = alloca((size_t)how_many * sizeof(int)); |
| 1815 |
va_start(ap, how_many); |
| 1816 |
for (i=0; i<how_many; i++) |
| 1817 |
values[i] = va_arg(ap, int); |
| 1818 |
va_end(ap); |
| 1819 |
return python_callback(how_many, values); |
| 1820 |
} |
| 1821 |
int some_c_function(int(*cb)(int,...)) { |
| 1822 |
int result = cb(2, 10, 20); |
| 1823 |
result += cb(3, 30, 40, 50); |
| 1824 |
return result; |
| 1825 |
} |
| 1826 |
""") |
| 1827 |
seen = [] |
| 1828 |
@ffi.callback("int(int, int*)") |
| 1829 |
def python_callback(how_many, values): |
| 1830 |
seen.append([values[i] for i in range(how_many)]) |
| 1831 |
return 42 |
| 1832 |
lib.python_callback = python_callback |
| 1833 |
|
| 1834 |
res = lib.some_c_function(lib.c_callback) |
| 1835 |
assert res == 84 |
| 1836 |
assert seen == [[10, 20], [30, 40, 50]] |
| 1837 |
|
| 1838 |
def test_floatstar_argument(): |
| 1839 |
ffi = FFI() |
| 1840 |
ffi.cdef("float sum3floats(float *);") |
| 1841 |
lib = ffi.verify(""" |
| 1842 |
float sum3floats(float *f) { |
| 1843 |
return f[0] + f[1] + f[2]; |
| 1844 |
} |
| 1845 |
""") |
| 1846 |
assert lib.sum3floats((1.5, 2.5, 3.5)) == 7.5 |
| 1847 |
p = ffi.new("float[]", (1.5, 2.5, 3.5)) |
| 1848 |
assert lib.sum3floats(p) == 7.5 |
| 1849 |
|
| 1850 |
def test_charstar_argument(): |
| 1851 |
ffi = FFI() |
| 1852 |
ffi.cdef("char sum3chars(char *);") |
| 1853 |
lib = ffi.verify(""" |
| 1854 |
char sum3chars(char *f) { |
| 1855 |
return (char)(f[0] + f[1] + f[2]); |
| 1856 |
} |
| 1857 |
""") |
| 1858 |
assert lib.sum3chars((b'\x10', b'\x20', b'\x30')) == b'\x60' |
| 1859 |
p = ffi.new("char[]", b'\x10\x20\x30') |
| 1860 |
assert lib.sum3chars(p) == b'\x60' |
| 1861 |
|
| 1862 |
def test_passing_string_or_NULL(): |
| 1863 |
ffi = FFI() |
| 1864 |
ffi.cdef("int seeme1(char *); int seeme2(int *);") |
| 1865 |
lib = ffi.verify(""" |
| 1866 |
int seeme1(char *x) { |
| 1867 |
return (x == NULL); |
| 1868 |
} |
| 1869 |
int seeme2(int *x) { |
| 1870 |
return (x == NULL); |
| 1871 |
} |
| 1872 |
""") |
| 1873 |
assert lib.seeme1(b"foo") == 0 |
| 1874 |
assert lib.seeme1(ffi.NULL) == 1 |
| 1875 |
assert lib.seeme2([42, 43]) == 0 |
| 1876 |
assert lib.seeme2(ffi.NULL) == 1 |
| 1877 |
pytest.raises(TypeError, lib.seeme1, None) |
| 1878 |
pytest.raises(TypeError, lib.seeme2, None) |
| 1879 |
pytest.raises(TypeError, lib.seeme1, 0.0) |
| 1880 |
pytest.raises(TypeError, lib.seeme2, 0.0) |
| 1881 |
pytest.raises(TypeError, lib.seeme1, 0) |
| 1882 |
pytest.raises(TypeError, lib.seeme2, 0) |
| 1883 |
zeroL = 99999999999999999999 |
| 1884 |
zeroL -= 99999999999999999999 |
| 1885 |
pytest.raises(TypeError, lib.seeme2, zeroL) |
| 1886 |
|
| 1887 |
def test_typeof_function(): |
| 1888 |
ffi = FFI() |
| 1889 |
ffi.cdef("int foo(int, char);") |
| 1890 |
lib = ffi.verify("int foo(int x, char y) { (void)x; (void)y; return 42; }") |
| 1891 |
ctype = ffi.typeof(lib.foo) |
| 1892 |
assert len(ctype.args) == 2 |
| 1893 |
assert ctype.result == ffi.typeof("int") |
| 1894 |
|
| 1895 |
def test_call_with_voidstar_arg(): |
| 1896 |
ffi = FFI() |
| 1897 |
ffi.cdef("int f(void *);") |
| 1898 |
lib = ffi.verify("int f(void *x) { return ((char*)x)[0]; }") |
| 1899 |
assert lib.f(b"foobar") == ord(b"f") |
| 1900 |
|
| 1901 |
def test_dir(): |
| 1902 |
ffi = FFI() |
| 1903 |
ffi.cdef("""void somefunc(void); |
| 1904 |
extern int somevar, somearray[2]; |
| 1905 |
static char *const sv2; |
| 1906 |
enum my_e { AA, BB, ... }; |
| 1907 |
#define FOO ...""") |
| 1908 |
lib = ffi.verify("""void somefunc(void) { } |
| 1909 |
int somevar, somearray[2]; |
| 1910 |
#define sv2 "text" |
| 1911 |
enum my_e { AA, BB }; |
| 1912 |
#define FOO 42""") |
| 1913 |
assert dir(lib) == ['AA', 'BB', 'FOO', 'somearray', |
| 1914 |
'somefunc', 'somevar', 'sv2'] |
| 1915 |
|
| 1916 |
def test_typeof_func_with_struct_argument(): |
| 1917 |
ffi = FFI() |
| 1918 |
ffi.cdef("""struct s { int a; }; int foo(struct s);""") |
| 1919 |
lib = ffi.verify("""struct s { int a; }; |
| 1920 |
int foo(struct s x) { return x.a; }""") |
| 1921 |
s = ffi.new("struct s *", [-1234]) |
| 1922 |
m = lib.foo(s[0]) |
| 1923 |
assert m == -1234 |
| 1924 |
assert repr(ffi.typeof(lib.foo)) == "<ctype 'int(*)(struct s)'>" |
| 1925 |
|
| 1926 |
def test_bug_const_char_ptr_array_1(): |
| 1927 |
ffi = FFI() |
| 1928 |
ffi.cdef("""extern const char *a[...];""") |
| 1929 |
lib = ffi.verify("""const char *a[5];""") |
| 1930 |
assert repr(ffi.typeof(lib.a)) == "<ctype 'char *[5]'>" |
| 1931 |
|
| 1932 |
def test_bug_const_char_ptr_array_2(): |
| 1933 |
ffi = FFI() |
| 1934 |
ffi.cdef("""extern const int a[];""") |
| 1935 |
lib = ffi.verify("""const int a[5];""") |
| 1936 |
assert repr(ffi.typeof(lib.a)) == "<ctype 'int *'>" |
| 1937 |
|
| 1938 |
def _test_various_calls(force_libffi): |
| 1939 |
cdef_source = """ |
| 1940 |
extern int xvalue; |
| 1941 |
extern long long ivalue, rvalue; |
| 1942 |
extern float fvalue; |
| 1943 |
extern double dvalue; |
| 1944 |
extern long double Dvalue; |
| 1945 |
signed char tf_bb(signed char x, signed char c); |
| 1946 |
unsigned char tf_bB(signed char x, unsigned char c); |
| 1947 |
short tf_bh(signed char x, short c); |
| 1948 |
unsigned short tf_bH(signed char x, unsigned short c); |
| 1949 |
int tf_bi(signed char x, int c); |
| 1950 |
unsigned int tf_bI(signed char x, unsigned int c); |
| 1951 |
long tf_bl(signed char x, long c); |
| 1952 |
unsigned long tf_bL(signed char x, unsigned long c); |
| 1953 |
long long tf_bq(signed char x, long long c); |
| 1954 |
unsigned long long tf_bQ(signed char x, unsigned long long c); |
| 1955 |
float tf_bf(signed char x, float c); |
| 1956 |
double tf_bd(signed char x, double c); |
| 1957 |
long double tf_bD(signed char x, long double c); |
| 1958 |
""" |
| 1959 |
if force_libffi: |
| 1960 |
cdef_source = (cdef_source |
| 1961 |
.replace('tf_', '(*const tf_') |
| 1962 |
.replace('(signed char x', ')(signed char x')) |
| 1963 |
ffi = FFI() |
| 1964 |
ffi.cdef(cdef_source) |
| 1965 |
lib = ffi.verify(""" |
| 1966 |
int xvalue; |
| 1967 |
long long ivalue, rvalue; |
| 1968 |
float fvalue; |
| 1969 |
double dvalue; |
| 1970 |
long double Dvalue; |
| 1971 |
|
| 1972 |
typedef signed char b_t; |
| 1973 |
typedef unsigned char B_t; |
| 1974 |
typedef short h_t; |
| 1975 |
typedef unsigned short H_t; |
| 1976 |
typedef int i_t; |
| 1977 |
typedef unsigned int I_t; |
| 1978 |
typedef long l_t; |
| 1979 |
typedef unsigned long L_t; |
| 1980 |
typedef long long q_t; |
| 1981 |
typedef unsigned long long Q_t; |
| 1982 |
typedef float f_t; |
| 1983 |
typedef double d_t; |
| 1984 |
typedef long double D_t; |
| 1985 |
#define S(letter) xvalue = (int)x; letter##value = (letter##_t)c; |
| 1986 |
#define R(letter) return (letter##_t)rvalue; |
| 1987 |
|
| 1988 |
signed char tf_bb(signed char x, signed char c) { S(i) R(b) } |
| 1989 |
unsigned char tf_bB(signed char x, unsigned char c) { S(i) R(B) } |
| 1990 |
short tf_bh(signed char x, short c) { S(i) R(h) } |
| 1991 |
unsigned short tf_bH(signed char x, unsigned short c) { S(i) R(H) } |
| 1992 |
int tf_bi(signed char x, int c) { S(i) R(i) } |
| 1993 |
unsigned int tf_bI(signed char x, unsigned int c) { S(i) R(I) } |
| 1994 |
long tf_bl(signed char x, long c) { S(i) R(l) } |
| 1995 |
unsigned long tf_bL(signed char x, unsigned long c) { S(i) R(L) } |
| 1996 |
long long tf_bq(signed char x, long long c) { S(i) R(q) } |
| 1997 |
unsigned long long tf_bQ(signed char x, unsigned long long c) { S(i) R(Q) } |
| 1998 |
float tf_bf(signed char x, float c) { S(f) R(f) } |
| 1999 |
double tf_bd(signed char x, double c) { S(d) R(d) } |
| 2000 |
long double tf_bD(signed char x, long double c) { S(D) R(D) } |
| 2001 |
""") |
| 2002 |
lib.rvalue = 0x7182838485868788 |
| 2003 |
for kind, cname in [('b', 'signed char'), |
| 2004 |
('B', 'unsigned char'), |
| 2005 |
('h', 'short'), |
| 2006 |
('H', 'unsigned short'), |
| 2007 |
('i', 'int'), |
| 2008 |
('I', 'unsigned int'), |
| 2009 |
('l', 'long'), |
| 2010 |
('L', 'unsigned long'), |
| 2011 |
('q', 'long long'), |
| 2012 |
('Q', 'unsigned long long'), |
| 2013 |
('f', 'float'), |
| 2014 |
('d', 'double'), |
| 2015 |
('D', 'long double')]: |
| 2016 |
sign = +1 if 'unsigned' in cname else -1 |
| 2017 |
lib.xvalue = 0 |
| 2018 |
lib.ivalue = 0 |
| 2019 |
lib.fvalue = 0 |
| 2020 |
lib.dvalue = 0 |
| 2021 |
lib.Dvalue = 0 |
| 2022 |
fun = getattr(lib, 'tf_b' + kind) |
| 2023 |
res = fun(-42, sign * 99) |
| 2024 |
if kind == 'D': |
| 2025 |
res = float(res) |
| 2026 |
assert res == int(ffi.cast(cname, 0x7182838485868788)) |
| 2027 |
assert lib.xvalue == -42 |
| 2028 |
if kind in 'fdD': |
| 2029 |
assert float(getattr(lib, kind + 'value')) == -99.0 |
| 2030 |
else: |
| 2031 |
assert lib.ivalue == sign * 99 |
| 2032 |
|
| 2033 |
def test_various_calls_direct(): |
| 2034 |
_test_various_calls(force_libffi=False) |
| 2035 |
|
| 2036 |
def test_various_calls_libffi(): |
| 2037 |
_test_various_calls(force_libffi=True) |
| 2038 |
|
| 2039 |
def test_ptr_to_opaque(): |
| 2040 |
ffi = FFI() |
| 2041 |
ffi.cdef("typedef ... foo_t; int f1(foo_t*); foo_t *f2(int);") |
| 2042 |
lib = ffi.verify(""" |
| 2043 |
#include <stdlib.h> |
| 2044 |
typedef struct { int x; } foo_t; |
| 2045 |
int f1(foo_t* p) { |
| 2046 |
int x = p->x; |
| 2047 |
free(p); |
| 2048 |
return x; |
| 2049 |
} |
| 2050 |
foo_t *f2(int x) { |
| 2051 |
foo_t *p = malloc(sizeof(foo_t)); |
| 2052 |
p->x = x; |
| 2053 |
return p; |
| 2054 |
} |
| 2055 |
""") |
| 2056 |
p = lib.f2(42) |
| 2057 |
x = lib.f1(p) |
| 2058 |
assert x == 42 |
| 2059 |
|
| 2060 |
def _run_in_multiple_threads(test1): |
| 2061 |
test1() |
| 2062 |
import sys |
| 2063 |
try: |
| 2064 |
import thread |
| 2065 |
except ImportError: |
| 2066 |
import _thread as thread |
| 2067 |
errors = [] |
| 2068 |
def wrapper(lock): |
| 2069 |
try: |
| 2070 |
test1() |
| 2071 |
except: |
| 2072 |
errors.append(sys.exc_info()) |
| 2073 |
lock.release() |
| 2074 |
locks = [] |
| 2075 |
for i in range(10): |
| 2076 |
_lock = thread.allocate_lock() |
| 2077 |
_lock.acquire() |
| 2078 |
thread.start_new_thread(wrapper, (_lock,)) |
| 2079 |
locks.append(_lock) |
| 2080 |
for _lock in locks: |
| 2081 |
_lock.acquire() |
| 2082 |
if errors: |
| 2083 |
raise errors[0][1] |
| 2084 |
|
| 2085 |
def test_errno_working_even_with_pypys_jit(): |
| 2086 |
ffi = FFI() |
| 2087 |
ffi.cdef("int f(int);") |
| 2088 |
lib = ffi.verify(""" |
| 2089 |
#include <errno.h> |
| 2090 |
int f(int x) { return (errno = errno + x); } |
| 2091 |
""") |
| 2092 |
@_run_in_multiple_threads |
| 2093 |
def test1(): |
| 2094 |
ffi.errno = 0 |
| 2095 |
for i in range(10000): |
| 2096 |
e = lib.f(1) |
| 2097 |
assert e == i + 1 |
| 2098 |
assert ffi.errno == e |
| 2099 |
for i in range(10000): |
| 2100 |
ffi.errno = i |
| 2101 |
e = lib.f(42) |
| 2102 |
assert e == i + 42 |
| 2103 |
|
| 2104 |
def test_getlasterror_working_even_with_pypys_jit(): |
| 2105 |
if sys.platform != 'win32': |
| 2106 |
pytest.skip("win32-only test") |
| 2107 |
ffi = FFI() |
| 2108 |
ffi.cdef("void SetLastError(DWORD);") |
| 2109 |
lib = ffi.dlopen("Kernel32.dll") |
| 2110 |
@_run_in_multiple_threads |
| 2111 |
def test1(): |
| 2112 |
for i in range(10000): |
| 2113 |
n = (1 << 29) + i |
| 2114 |
lib.SetLastError(n) |
| 2115 |
assert ffi.getwinerror()[0] == n |
| 2116 |
|
| 2117 |
def test_verify_dlopen_flags(): |
| 2118 |
if not hasattr(sys, 'setdlopenflags'): |
| 2119 |
pytest.skip("requires sys.setdlopenflags()") |
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
old = sys.getdlopenflags() |
| 2125 |
try: |
| 2126 |
ffi1 = FFI() |
| 2127 |
ffi1.cdef("extern int foo_verify_dlopen_flags_1;") |
| 2128 |
sys.setdlopenflags(ffi1.RTLD_GLOBAL | ffi1.RTLD_NOW) |
| 2129 |
lib1 = ffi1.verify("int foo_verify_dlopen_flags_1;") |
| 2130 |
finally: |
| 2131 |
sys.setdlopenflags(old) |
| 2132 |
|
| 2133 |
ffi2 = FFI() |
| 2134 |
ffi2.cdef("int *getptr(void);") |
| 2135 |
lib2 = ffi2.verify(""" |
| 2136 |
extern int foo_verify_dlopen_flags_1; |
| 2137 |
static int *getptr(void) { return &foo_verify_dlopen_flags_1; } |
| 2138 |
""") |
| 2139 |
p = lib2.getptr() |
| 2140 |
assert ffi1.addressof(lib1, 'foo_verify_dlopen_flags_1') == p |
| 2141 |
|
| 2142 |
def test_consider_not_implemented_function_type(): |
| 2143 |
ffi = FFI() |
| 2144 |
ffi.cdef("typedef union { int a; float b; } Data;" |
| 2145 |
"typedef struct { int a:2; } MyStr;" |
| 2146 |
"typedef void (*foofunc_t)(Data);" |
| 2147 |
"typedef Data (*bazfunc_t)(void);" |
| 2148 |
"typedef MyStr (*barfunc_t)(void);") |
| 2149 |
fooptr = ffi.cast("foofunc_t", 123) |
| 2150 |
bazptr = ffi.cast("bazfunc_t", 123) |
| 2151 |
barptr = ffi.cast("barfunc_t", 123) |
| 2152 |
|
| 2153 |
e = pytest.raises(NotImplementedError, fooptr, ffi.new("Data *")) |
| 2154 |
assert str(e.value) == ( |
| 2155 |
"ctype 'Data' not supported as argument by libffi. Unions are only " |
| 2156 |
"supported as argument if the function is 'API mode' and " |
| 2157 |
"non-variadic (i.e. declared inside ffibuilder.cdef()+" |
| 2158 |
"ffibuilder.set_source() and not taking a final '...' argument)") |
| 2159 |
e = pytest.raises(NotImplementedError, bazptr) |
| 2160 |
assert str(e.value) == ( |
| 2161 |
"ctype 'Data' not supported as return value by libffi. Unions are " |
| 2162 |
"only supported as return value if the function is 'API mode' and " |
| 2163 |
"non-variadic (i.e. declared inside ffibuilder.cdef()+" |
| 2164 |
"ffibuilder.set_source() and not taking a final '...' argument)") |
| 2165 |
e = pytest.raises(NotImplementedError, barptr) |
| 2166 |
assert str(e.value) == ( |
| 2167 |
"ctype 'MyStr' not supported as return value. It is a struct with " |
| 2168 |
"bit fields, which libffi does not support. Such structs are only " |
| 2169 |
"supported as return value if the function is 'API mode' and non-" |
| 2170 |
"variadic (i.e. declared inside ffibuilder.cdef()+ffibuilder." |
| 2171 |
"set_source() and not taking a final '...' argument)") |
| 2172 |
|
| 2173 |
def test_verify_extra_arguments(): |
| 2174 |
ffi = FFI() |
| 2175 |
ffi.cdef("#define ABA ...") |
| 2176 |
lib = ffi.verify("", define_macros=[('ABA', '42')]) |
| 2177 |
assert lib.ABA == 42 |
| 2178 |
|
| 2179 |
def test_implicit_unicode_on_windows(): |
| 2180 |
from cffi import FFIError |
| 2181 |
if sys.platform != 'win32': |
| 2182 |
pytest.skip("win32-only test") |
| 2183 |
ffi = FFI() |
| 2184 |
e = pytest.raises(FFIError, ffi.cdef, "int foo(LPTSTR);") |
| 2185 |
assert str(e.value) == ("The Windows type 'LPTSTR' is only available after" |
| 2186 |
" you call ffi.set_unicode()") |
| 2187 |
for with_unicode in [True, False]: |
| 2188 |
ffi = FFI() |
| 2189 |
ffi.set_unicode(with_unicode) |
| 2190 |
ffi.cdef(""" |
| 2191 |
DWORD GetModuleFileName(HMODULE hModule, LPTSTR lpFilename, |
| 2192 |
DWORD nSize); |
| 2193 |
""") |
| 2194 |
lib = ffi.verify(""" |
| 2195 |
#include <windows.h> |
| 2196 |
""", libraries=['Kernel32']) |
| 2197 |
outbuf = ffi.new("TCHAR[]", 200) |
| 2198 |
n = lib.GetModuleFileName(ffi.NULL, outbuf, 500) |
| 2199 |
assert 0 < n < 500 |
| 2200 |
for i in range(n): |
| 2201 |
|
| 2202 |
assert ord(outbuf[i]) != 0 |
| 2203 |
assert ord(outbuf[n]) == 0 |
| 2204 |
assert ord(outbuf[0]) < 128 |
| 2205 |
|
| 2206 |
def test_define_known_value(): |
| 2207 |
ffi = FFI() |
| 2208 |
ffi.cdef("#define FOO 0x123") |
| 2209 |
lib = ffi.verify("#define FOO 0x123") |
| 2210 |
assert lib.FOO == 0x123 |
| 2211 |
|
| 2212 |
def test_define_wrong_value(): |
| 2213 |
ffi = FFI() |
| 2214 |
ffi.cdef("#define FOO 123") |
| 2215 |
lib = ffi.verify("#define FOO 124") |
| 2216 |
with pytest.raises(ffi.error) as e: |
| 2217 |
lib.FOO |
| 2218 |
assert str(e.value) == ("the C compiler says 'FOO' is equal to 124 (0x7c)," |
| 2219 |
" but the cdef disagrees") |
| 2220 |
|
| 2221 |
def test_some_integer_type_for_issue73(): |
| 2222 |
ffi = FFI() |
| 2223 |
ffi.cdef(""" |
| 2224 |
typedef int... AnIntegerWith32Bits; |
| 2225 |
typedef AnIntegerWith32Bits (*AFunctionReturningInteger) (void); |
| 2226 |
AnIntegerWith32Bits InvokeFunction(AFunctionReturningInteger); |
| 2227 |
""") |
| 2228 |
lib = ffi.verify(""" |
| 2229 |
#ifdef __LP64__ |
| 2230 |
typedef int AnIntegerWith32Bits; |
| 2231 |
#else |
| 2232 |
typedef long AnIntegerWith32Bits; |
| 2233 |
#endif |
| 2234 |
typedef AnIntegerWith32Bits (*AFunctionReturningInteger) (void); |
| 2235 |
AnIntegerWith32Bits InvokeFunction(AFunctionReturningInteger f) { |
| 2236 |
return f(); |
| 2237 |
} |
| 2238 |
""") |
| 2239 |
@ffi.callback("AFunctionReturningInteger") |
| 2240 |
def add(): |
| 2241 |
return 3 + 4 |
| 2242 |
x = lib.InvokeFunction(add) |
| 2243 |
assert x == 7 |
| 2244 |
|
| 2245 |
def test_unsupported_some_primitive_types(): |
| 2246 |
ffi = FFI() |
| 2247 |
pytest.raises((FFIError, |
| 2248 |
CDefError), |
| 2249 |
ffi.cdef, """typedef void... foo_t;""") |
| 2250 |
|
| 2251 |
ffi.cdef("typedef int... foo_t;") |
| 2252 |
pytest.raises(VerificationError, ffi.verify, "typedef float foo_t;") |
| 2253 |
|
| 2254 |
def test_windows_dllimport_data(): |
| 2255 |
if sys.platform != 'win32': |
| 2256 |
pytest.skip("Windows only") |
| 2257 |
from extra_tests.cffi_tests.udir import udir |
| 2258 |
tmpfile = udir.join('dllimport_data.c') |
| 2259 |
tmpfile.write('int my_value = 42;\n') |
| 2260 |
ffi = FFI() |
| 2261 |
ffi.cdef("int my_value;") |
| 2262 |
lib = ffi.verify("extern __declspec(dllimport) int my_value;", |
| 2263 |
sources = [str(tmpfile)]) |
| 2264 |
assert lib.my_value == 42 |
| 2265 |
|
| 2266 |
def test_macro_var(): |
| 2267 |
ffi = FFI() |
| 2268 |
ffi.cdef("extern int myarray[50], my_value;") |
| 2269 |
lib = ffi.verify(""" |
| 2270 |
int myarray[50]; |
| 2271 |
int *get_my_value(void) { |
| 2272 |
static int index = 0; |
| 2273 |
return &myarray[index++]; |
| 2274 |
} |
| 2275 |
#define my_value (*get_my_value()) |
| 2276 |
""") |
| 2277 |
assert lib.my_value == 0 |
| 2278 |
lib.my_value = 42 |
| 2279 |
assert lib.myarray[1] == 42 |
| 2280 |
assert lib.my_value == 0 |
| 2281 |
lib.myarray[3] = 63 |
| 2282 |
assert lib.my_value == 63 |
| 2283 |
p = ffi.addressof(lib, 'my_value') |
| 2284 |
assert p[-1] == 63 |
| 2285 |
assert p[0] == 0 |
| 2286 |
assert p == lib.myarray + 4 |
| 2287 |
p[1] = 82 |
| 2288 |
assert lib.my_value == 82 |
| 2289 |
|
| 2290 |
def test_const_pointer_to_pointer(): |
| 2291 |
ffi = FFI() |
| 2292 |
ffi.cdef("struct s { char *const *a; };") |
| 2293 |
ffi.verify("struct s { char *const *a; };") |
| 2294 |
|
| 2295 |
def test_share_FILE(): |
| 2296 |
ffi1 = FFI() |
| 2297 |
ffi1.cdef("void do_stuff(FILE *);") |
| 2298 |
lib1 = ffi1.verify("void do_stuff(FILE *f) { (void)f; }") |
| 2299 |
ffi2 = FFI() |
| 2300 |
ffi2.cdef("FILE *barize(void);") |
| 2301 |
lib2 = ffi2.verify("FILE *barize(void) { return NULL; }") |
| 2302 |
lib1.do_stuff(lib2.barize()) |
| 2303 |
|
| 2304 |
def test_win_common_types(): |
| 2305 |
if sys.platform != 'win32': |
| 2306 |
pytest.skip("Windows only") |
| 2307 |
ffi = FFI() |
| 2308 |
ffi.set_unicode(True) |
| 2309 |
ffi.verify("") |
| 2310 |
assert ffi.typeof("PBYTE") is ffi.typeof("unsigned char *") |
| 2311 |
if sys.maxsize > 2**32: |
| 2312 |
expected = "unsigned long long" |
| 2313 |
else: |
| 2314 |
expected = "unsigned int" |
| 2315 |
assert ffi.typeof("UINT_PTR") is ffi.typeof(expected) |
| 2316 |
assert ffi.typeof("PTSTR") is ffi.typeof("wchar_t *") |
| 2317 |
|
| 2318 |
def _only_test_on_linux_intel(): |
| 2319 |
if not sys.platform.startswith('linux'): |
| 2320 |
pytest.skip('only running the memory-intensive test on Linux') |
| 2321 |
import platform |
| 2322 |
machine = platform.machine() |
| 2323 |
if 'x86' not in machine and 'x64' not in machine: |
| 2324 |
pytest.skip('only running the memory-intensive test on x86/x64') |
| 2325 |
|
| 2326 |
def test_ffi_gc_size_arg(): |
| 2327 |
_only_test_on_linux_intel() |
| 2328 |
ffi = FFI() |
| 2329 |
ffi.cdef("void *malloc(size_t); void free(void *);") |
| 2330 |
lib = ffi.verify(r""" |
| 2331 |
#include <stdlib.h> |
| 2332 |
""") |
| 2333 |
for i in range(2000): |
| 2334 |
p = lib.malloc(20*1024*1024) |
| 2335 |
p1 = ffi.cast("char *", p) |
| 2336 |
for j in range(0, 20*1024*1024, 4096): |
| 2337 |
p1[j] = b'!' |
| 2338 |
p = ffi.gc(p, lib.free, 20*1024*1024) |
| 2339 |
del p |
| 2340 |
|
| 2341 |
|
| 2342 |
|
| 2343 |
def test_ffi_gc_size_arg_2(): |
| 2344 |
|
| 2345 |
|
| 2346 |
|
| 2347 |
if '__pypy__' not in sys.builtin_module_names: |
| 2348 |
pytest.skip("find a way to tweak the cyclic GC of CPython") |
| 2349 |
_only_test_on_linux_intel() |
| 2350 |
ffi = FFI() |
| 2351 |
ffi.cdef("void *malloc(size_t); void free(void *);") |
| 2352 |
lib = ffi.verify(r""" |
| 2353 |
#include <stdlib.h> |
| 2354 |
""") |
| 2355 |
class X(object): |
| 2356 |
pass |
| 2357 |
for i in range(2000): |
| 2358 |
p = lib.malloc(50*1024*1024) |
| 2359 |
p1 = ffi.cast("char *", p) |
| 2360 |
for j in range(0, 50*1024*1024, 4096): |
| 2361 |
p1[j] = b'!' |
| 2362 |
p = ffi.gc(p, lib.free, 50*1024*1024) |
| 2363 |
x = X() |
| 2364 |
x.p = p |
| 2365 |
x.cyclic = x |
| 2366 |
del p, x |
| 2367 |
|
| 2368 |
def test_ffi_new_with_cycles(): |
| 2369 |
|
| 2370 |
if '__pypy__' not in sys.builtin_module_names: |
| 2371 |
pytest.skip("find a way to tweak the cyclic GC of CPython") |
| 2372 |
ffi = FFI() |
| 2373 |
ffi.cdef("") |
| 2374 |
lib = ffi.verify("") |
| 2375 |
class X(object): |
| 2376 |
pass |
| 2377 |
for i in range(2000): |
| 2378 |
p = ffi.new("char[]", 50*1024*1024) |
| 2379 |
for j in range(0, 50*1024*1024, 4096): |
| 2380 |
p[j] = b'!' |
| 2381 |
x = X() |
| 2382 |
x.p = p |
| 2383 |
x.cyclic = x |
| 2384 |
del p, x |