| 1 |
|
| 2 |
import pytest |
| 3 |
from cffi import FFI, CDefError |
| 4 |
import math, os, sys |
| 5 |
import ctypes.util |
| 6 |
from cffi.backend_ctypes import CTypesBackend |
| 7 |
from extra_tests.cffi_tests.udir import udir |
| 8 |
from extra_tests.cffi_tests.support import FdWriteCapture, StdErrCapture, is_musl |
| 9 |
from .backend_tests import needs_dlopen_none |
| 10 |
|
| 11 |
try: |
| 12 |
from StringIO import StringIO |
| 13 |
except ImportError: |
| 14 |
from io import StringIO |
| 15 |
|
| 16 |
try: |
| 17 |
from packaging.tags import platform_tags |
| 18 |
_platform_tags_cached = set(platform_tags()) |
| 19 |
_is_musl = any(t.startswith('musllinux') for t in _platform_tags_cached) |
| 20 |
except ImportError: |
| 21 |
_is_musl = False |
| 22 |
|
| 23 |
lib_m = 'm' |
| 24 |
if sys.platform == 'win32': |
| 25 |
|
| 26 |
import distutils.ccompiler |
| 27 |
if distutils.ccompiler.get_default_compiler() == 'msvc': |
| 28 |
lib_m = 'msvcrt' |
| 29 |
elif is_musl: |
| 30 |
lib_m = 'c' |
| 31 |
|
| 32 |
class TestFunction(object): |
| 33 |
Backend = CTypesBackend |
| 34 |
|
| 35 |
def test_sin(self): |
| 36 |
ffi = FFI(backend=self.Backend()) |
| 37 |
ffi.cdef(""" |
| 38 |
double sin(double x); |
| 39 |
""") |
| 40 |
m = ffi.dlopen(lib_m) |
| 41 |
x = m.sin(1.23) |
| 42 |
assert x == math.sin(1.23) |
| 43 |
|
| 44 |
def test_sinf(self): |
| 45 |
if sys.platform == 'win32': |
| 46 |
pytest.skip("no sinf found in the Windows stdlib") |
| 47 |
ffi = FFI(backend=self.Backend()) |
| 48 |
ffi.cdef(""" |
| 49 |
float sinf(float x); |
| 50 |
""") |
| 51 |
m = ffi.dlopen(lib_m) |
| 52 |
x = m.sinf(1.23) |
| 53 |
assert type(x) is float |
| 54 |
assert x != math.sin(1.23) |
| 55 |
assert abs(x - math.sin(1.23)) < 1E-6 |
| 56 |
|
| 57 |
def test_getenv_no_return_value(self): |
| 58 |
|
| 59 |
ffi = FFI(backend=self.Backend()) |
| 60 |
ffi.cdef(""" |
| 61 |
void getenv(char *); |
| 62 |
""") |
| 63 |
needs_dlopen_none() |
| 64 |
m = ffi.dlopen(None) |
| 65 |
x = m.getenv(b"FOO") |
| 66 |
assert x is None |
| 67 |
|
| 68 |
def test_dlopen_filename(self): |
| 69 |
path = ctypes.util.find_library(lib_m) |
| 70 |
if not path: |
| 71 |
pytest.skip("%s not found" % lib_m) |
| 72 |
ffi = FFI(backend=self.Backend()) |
| 73 |
ffi.cdef(""" |
| 74 |
double cos(double x); |
| 75 |
""") |
| 76 |
m = ffi.dlopen(path) |
| 77 |
x = m.cos(1.23) |
| 78 |
assert x == math.cos(1.23) |
| 79 |
|
| 80 |
m = ffi.dlopen(os.path.basename(path)) |
| 81 |
x = m.cos(1.23) |
| 82 |
assert x == math.cos(1.23) |
| 83 |
|
| 84 |
def test_dlopen_flags(self): |
| 85 |
ffi = FFI(backend=self.Backend()) |
| 86 |
ffi.cdef(""" |
| 87 |
double cos(double x); |
| 88 |
""") |
| 89 |
m = ffi.dlopen(lib_m, ffi.RTLD_LAZY | ffi.RTLD_LOCAL) |
| 90 |
x = m.cos(1.23) |
| 91 |
assert x == math.cos(1.23) |
| 92 |
|
| 93 |
def test_dlopen_constant(self): |
| 94 |
ffi = FFI(backend=self.Backend()) |
| 95 |
ffi.cdef(""" |
| 96 |
#define FOOBAR 42 |
| 97 |
static const float baz = 42.5; /* not visible */ |
| 98 |
double sin(double x); |
| 99 |
""") |
| 100 |
m = ffi.dlopen(lib_m) |
| 101 |
assert m.FOOBAR == 42 |
| 102 |
with pytest.raises(NotImplementedError): |
| 103 |
m.baz |
| 104 |
|
| 105 |
def test_tlsalloc(self): |
| 106 |
if sys.platform != 'win32': |
| 107 |
pytest.skip("win32 only") |
| 108 |
if self.Backend is CTypesBackend: |
| 109 |
pytest.skip("ctypes complains on wrong calling conv") |
| 110 |
ffi = FFI(backend=self.Backend()) |
| 111 |
ffi.cdef("long TlsAlloc(void); int TlsFree(long);") |
| 112 |
lib = ffi.dlopen('KERNEL32.DLL') |
| 113 |
x = lib.TlsAlloc() |
| 114 |
assert x != 0 |
| 115 |
y = lib.TlsFree(x) |
| 116 |
assert y != 0 |
| 117 |
|
| 118 |
def test_fputs(self): |
| 119 |
if not sys.platform.startswith('linux'): |
| 120 |
pytest.skip("probably no symbol 'stderr' in the lib") |
| 121 |
ffi = FFI(backend=self.Backend()) |
| 122 |
ffi.cdef(""" |
| 123 |
int fputs(const char *, void *); |
| 124 |
extern void *stderr; |
| 125 |
""") |
| 126 |
needs_dlopen_none() |
| 127 |
ffi.C = ffi.dlopen(None) |
| 128 |
ffi.C.fputs |
| 129 |
with FdWriteCapture() as fd: |
| 130 |
ffi.C.fputs(b"hello\n", ffi.C.stderr) |
| 131 |
ffi.C.fputs(b" world\n", ffi.C.stderr) |
| 132 |
res = fd.getvalue() |
| 133 |
assert res == b'hello\n world\n' |
| 134 |
|
| 135 |
def test_fputs_without_const(self): |
| 136 |
if not sys.platform.startswith('linux'): |
| 137 |
pytest.skip("probably no symbol 'stderr' in the lib") |
| 138 |
ffi = FFI(backend=self.Backend()) |
| 139 |
ffi.cdef(""" |
| 140 |
int fputs(char *, void *); |
| 141 |
extern void *stderr; |
| 142 |
""") |
| 143 |
needs_dlopen_none() |
| 144 |
ffi.C = ffi.dlopen(None) |
| 145 |
ffi.C.fputs |
| 146 |
with FdWriteCapture() as fd: |
| 147 |
ffi.C.fputs(b"hello\n", ffi.C.stderr) |
| 148 |
ffi.C.fputs(b" world\n", ffi.C.stderr) |
| 149 |
res = fd.getvalue() |
| 150 |
assert res == b'hello\n world\n' |
| 151 |
|
| 152 |
def test_vararg(self): |
| 153 |
if not sys.platform.startswith('linux'): |
| 154 |
pytest.skip("probably no symbol 'stderr' in the lib") |
| 155 |
ffi = FFI(backend=self.Backend()) |
| 156 |
ffi.cdef(""" |
| 157 |
int fprintf(void *, const char *format, ...); |
| 158 |
extern void *stderr; |
| 159 |
""") |
| 160 |
needs_dlopen_none() |
| 161 |
ffi.C = ffi.dlopen(None) |
| 162 |
with FdWriteCapture() as fd: |
| 163 |
ffi.C.fprintf(ffi.C.stderr, b"hello with no arguments\n") |
| 164 |
ffi.C.fprintf(ffi.C.stderr, |
| 165 |
b"hello, %s!\n", ffi.new("char[]", b"world")) |
| 166 |
ffi.C.fprintf(ffi.C.stderr, |
| 167 |
ffi.new("char[]", b"hello, %s!\n"), |
| 168 |
ffi.new("char[]", b"world2")) |
| 169 |
ffi.C.fprintf(ffi.C.stderr, |
| 170 |
b"hello int %d long %ld long long %lld\n", |
| 171 |
ffi.cast("int", 42), |
| 172 |
ffi.cast("long", 84), |
| 173 |
ffi.cast("long long", 168)) |
| 174 |
ffi.C.fprintf(ffi.C.stderr, b"hello %p\n", ffi.NULL) |
| 175 |
res = fd.getvalue() |
| 176 |
if is_musl: |
| 177 |
nil_repr = b'0' |
| 178 |
else: |
| 179 |
nil_repr = b'(nil)' |
| 180 |
assert res == (b"hello with no arguments\n" |
| 181 |
b"hello, world!\n" |
| 182 |
b"hello, world2!\n" |
| 183 |
b"hello int 42 long 84 long long 168\n" |
| 184 |
b"hello " + nil_repr + b"\n") |
| 185 |
|
| 186 |
def test_must_specify_type_of_vararg(self): |
| 187 |
ffi = FFI(backend=self.Backend()) |
| 188 |
ffi.cdef(""" |
| 189 |
int printf(const char *format, ...); |
| 190 |
""") |
| 191 |
needs_dlopen_none() |
| 192 |
ffi.C = ffi.dlopen(None) |
| 193 |
e = pytest.raises(TypeError, ffi.C.printf, b"hello %d\n", 42) |
| 194 |
assert str(e.value) == ("argument 2 passed in the variadic part " |
| 195 |
"needs to be a cdata object (got int)") |
| 196 |
|
| 197 |
def test_function_has_a_c_type(self): |
| 198 |
ffi = FFI(backend=self.Backend()) |
| 199 |
ffi.cdef(""" |
| 200 |
int puts(const char *); |
| 201 |
""") |
| 202 |
needs_dlopen_none() |
| 203 |
ffi.C = ffi.dlopen(None) |
| 204 |
fptr = ffi.C.puts |
| 205 |
assert ffi.typeof(fptr) == ffi.typeof("int(*)(const char*)") |
| 206 |
if self.Backend is CTypesBackend: |
| 207 |
assert repr(fptr).startswith("<cdata 'int puts(char *)' 0x") |
| 208 |
|
| 209 |
def test_function_pointer(self): |
| 210 |
ffi = FFI(backend=self.Backend()) |
| 211 |
def cb(charp): |
| 212 |
assert repr(charp).startswith("<cdata 'char *' 0x") |
| 213 |
return 42 |
| 214 |
fptr = ffi.callback("int(*)(const char *txt)", cb) |
| 215 |
assert fptr != ffi.callback("int(*)(const char *)", cb) |
| 216 |
assert repr(fptr) == "<cdata 'int(*)(char *)' calling %r>" % (cb,) |
| 217 |
res = fptr(b"Hello") |
| 218 |
assert res == 42 |
| 219 |
|
| 220 |
if not sys.platform.startswith('linux'): |
| 221 |
pytest.skip("probably no symbol 'stderr' in the lib") |
| 222 |
ffi.cdef(""" |
| 223 |
int fputs(const char *, void *); |
| 224 |
extern void *stderr; |
| 225 |
""") |
| 226 |
needs_dlopen_none() |
| 227 |
ffi.C = ffi.dlopen(None) |
| 228 |
fptr = ffi.cast("int(*)(const char *txt, void *)", ffi.C.fputs) |
| 229 |
assert fptr == ffi.C.fputs |
| 230 |
assert repr(fptr).startswith("<cdata 'int(*)(char *, void *)' 0x") |
| 231 |
with FdWriteCapture() as fd: |
| 232 |
fptr(b"world\n", ffi.C.stderr) |
| 233 |
res = fd.getvalue() |
| 234 |
assert res == b'world\n' |
| 235 |
|
| 236 |
def test_callback_returning_void(self): |
| 237 |
ffi = FFI(backend=self.Backend()) |
| 238 |
for returnvalue in [None, 42]: |
| 239 |
def cb(): |
| 240 |
return returnvalue |
| 241 |
fptr = ffi.callback("void(*)(void)", cb) |
| 242 |
with StdErrCapture() as f: |
| 243 |
returned = fptr() |
| 244 |
printed = f.getvalue() |
| 245 |
assert returned is None |
| 246 |
if returnvalue is None: |
| 247 |
assert printed == '' |
| 248 |
else: |
| 249 |
assert "None" in printed |
| 250 |
|
| 251 |
def test_callback_returning_struct_three_bytes(self): |
| 252 |
if self.Backend is CTypesBackend: |
| 253 |
pytest.skip("not supported with the ctypes backend") |
| 254 |
ffi = FFI(backend=self.Backend()) |
| 255 |
ffi.cdef(""" |
| 256 |
typedef struct { |
| 257 |
unsigned char a, b, c; |
| 258 |
} THREEBYTES; |
| 259 |
""") |
| 260 |
def cb(): |
| 261 |
return (12, 34, 56) |
| 262 |
fptr = ffi.callback("THREEBYTES(*)(void)", cb) |
| 263 |
tb = fptr() |
| 264 |
assert tb.a == 12 |
| 265 |
assert tb.b == 34 |
| 266 |
assert tb.c == 56 |
| 267 |
|
| 268 |
def test_passing_array(self): |
| 269 |
ffi = FFI(backend=self.Backend()) |
| 270 |
ffi.cdef(""" |
| 271 |
int strlen(char[]); |
| 272 |
""") |
| 273 |
needs_dlopen_none() |
| 274 |
ffi.C = ffi.dlopen(None) |
| 275 |
p = ffi.new("char[]", b"hello") |
| 276 |
res = ffi.C.strlen(p) |
| 277 |
assert res == 5 |
| 278 |
|
| 279 |
def test_write_variable(self): |
| 280 |
if not sys.platform.startswith('linux') or _is_musl: |
| 281 |
pytest.skip("probably no symbol 'stdout' in the lib") |
| 282 |
ffi = FFI(backend=self.Backend()) |
| 283 |
ffi.cdef(""" |
| 284 |
extern void *stdout; |
| 285 |
""") |
| 286 |
needs_dlopen_none() |
| 287 |
C = ffi.dlopen(None) |
| 288 |
pout = C.stdout |
| 289 |
C.stdout = ffi.NULL |
| 290 |
assert C.stdout == ffi.NULL |
| 291 |
C.stdout = pout |
| 292 |
assert C.stdout == pout |
| 293 |
|
| 294 |
def test_strchr(self): |
| 295 |
ffi = FFI(backend=self.Backend()) |
| 296 |
ffi.cdef(""" |
| 297 |
char *strchr(const char *s, int c); |
| 298 |
""") |
| 299 |
needs_dlopen_none() |
| 300 |
ffi.C = ffi.dlopen(None) |
| 301 |
p = ffi.new("char[]", b"hello world!") |
| 302 |
q = ffi.C.strchr(p, ord('w')) |
| 303 |
assert ffi.string(q) == b"world!" |
| 304 |
|
| 305 |
def test_function_with_struct_argument(self): |
| 306 |
if sys.platform == 'win32': |
| 307 |
pytest.skip("no 'inet_ntoa'") |
| 308 |
if (self.Backend is CTypesBackend and |
| 309 |
'__pypy__' in sys.builtin_module_names): |
| 310 |
pytest.skip("ctypes limitation on pypy") |
| 311 |
ffi = FFI(backend=self.Backend()) |
| 312 |
ffi.cdef(""" |
| 313 |
struct in_addr { unsigned int s_addr; }; |
| 314 |
char *inet_ntoa(struct in_addr in); |
| 315 |
""") |
| 316 |
needs_dlopen_none() |
| 317 |
ffi.C = ffi.dlopen(None) |
| 318 |
ina = ffi.new("struct in_addr *", [0x04040404]) |
| 319 |
a = ffi.C.inet_ntoa(ina[0]) |
| 320 |
assert ffi.string(a) == b'4.4.4.4' |
| 321 |
|
| 322 |
def test_function_typedef(self): |
| 323 |
ffi = FFI(backend=self.Backend()) |
| 324 |
ffi.cdef(""" |
| 325 |
typedef double func_t(double); |
| 326 |
func_t sin; |
| 327 |
""") |
| 328 |
m = ffi.dlopen(lib_m) |
| 329 |
x = m.sin(1.23) |
| 330 |
assert x == math.sin(1.23) |
| 331 |
|
| 332 |
def test_fputs_custom_FILE(self): |
| 333 |
if self.Backend is CTypesBackend: |
| 334 |
pytest.skip("FILE not supported with the ctypes backend") |
| 335 |
filename = str(udir.join('fputs_custom_FILE')) |
| 336 |
ffi = FFI(backend=self.Backend()) |
| 337 |
ffi.cdef("int fputs(const char *, FILE *);") |
| 338 |
needs_dlopen_none() |
| 339 |
C = ffi.dlopen(None) |
| 340 |
with open(filename, 'wb') as f: |
| 341 |
f.write(b'[') |
| 342 |
C.fputs(b"hello from custom file", f) |
| 343 |
f.write(b'][') |
| 344 |
C.fputs(b"some more output", f) |
| 345 |
f.write(b']') |
| 346 |
with open(filename, 'rb') as f: |
| 347 |
res = f.read() |
| 348 |
assert res == b'[hello from custom file][some more output]' |
| 349 |
|
| 350 |
def test_constants_on_lib(self): |
| 351 |
ffi = FFI(backend=self.Backend()) |
| 352 |
ffi.cdef("""enum foo_e { AA, BB, CC=5, DD }; |
| 353 |
typedef enum { EE=-5, FF } some_enum_t;""") |
| 354 |
needs_dlopen_none() |
| 355 |
lib = ffi.dlopen(None) |
| 356 |
assert lib.AA == 0 |
| 357 |
assert lib.BB == 1 |
| 358 |
assert lib.CC == 5 |
| 359 |
assert lib.DD == 6 |
| 360 |
assert lib.EE == -5 |
| 361 |
assert lib.FF == -4 |
| 362 |
|
| 363 |
def test_void_star_accepts_string(self): |
| 364 |
ffi = FFI(backend=self.Backend()) |
| 365 |
ffi.cdef("""int strlen(const void *);""") |
| 366 |
needs_dlopen_none() |
| 367 |
lib = ffi.dlopen(None) |
| 368 |
res = lib.strlen(b"hello") |
| 369 |
assert res == 5 |
| 370 |
|
| 371 |
def test_signed_char_star_accepts_string(self): |
| 372 |
if self.Backend is CTypesBackend: |
| 373 |
pytest.skip("not supported by the ctypes backend") |
| 374 |
ffi = FFI(backend=self.Backend()) |
| 375 |
ffi.cdef("""int strlen(signed char *);""") |
| 376 |
needs_dlopen_none() |
| 377 |
lib = ffi.dlopen(None) |
| 378 |
res = lib.strlen(b"hello") |
| 379 |
assert res == 5 |
| 380 |
|
| 381 |
def test_unsigned_char_star_accepts_string(self): |
| 382 |
if self.Backend is CTypesBackend: |
| 383 |
pytest.skip("not supported by the ctypes backend") |
| 384 |
ffi = FFI(backend=self.Backend()) |
| 385 |
ffi.cdef("""int strlen(unsigned char *);""") |
| 386 |
needs_dlopen_none() |
| 387 |
lib = ffi.dlopen(None) |
| 388 |
res = lib.strlen(b"hello") |
| 389 |
assert res == 5 |
| 390 |
|
| 391 |
def test_missing_function(self): |
| 392 |
ffi = FFI(backend=self.Backend()) |
| 393 |
ffi.cdef(""" |
| 394 |
int nonexistent(); |
| 395 |
""") |
| 396 |
m = ffi.dlopen(lib_m) |
| 397 |
assert not hasattr(m, 'nonexistent') |
| 398 |
|
| 399 |
def test_wraps_from_stdlib(self): |
| 400 |
import functools |
| 401 |
ffi = FFI(backend=self.Backend()) |
| 402 |
ffi.cdef(""" |
| 403 |
double sin(double x); |
| 404 |
""") |
| 405 |
def my_decorator(f): |
| 406 |
@functools.wraps(f) |
| 407 |
def wrapper(*args): |
| 408 |
return f(*args) + 100 |
| 409 |
return wrapper |
| 410 |
m = ffi.dlopen(lib_m) |
| 411 |
sin100 = my_decorator(m.sin) |
| 412 |
x = sin100(1.23) |
| 413 |
assert x == math.sin(1.23) + 100 |
| 414 |
|
| 415 |
def test_free_callback_cycle(self): |
| 416 |
if self.Backend is CTypesBackend: |
| 417 |
pytest.skip("seems to fail with the ctypes backend on windows") |
| 418 |
import weakref |
| 419 |
def make_callback(data): |
| 420 |
container = [data] |
| 421 |
callback = ffi.callback('int()', lambda: len(container)) |
| 422 |
container.append(callback) |
| 423 |
|
| 424 |
return callback |
| 425 |
|
| 426 |
class Data(object): |
| 427 |
pass |
| 428 |
ffi = FFI(backend=self.Backend()) |
| 429 |
data = Data() |
| 430 |
callback = make_callback(data) |
| 431 |
wr = weakref.ref(data) |
| 432 |
del callback, data |
| 433 |
for i in range(3): |
| 434 |
if wr() is not None: |
| 435 |
import gc; gc.collect() |
| 436 |
assert wr() is None |
| 437 |
|
| 438 |
def test_windows_stdcall(self): |
| 439 |
if sys.platform != 'win32': |
| 440 |
pytest.skip("Windows-only test") |
| 441 |
if self.Backend is CTypesBackend: |
| 442 |
pytest.skip("not with the ctypes backend") |
| 443 |
ffi = FFI(backend=self.Backend()) |
| 444 |
ffi.cdef(""" |
| 445 |
BOOL QueryPerformanceFrequency(LONGLONG *lpFrequency); |
| 446 |
""") |
| 447 |
m = ffi.dlopen("Kernel32.dll") |
| 448 |
p_freq = ffi.new("LONGLONG *") |
| 449 |
res = m.QueryPerformanceFrequency(p_freq) |
| 450 |
assert res != 0 |
| 451 |
assert p_freq[0] != 0 |
| 452 |
|
| 453 |
def test_explicit_cdecl_stdcall(self): |
| 454 |
if sys.platform != 'win32': |
| 455 |
pytest.skip("Windows-only test") |
| 456 |
if self.Backend is CTypesBackend: |
| 457 |
pytest.skip("not with the ctypes backend") |
| 458 |
win64 = (sys.maxsize > 2**32) |
| 459 |
|
| 460 |
ffi = FFI(backend=self.Backend()) |
| 461 |
ffi.cdef(""" |
| 462 |
BOOL QueryPerformanceFrequency(LONGLONG *lpFrequency); |
| 463 |
""") |
| 464 |
m = ffi.dlopen("Kernel32.dll") |
| 465 |
tp = ffi.typeof(m.QueryPerformanceFrequency) |
| 466 |
assert str(tp) == "<ctype 'int(*)(long long *)'>" |
| 467 |
|
| 468 |
ffi = FFI(backend=self.Backend()) |
| 469 |
ffi.cdef(""" |
| 470 |
BOOL __cdecl QueryPerformanceFrequency(LONGLONG *lpFrequency); |
| 471 |
""") |
| 472 |
m = ffi.dlopen("Kernel32.dll") |
| 473 |
tpc = ffi.typeof(m.QueryPerformanceFrequency) |
| 474 |
assert tpc is tp |
| 475 |
|
| 476 |
ffi = FFI(backend=self.Backend()) |
| 477 |
ffi.cdef(""" |
| 478 |
BOOL WINAPI QueryPerformanceFrequency(LONGLONG *lpFrequency); |
| 479 |
""") |
| 480 |
m = ffi.dlopen("Kernel32.dll") |
| 481 |
tps = ffi.typeof(m.QueryPerformanceFrequency) |
| 482 |
if win64: |
| 483 |
assert tps is tpc |
| 484 |
else: |
| 485 |
assert tps is not tpc |
| 486 |
assert str(tps) == "<ctype 'int(__stdcall *)(long long *)'>" |
| 487 |
|
| 488 |
ffi = FFI(backend=self.Backend()) |
| 489 |
ffi.cdef("typedef int (__cdecl *fnc_t)(int);") |
| 490 |
ffi.cdef("typedef int (__stdcall *fns_t)(int);") |
| 491 |
tpc = ffi.typeof("fnc_t") |
| 492 |
tps = ffi.typeof("fns_t") |
| 493 |
assert str(tpc) == "<ctype 'int(*)(int)'>" |
| 494 |
if win64: |
| 495 |
assert tps is tpc |
| 496 |
else: |
| 497 |
assert str(tps) == "<ctype 'int(__stdcall *)(int)'>" |
| 498 |
|
| 499 |
fnc = ffi.cast("fnc_t", 0) |
| 500 |
fns = ffi.cast("fns_t", 0) |
| 501 |
ffi.new("fnc_t[]", [fnc]) |
| 502 |
if not win64: |
| 503 |
pytest.raises(TypeError, ffi.new, "fnc_t[]", [fns]) |
| 504 |
pytest.raises(TypeError, ffi.new, "fns_t[]", [fnc]) |
| 505 |
ffi.new("fns_t[]", [fns]) |
| 506 |
|
| 507 |
def test_stdcall_only_on_windows(self): |
| 508 |
ffi = FFI(backend=self.Backend()) |
| 509 |
ffi.cdef("double __stdcall sin(double x);") |
| 510 |
m = ffi.dlopen(lib_m) |
| 511 |
if (sys.platform == 'win32' and sys.maxsize < 2**32 and |
| 512 |
self.Backend is not CTypesBackend): |
| 513 |
assert "double(__stdcall *)(double)" in str(ffi.typeof(m.sin)) |
| 514 |
else: |
| 515 |
assert "double(*)(double)" in str(ffi.typeof(m.sin)) |
| 516 |
x = m.sin(1.23) |
| 517 |
assert x == math.sin(1.23) |
| 518 |
|
| 519 |
def test_dir_on_dlopen_lib(self): |
| 520 |
ffi = FFI(backend=self.Backend()) |
| 521 |
ffi.cdef(""" |
| 522 |
typedef enum { MYE1, MYE2 } myenum_t; |
| 523 |
double myfunc(double); |
| 524 |
extern double myvar; |
| 525 |
const double myconst; |
| 526 |
#define MYFOO 42 |
| 527 |
""") |
| 528 |
m = ffi.dlopen(lib_m) |
| 529 |
assert dir(m) == ['MYE1', 'MYE2', 'MYFOO', 'myconst', 'myfunc', 'myvar'] |
| 530 |
|
| 531 |
def test_dlclose(self): |
| 532 |
if self.Backend is CTypesBackend: |
| 533 |
pytest.skip("not with the ctypes backend") |
| 534 |
ffi = FFI(backend=self.Backend()) |
| 535 |
ffi.cdef("int foobar(void); extern int foobaz;") |
| 536 |
lib = ffi.dlopen(lib_m) |
| 537 |
ffi.dlclose(lib) |
| 538 |
e = pytest.raises(ValueError, getattr, lib, 'foobar') |
| 539 |
assert str(e.value).startswith("library '") |
| 540 |
assert str(e.value).endswith("' has already been closed") |
| 541 |
e = pytest.raises(ValueError, getattr, lib, 'foobaz') |
| 542 |
assert str(e.value).startswith("library '") |
| 543 |
assert str(e.value).endswith("' has already been closed") |
| 544 |
e = pytest.raises(ValueError, setattr, lib, 'foobaz', 42) |
| 545 |
assert str(e.value).startswith("library '") |
| 546 |
assert str(e.value).endswith("' has already been closed") |
| 547 |
ffi.dlclose(lib) |
| 548 |
|
| 549 |
def test_passing_large_list(self): |
| 550 |
if self.Backend is CTypesBackend: |
| 551 |
pytest.skip("the ctypes backend doesn't support this") |
| 552 |
ffi = FFI(backend=self.Backend()) |
| 553 |
ffi.cdef(""" |
| 554 |
void getenv(char *); |
| 555 |
""") |
| 556 |
needs_dlopen_none() |
| 557 |
m = ffi.dlopen(None) |
| 558 |
arg = [b"F", b"O", b"O"] + [b"\x00"] * 20000000 |
| 559 |
x = m.getenv(arg) |
| 560 |
assert x is None |