| 1 |
from rpython.rtyper.lltypesystem import rffi |
| 2 |
from rpython.translator import cdir |
| 3 |
from rpython.rlib.clibffi import get_libc_name |
| 4 |
from rpython.rlib.libffi import types |
| 5 |
from rpython.rlib.libffi import CDLL |
| 6 |
from rpython.rlib.test.test_clibffi import get_libm_name |
| 7 |
|
| 8 |
import sys, py |
| 9 |
|
| 10 |
class BaseAppTestFFI(object): |
| 11 |
spaceconfig = dict(usemodules=('_rawffi',)) |
| 12 |
|
| 13 |
@classmethod |
| 14 |
def prepare_c_example(cls): |
| 15 |
from rpython.tool.udir import udir |
| 16 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 17 |
from rpython.translator.platform import platform |
| 18 |
|
| 19 |
c_file = udir.ensure("test__ffi", dir=1).join("foolib.c") |
| 20 |
|
| 21 |
snippets = [""" |
| 22 |
#include "src/precommondefs.h" |
| 23 |
#define DLLEXPORT RPY_EXPORTED |
| 24 |
"""] |
| 25 |
for name in dir(cls): |
| 26 |
if name.startswith('test_'): |
| 27 |
meth = getattr(cls, name) |
| 28 |
|
| 29 |
|
| 30 |
if meth.__doc__ is not None and '{' in meth.__doc__: |
| 31 |
snippets.append(meth.__doc__) |
| 32 |
|
| 33 |
c_file.write(py.code.Source('\n'.join(snippets))) |
| 34 |
eci = ExternalCompilationInfo(include_dirs=[cdir]) |
| 35 |
|
| 36 |
|
| 37 |
return str(platform.compile([c_file], eci, 'x' + cls.__name__, |
| 38 |
standalone=False)) |
| 39 |
|
| 40 |
def setup_class(cls): |
| 41 |
space = cls.space |
| 42 |
cls.w_iswin32 = space.wrap(sys.platform == 'win32') |
| 43 |
cls.w_iswin64 = space.wrap(sys.platform == 'win32' |
| 44 |
and sys.maxint == 2**63-1) |
| 45 |
cls.w_libfoo_name = space.wrap(cls.prepare_c_example()) |
| 46 |
cls.w_libc_name = space.wrap(get_libc_name()) |
| 47 |
libm_name = get_libm_name(sys.platform) |
| 48 |
cls.w_libm_name = space.wrap(libm_name) |
| 49 |
libm = CDLL(libm_name) |
| 50 |
pow = libm.getpointer('pow', [], types.void) |
| 51 |
pow_addr = rffi.cast(rffi.SIGNED, pow.funcsym) |
| 52 |
cls._libm = libm |
| 53 |
cls.w_pow_addr = space.wrap(pow_addr) |
| 54 |
|
| 55 |
class AppTestFFI(BaseAppTestFFI): |
| 56 |
|
| 57 |
def setup_class(cls): |
| 58 |
BaseAppTestFFI.setup_class.im_func(cls) |
| 59 |
space = cls.space |
| 60 |
|
| 61 |
from ctypes import c_float |
| 62 |
f_12_34 = c_float(12.34).value |
| 63 |
f_56_78 = c_float(56.78).value |
| 64 |
f_result = c_float(f_12_34 + f_56_78).value |
| 65 |
cls.w_f_12_34_plus_56_78 = space.wrap(f_result) |
| 66 |
|
| 67 |
def test_libload(self): |
| 68 |
import _rawffi.alt |
| 69 |
_rawffi.alt.CDLL(self.libc_name) |
| 70 |
|
| 71 |
def test_libload_fail(self): |
| 72 |
import _rawffi.alt |
| 73 |
raises(OSError, _rawffi.alt.CDLL, "xxxxx_this_name_does_not_exist_xxxxx") |
| 74 |
|
| 75 |
def test_libload_None(self): |
| 76 |
if self.iswin32: |
| 77 |
skip("unix specific") |
| 78 |
from _rawffi.alt import CDLL, types |
| 79 |
|
| 80 |
dll = CDLL(None) |
| 81 |
|
| 82 |
res = dll.getfunc('sqrt', [types.double], types.double)(1.0) |
| 83 |
assert res == 1.0 |
| 84 |
|
| 85 |
def test_callfunc(self): |
| 86 |
from _rawffi.alt import CDLL, types |
| 87 |
libm = CDLL(self.libm_name) |
| 88 |
pow = libm.getfunc('pow', [types.double, types.double], types.double) |
| 89 |
assert pow(2, 3) == 8 |
| 90 |
|
| 91 |
@py.test.mark.skipif("py.test.config.option.runappdirect") |
| 92 |
def test_getaddr(self): |
| 93 |
from _rawffi.alt import CDLL, types |
| 94 |
libm = CDLL(self.libm_name) |
| 95 |
pow = libm.getfunc('pow', [types.double, types.double], types.double) |
| 96 |
assert pow.getaddr() == self.pow_addr |
| 97 |
|
| 98 |
@py.test.mark.skipif("py.test.config.option.runappdirect") |
| 99 |
def test_getaddressindll(self): |
| 100 |
import sys |
| 101 |
from _rawffi.alt import CDLL |
| 102 |
libm = CDLL(self.libm_name) |
| 103 |
pow_addr = libm.getaddressindll('pow') |
| 104 |
fff = sys.maxsize*2-1 |
| 105 |
assert pow_addr == self.pow_addr & fff |
| 106 |
|
| 107 |
def test_func_fromaddr(self): |
| 108 |
from _rawffi.alt import CDLL, types, FuncPtr |
| 109 |
libm = CDLL(self.libm_name) |
| 110 |
pow_addr = libm.getaddressindll('pow') |
| 111 |
pow = FuncPtr.fromaddr(pow_addr, 'pow', [types.double, types.double], |
| 112 |
types.double) |
| 113 |
assert pow(2, 3) == 8 |
| 114 |
|
| 115 |
def test_int_args(self): |
| 116 |
""" |
| 117 |
DLLEXPORT int sum_xy(int x, int y) |
| 118 |
{ |
| 119 |
return x+y; |
| 120 |
} |
| 121 |
""" |
| 122 |
import sys |
| 123 |
from _rawffi.alt import CDLL, types |
| 124 |
libfoo = CDLL(self.libfoo_name) |
| 125 |
sum_xy = libfoo.getfunc('sum_xy', [types.sint, types.sint], types.sint) |
| 126 |
assert sum_xy(30, 12) == 42 |
| 127 |
assert sum_xy(sys.maxsize*2, 0) == -2 |
| 128 |
|
| 129 |
def test_void_result(self): |
| 130 |
""" |
| 131 |
int dummy = 0; |
| 132 |
DLLEXPORT void set_dummy(int val) { dummy = val; } |
| 133 |
DLLEXPORT int get_dummy() { return dummy; } |
| 134 |
""" |
| 135 |
from _rawffi.alt import CDLL, types |
| 136 |
libfoo = CDLL(self.libfoo_name) |
| 137 |
set_dummy = libfoo.getfunc('set_dummy', [types.sint], types.void) |
| 138 |
get_dummy = libfoo.getfunc('get_dummy', [], types.sint) |
| 139 |
assert get_dummy() == 0 |
| 140 |
assert set_dummy(42) is None |
| 141 |
assert get_dummy() == 42 |
| 142 |
set_dummy(0) |
| 143 |
|
| 144 |
def test_pointer_args(self): |
| 145 |
""" |
| 146 |
extern int dummy; // defined in test_void_result |
| 147 |
DLLEXPORT int* get_dummy_ptr() { return &dummy; } |
| 148 |
DLLEXPORT void set_val_to_ptr(int* ptr, int val) { *ptr = val; } |
| 149 |
""" |
| 150 |
from _rawffi.alt import CDLL, types |
| 151 |
libfoo = CDLL(self.libfoo_name) |
| 152 |
get_dummy = libfoo.getfunc('get_dummy', [], types.sint) |
| 153 |
get_dummy_ptr = libfoo.getfunc('get_dummy_ptr', [], types.void_p) |
| 154 |
set_val_to_ptr = libfoo.getfunc('set_val_to_ptr', |
| 155 |
[types.void_p, types.sint], |
| 156 |
types.void) |
| 157 |
assert get_dummy() == 0 |
| 158 |
ptr = get_dummy_ptr() |
| 159 |
set_val_to_ptr(ptr, 123) |
| 160 |
assert get_dummy() == 123 |
| 161 |
set_val_to_ptr(ptr, 0) |
| 162 |
|
| 163 |
def test_convert_pointer_args(self): |
| 164 |
""" |
| 165 |
extern int dummy; // defined in test_void_result |
| 166 |
DLLEXPORT int* get_dummy_ptr(); // defined in test_pointer_args |
| 167 |
DLLEXPORT void set_val_to_ptr(int* ptr, int val); // ditto |
| 168 |
""" |
| 169 |
from _rawffi.alt import CDLL, types |
| 170 |
|
| 171 |
class MyPointerWrapper(object): |
| 172 |
def __init__(self, value): |
| 173 |
self.value = value |
| 174 |
def _as_ffi_pointer_(self, ffitype): |
| 175 |
assert ffitype is types.void_p |
| 176 |
return self.value |
| 177 |
|
| 178 |
libfoo = CDLL(self.libfoo_name) |
| 179 |
get_dummy = libfoo.getfunc('get_dummy', [], types.sint) |
| 180 |
get_dummy_ptr = libfoo.getfunc('get_dummy_ptr', [], types.void_p) |
| 181 |
set_val_to_ptr = libfoo.getfunc('set_val_to_ptr', |
| 182 |
[types.void_p, types.sint], |
| 183 |
types.void) |
| 184 |
assert get_dummy() == 0 |
| 185 |
ptr = get_dummy_ptr() |
| 186 |
assert type(ptr) is int |
| 187 |
ptr2 = MyPointerWrapper(ptr) |
| 188 |
set_val_to_ptr(ptr2, 123) |
| 189 |
assert get_dummy() == 123 |
| 190 |
set_val_to_ptr(ptr2, 0) |
| 191 |
|
| 192 |
class OldStyle: |
| 193 |
pass |
| 194 |
raises(TypeError, "set_val_to_ptr(OldStyle(), 0)") |
| 195 |
|
| 196 |
def test_convert_strings_to_char_p(self): |
| 197 |
""" |
| 198 |
DLLEXPORT |
| 199 |
long mystrlen(char* s) |
| 200 |
{ |
| 201 |
long len = 0; |
| 202 |
while(*s++) |
| 203 |
len++; |
| 204 |
return len; |
| 205 |
} |
| 206 |
""" |
| 207 |
from _rawffi.alt import CDLL, types |
| 208 |
import _rawffi |
| 209 |
libfoo = CDLL(self.libfoo_name) |
| 210 |
mystrlen = libfoo.getfunc('mystrlen', [types.char_p], types.slong) |
| 211 |
|
| 212 |
|
| 213 |
assert mystrlen(b'foobar') == 6 |
| 214 |
|
| 215 |
CharArray = _rawffi.Array('c') |
| 216 |
mystr = CharArray(7, b'foobar') |
| 217 |
assert mystrlen(mystr.buffer) == 6 |
| 218 |
mystr.free() |
| 219 |
mystrlen.free_temp_buffers() |
| 220 |
|
| 221 |
def test_convert_unicode_to_unichar_p(self): |
| 222 |
""" |
| 223 |
#include <wchar.h> |
| 224 |
DLLEXPORT |
| 225 |
long mystrlen_u(wchar_t* s) |
| 226 |
{ |
| 227 |
long len = 0; |
| 228 |
while(*s++) |
| 229 |
len++; |
| 230 |
return len; |
| 231 |
} |
| 232 |
""" |
| 233 |
from _rawffi.alt import CDLL, types |
| 234 |
import _rawffi |
| 235 |
libfoo = CDLL(self.libfoo_name) |
| 236 |
mystrlen = libfoo.getfunc('mystrlen_u', [types.unichar_p], types.slong) |
| 237 |
|
| 238 |
|
| 239 |
assert mystrlen('foobar') == 6 |
| 240 |
assert mystrlen(u'foobar') == 6 |
| 241 |
assert mystrlen(u'ab\u2070') == 3 |
| 242 |
|
| 243 |
UniCharArray = _rawffi.Array('u') |
| 244 |
mystr = UniCharArray(7, u'foobar') |
| 245 |
assert mystrlen(mystr.buffer) == 6 |
| 246 |
mystr.free() |
| 247 |
mystrlen.free_temp_buffers() |
| 248 |
|
| 249 |
def test_keepalive_temp_buffer(self): |
| 250 |
""" |
| 251 |
DLLEXPORT |
| 252 |
char* do_nothing(char* s) |
| 253 |
{ |
| 254 |
return s; |
| 255 |
} |
| 256 |
""" |
| 257 |
from _rawffi.alt import CDLL, types |
| 258 |
import _rawffi |
| 259 |
libfoo = CDLL(self.libfoo_name) |
| 260 |
do_nothing = libfoo.getfunc('do_nothing', [types.char_p], types.char_p) |
| 261 |
CharArray = _rawffi.Array('c') |
| 262 |
|
| 263 |
ptr = do_nothing(b'foobar') |
| 264 |
array = CharArray.fromaddress(ptr, 7) |
| 265 |
assert bytes(array) == b'foobar\00' |
| 266 |
do_nothing.free_temp_buffers() |
| 267 |
|
| 268 |
def test_typed_pointer_args(self): |
| 269 |
""" |
| 270 |
extern int dummy; // defined in test_void_result |
| 271 |
DLLEXPORT int* get_dummy_ptr(); // defined in test_pointer_args |
| 272 |
DLLEXPORT void set_val_to_ptr(int* ptr, int val); // ditto |
| 273 |
""" |
| 274 |
from _rawffi.alt import CDLL, types |
| 275 |
|
| 276 |
libfoo = CDLL(self.libfoo_name) |
| 277 |
intptr = types.Pointer(types.sint) |
| 278 |
get_dummy = libfoo.getfunc('get_dummy', [], types.sint) |
| 279 |
get_dummy_ptr = libfoo.getfunc('get_dummy_ptr', [], intptr) |
| 280 |
set_val_to_ptr = libfoo.getfunc('set_val_to_ptr', [intptr, types.sint], types.void) |
| 281 |
assert get_dummy() == 0 |
| 282 |
ptr = get_dummy_ptr() |
| 283 |
set_val_to_ptr(ptr, 123) |
| 284 |
assert get_dummy() == 123 |
| 285 |
set_val_to_ptr(ptr, 0) |
| 286 |
|
| 287 |
def test_huge_pointer_args(self): |
| 288 |
""" |
| 289 |
#include <stdlib.h> |
| 290 |
DLLEXPORT long is_null_ptr(void* ptr) { return ptr == NULL; } |
| 291 |
""" |
| 292 |
import sys |
| 293 |
from _rawffi.alt import CDLL, types |
| 294 |
libfoo = CDLL(self.libfoo_name) |
| 295 |
is_null_ptr = libfoo.getfunc('is_null_ptr', [types.void_p], types.ulong) |
| 296 |
assert not is_null_ptr(sys.maxsize+1) |
| 297 |
|
| 298 |
def test_unsigned_long_args(self): |
| 299 |
""" |
| 300 |
DLLEXPORT unsigned long sum_xy_ul(unsigned long x, unsigned long y) |
| 301 |
{ |
| 302 |
return x+y; |
| 303 |
} |
| 304 |
""" |
| 305 |
import sys |
| 306 |
if sys.platform == 'win32': |
| 307 |
maxlong = 2 ** 31 - 1 |
| 308 |
else: |
| 309 |
maxlong = sys.maxsize |
| 310 |
from _rawffi.alt import CDLL, types |
| 311 |
libfoo = CDLL(self.libfoo_name) |
| 312 |
sum_xy = libfoo.getfunc('sum_xy_ul', [types.ulong, types.ulong], |
| 313 |
types.ulong) |
| 314 |
assert sum_xy(maxlong, 12) == maxlong + 12 |
| 315 |
assert sum_xy(maxlong + 1, 12) == maxlong + 13 |
| 316 |
|
| 317 |
res = sum_xy(maxlong * 2 + 3, 0) |
| 318 |
assert res == 1 |
| 319 |
|
| 320 |
def test_unsigned_short_args(self): |
| 321 |
""" |
| 322 |
DLLEXPORT unsigned short sum_xy_us(unsigned short x, unsigned short y) |
| 323 |
{ |
| 324 |
return x+y; |
| 325 |
} |
| 326 |
""" |
| 327 |
from _rawffi.alt import CDLL, types |
| 328 |
libfoo = CDLL(self.libfoo_name) |
| 329 |
sum_xy = libfoo.getfunc('sum_xy_us', [types.ushort, types.ushort], |
| 330 |
types.ushort) |
| 331 |
assert sum_xy(32000, 8000) == 40000 |
| 332 |
assert sum_xy(60000, 30000) == 90000 % 65536 |
| 333 |
|
| 334 |
def test_unsigned_byte_args(self): |
| 335 |
""" |
| 336 |
DLLEXPORT unsigned char sum_xy_ub(unsigned char x, unsigned char y) |
| 337 |
{ |
| 338 |
return x+y; |
| 339 |
} |
| 340 |
""" |
| 341 |
from _rawffi.alt import CDLL, types |
| 342 |
libfoo = CDLL(self.libfoo_name) |
| 343 |
sum_xy = libfoo.getfunc('sum_xy_us', [types.ubyte, types.ubyte], |
| 344 |
types.ubyte) |
| 345 |
assert sum_xy(100, 40) == 140 |
| 346 |
assert sum_xy(200, 60) == 260 % 256 |
| 347 |
|
| 348 |
def test_unsigned_int_args(self): |
| 349 |
r""" |
| 350 |
DLLEXPORT unsigned int sum_xy_ui(unsigned int x, unsigned int y) |
| 351 |
{ |
| 352 |
return x+y; |
| 353 |
} |
| 354 |
""" |
| 355 |
import sys |
| 356 |
from _rawffi.alt import CDLL, types |
| 357 |
maxint32 = 2147483647 |
| 358 |
libfoo = CDLL(self.libfoo_name) |
| 359 |
sum_xy = libfoo.getfunc('sum_xy_ui', [types.uint, types.uint], |
| 360 |
types.uint) |
| 361 |
assert sum_xy(maxint32, 1) == maxint32+1 |
| 362 |
assert sum_xy(maxint32, maxint32+2) == 0 |
| 363 |
|
| 364 |
def test_signed_byte_args(self): |
| 365 |
""" |
| 366 |
DLLEXPORT signed char sum_xy_sb(signed char x, signed char y) |
| 367 |
{ |
| 368 |
return x+y; |
| 369 |
} |
| 370 |
""" |
| 371 |
from _rawffi.alt import CDLL, types |
| 372 |
libfoo = CDLL(self.libfoo_name) |
| 373 |
sum_xy = libfoo.getfunc('sum_xy_sb', [types.sbyte, types.sbyte], |
| 374 |
types.sbyte) |
| 375 |
assert sum_xy(10, 20) == 30 |
| 376 |
assert sum_xy(100, 28) == -128 |
| 377 |
|
| 378 |
def test_char_args(self): |
| 379 |
""" |
| 380 |
DLLEXPORT char my_toupper(char x) |
| 381 |
{ |
| 382 |
return x - ('a'-'A'); |
| 383 |
} |
| 384 |
""" |
| 385 |
from _rawffi.alt import CDLL, types |
| 386 |
libfoo = CDLL(self.libfoo_name) |
| 387 |
my_toupper = libfoo.getfunc('my_toupper', [types.char], |
| 388 |
types.char) |
| 389 |
res = my_toupper(b'c') |
| 390 |
assert type(res) is bytes |
| 391 |
assert res == b'C' |
| 392 |
|
| 393 |
def test_unichar_args(self): |
| 394 |
""" |
| 395 |
#include <stddef.h> |
| 396 |
DLLEXPORT wchar_t sum_xy_wc(wchar_t x, wchar_t y) |
| 397 |
{ |
| 398 |
return x + y; |
| 399 |
} |
| 400 |
""" |
| 401 |
from _rawffi.alt import CDLL, types |
| 402 |
libfoo = CDLL(self.libfoo_name) |
| 403 |
sum_xy = libfoo.getfunc('sum_xy_wc', [types.unichar, types.unichar], |
| 404 |
types.unichar) |
| 405 |
res = sum_xy(chr(1000), chr(2000)) |
| 406 |
assert type(res) is str |
| 407 |
assert ord(res) == 3000 |
| 408 |
|
| 409 |
def test_single_float_args(self): |
| 410 |
""" |
| 411 |
DLLEXPORT float sum_xy_float(float x, float y) |
| 412 |
{ |
| 413 |
return x+y; |
| 414 |
} |
| 415 |
""" |
| 416 |
from _rawffi.alt import CDLL, types |
| 417 |
libfoo = CDLL(self.libfoo_name) |
| 418 |
sum_xy = libfoo.getfunc('sum_xy_float', [types.float, types.float], |
| 419 |
types.float) |
| 420 |
res = sum_xy(12.34, 56.78) |
| 421 |
assert res == self.f_12_34_plus_56_78 |
| 422 |
|
| 423 |
|
| 424 |
def test_slonglong_args(self): |
| 425 |
""" |
| 426 |
DLLEXPORT long long sum_xy_longlong(long long x, long long y) |
| 427 |
{ |
| 428 |
return x+y; |
| 429 |
} |
| 430 |
""" |
| 431 |
from _rawffi.alt import CDLL, types |
| 432 |
maxint32 = 2147483647 |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
libfoo = CDLL(self.libfoo_name) |
| 437 |
sum_xy = libfoo.getfunc('sum_xy_longlong', [types.slonglong, types.slonglong], |
| 438 |
types.slonglong) |
| 439 |
x = maxint32+1 |
| 440 |
y = maxint32+2 |
| 441 |
res = sum_xy(x, y) |
| 442 |
expected = maxint32*2 + 3 |
| 443 |
assert res == expected |
| 444 |
|
| 445 |
def test_ulonglong_args(self): |
| 446 |
""" |
| 447 |
DLLEXPORT unsigned long long sum_xy_ulonglong(unsigned long long x, |
| 448 |
unsigned long long y) |
| 449 |
{ |
| 450 |
return x+y; |
| 451 |
} |
| 452 |
""" |
| 453 |
from _rawffi.alt import CDLL, types |
| 454 |
maxint64 = 9223372036854775807 |
| 455 |
|
| 456 |
|
| 457 |
libfoo = CDLL(self.libfoo_name) |
| 458 |
sum_xy = libfoo.getfunc('sum_xy_ulonglong', [types.ulonglong, types.ulonglong], |
| 459 |
types.ulonglong) |
| 460 |
x = maxint64+1 |
| 461 |
y = 2 |
| 462 |
res = sum_xy(x, y) |
| 463 |
expected = maxint64 + 3 |
| 464 |
assert res == expected |
| 465 |
|
| 466 |
res = sum_xy(maxint64*2+3, 0) |
| 467 |
assert res == 1 |
| 468 |
|
| 469 |
def test_byval_argument(self): |
| 470 |
""" |
| 471 |
struct Point { |
| 472 |
long x; |
| 473 |
long y; |
| 474 |
}; |
| 475 |
|
| 476 |
DLLEXPORT long sum_point(struct Point p) { |
| 477 |
return p.x + p.y; |
| 478 |
} |
| 479 |
""" |
| 480 |
from _rawffi.alt import CDLL, types, _StructDescr, Field |
| 481 |
Point = _StructDescr('Point', [ |
| 482 |
Field('x', types.slong), |
| 483 |
Field('y', types.slong), |
| 484 |
]) |
| 485 |
libfoo = CDLL(self.libfoo_name) |
| 486 |
sum_point = libfoo.getfunc('sum_point', [Point.ffitype], types.slong) |
| 487 |
|
| 488 |
p = Point.allocate() |
| 489 |
p.setfield('x', 30) |
| 490 |
p.setfield('y', 12) |
| 491 |
res = sum_point(p) |
| 492 |
assert res == 42 |
| 493 |
|
| 494 |
def test_byval_result(self): |
| 495 |
""" |
| 496 |
DLLEXPORT struct Point make_point(long x, long y) { |
| 497 |
struct Point p; |
| 498 |
p.x = x; |
| 499 |
p.y = y; |
| 500 |
return p; |
| 501 |
} |
| 502 |
""" |
| 503 |
from _rawffi.alt import CDLL, types, _StructDescr, Field |
| 504 |
Point = _StructDescr('Point', [ |
| 505 |
Field('x', types.slong), |
| 506 |
Field('y', types.slong), |
| 507 |
]) |
| 508 |
libfoo = CDLL(self.libfoo_name) |
| 509 |
make_point = libfoo.getfunc('make_point', [types.slong, types.slong], |
| 510 |
Point.ffitype) |
| 511 |
|
| 512 |
p = make_point(12, 34) |
| 513 |
assert p.getfield('x') == 12 |
| 514 |
assert p.getfield('y') == 34 |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
def test_byval_argument__rawffi(self): |
| 520 |
""" |
| 521 |
// defined above |
| 522 |
struct Point; |
| 523 |
DLLEXPORT long sum_point(struct Point p); |
| 524 |
""" |
| 525 |
import _rawffi |
| 526 |
from _rawffi.alt import CDLL, types |
| 527 |
POINT = _rawffi.Structure([('x', 'l'), ('y', 'l')]) |
| 528 |
ffi_point = POINT.get_ffi_type() |
| 529 |
libfoo = CDLL(self.libfoo_name) |
| 530 |
sum_point = libfoo.getfunc('sum_point', [ffi_point], types.slong) |
| 531 |
|
| 532 |
p = POINT() |
| 533 |
p.x = 30 |
| 534 |
p.y = 12 |
| 535 |
res = sum_point(p) |
| 536 |
assert res == 42 |
| 537 |
p.free() |
| 538 |
|
| 539 |
def test_byval_result__rawffi(self): |
| 540 |
""" |
| 541 |
// defined above |
| 542 |
DLLEXPORT struct Point make_point(long x, long y); |
| 543 |
""" |
| 544 |
import _rawffi |
| 545 |
from _rawffi.alt import CDLL, types |
| 546 |
POINT = _rawffi.Structure([('x', 'l'), ('y', 'l')]) |
| 547 |
ffi_point = POINT.get_ffi_type() |
| 548 |
libfoo = CDLL(self.libfoo_name) |
| 549 |
make_point = libfoo.getfunc('make_point', [types.slong, types.slong], ffi_point) |
| 550 |
|
| 551 |
p = make_point(12, 34) |
| 552 |
assert p.x == 12 |
| 553 |
assert p.y == 34 |
| 554 |
p.free() |
| 555 |
|
| 556 |
|
| 557 |
def test_TypeError_numargs(self): |
| 558 |
from _rawffi.alt import CDLL, types |
| 559 |
libfoo = CDLL(self.libfoo_name) |
| 560 |
sum_xy = libfoo.getfunc('sum_xy', [types.sint, types.sint], types.sint) |
| 561 |
raises(TypeError, "sum_xy(1, 2, 3)") |
| 562 |
raises(TypeError, "sum_xy(1)") |
| 563 |
|
| 564 |
def test_TypeError_voidarg(self): |
| 565 |
from _rawffi.alt import CDLL, types |
| 566 |
libfoo = CDLL(self.libfoo_name) |
| 567 |
raises(TypeError, "libfoo.getfunc('sum_xy', [types.void], types.sint)") |
| 568 |
|
| 569 |
def test_OSError_loading(self): |
| 570 |
from _rawffi.alt import CDLL, types |
| 571 |
raises(OSError, "CDLL('I do not exist')") |
| 572 |
|
| 573 |
def test_AttributeError_missing_function(self): |
| 574 |
from _rawffi.alt import CDLL, types |
| 575 |
libfoo = CDLL(self.libfoo_name) |
| 576 |
raises(AttributeError, "libfoo.getfunc('I_do_not_exist', [], types.void)") |
| 577 |
if self.iswin32 or self.iswin64: |
| 578 |
skip("unix specific") |
| 579 |
libnone = CDLL(None) |
| 580 |
raises(AttributeError, "libnone.getfunc('I_do_not_exist', [], types.void)") |
| 581 |
|
| 582 |
def test_calling_convention1(self): |
| 583 |
|
| 584 |
if not self.iswin32 or self.iswin64: |
| 585 |
skip("windows 32-bit specific") |
| 586 |
from _rawffi.alt import WinDLL, types |
| 587 |
libm = WinDLL(self.libm_name) |
| 588 |
pow = libm.getfunc('pow', [types.double, types.double], types.double) |
| 589 |
try: |
| 590 |
pow(2, 3) |
| 591 |
except ValueError as e: |
| 592 |
assert str(e).startswith('Procedure called with') |
| 593 |
else: |
| 594 |
assert 0, 'test must assert, wrong calling convention' |
| 595 |
|
| 596 |
def test_calling_convention2(self): |
| 597 |
if not self.iswin32: |
| 598 |
skip("windows specific") |
| 599 |
from _rawffi.alt import WinDLL, types |
| 600 |
kernel = WinDLL('Kernel32.dll') |
| 601 |
sleep = kernel.getfunc('Sleep', [types.uint], types.void) |
| 602 |
sleep(10) |
| 603 |
|
| 604 |
def test_calling_convention3(self): |
| 605 |
|
| 606 |
if not self.iswin32 or self.iswin64: |
| 607 |
skip("windows 32-bit specific") |
| 608 |
from _rawffi.alt import CDLL, types |
| 609 |
wrong_kernel = CDLL('Kernel32.dll') |
| 610 |
wrong_sleep = wrong_kernel.getfunc('Sleep', [types.uint], types.void) |
| 611 |
try: |
| 612 |
wrong_sleep(10) |
| 613 |
except ValueError as e: |
| 614 |
assert str(e).startswith('Procedure called with') |
| 615 |
else: |
| 616 |
assert 0, 'test must assert, wrong calling convention' |
| 617 |
|
| 618 |
def test_func_fromaddr2(self): |
| 619 |
|
| 620 |
if not self.iswin32 or self.iswin64: |
| 621 |
skip("windows 32-bit specific") |
| 622 |
from _rawffi.alt import CDLL, types, FuncPtr |
| 623 |
from _rawffi import FUNCFLAG_STDCALL |
| 624 |
libm = CDLL(self.libm_name) |
| 625 |
pow_addr = libm.getaddressindll('pow') |
| 626 |
wrong_pow = FuncPtr.fromaddr(pow_addr, 'pow', |
| 627 |
[types.double, types.double], types.double, FUNCFLAG_STDCALL) |
| 628 |
try: |
| 629 |
wrong_pow(2, 3) == 8 |
| 630 |
except ValueError as e: |
| 631 |
assert str(e).startswith('Procedure called with') |
| 632 |
else: |
| 633 |
assert 0, 'test must assert, wrong calling convention' |
| 634 |
|
| 635 |
def test_func_fromaddr3(self): |
| 636 |
|
| 637 |
if not self.iswin32: |
| 638 |
skip("windows specific") |
| 639 |
from _rawffi.alt import WinDLL, types, FuncPtr |
| 640 |
from _rawffi import FUNCFLAG_STDCALL |
| 641 |
kernel = WinDLL('Kernel32.dll') |
| 642 |
sleep_addr = kernel.getaddressindll('Sleep') |
| 643 |
sleep = FuncPtr.fromaddr(sleep_addr, 'sleep', [types.uint], |
| 644 |
types.void, FUNCFLAG_STDCALL) |
| 645 |
sleep(10) |
| 646 |
|
| 647 |
def test_by_ordinal(self): |
| 648 |
""" |
| 649 |
int DLLEXPORT AAA_first_ordinal_function() |
| 650 |
{ |
| 651 |
return 42; |
| 652 |
} |
| 653 |
""" |
| 654 |
if not self.iswin32: |
| 655 |
skip("windows specific") |
| 656 |
from _rawffi.alt import CDLL, types |
| 657 |
libfoo = CDLL(self.libfoo_name) |
| 658 |
f_name = libfoo.getfunc('AAA_first_ordinal_function', [], types.sint) |
| 659 |
f_ordinal = libfoo.getfunc(1, [], types.sint) |
| 660 |
assert f_name.getaddr() == f_ordinal.getaddr() |
| 661 |
|
| 662 |
def test_cdll_as_integer(self): |
| 663 |
import _rawffi |
| 664 |
from _rawffi.alt import CDLL |
| 665 |
libfoo = CDLL(self.libfoo_name) |
| 666 |
A = _rawffi.Array('i') |
| 667 |
a = A(1, autofree=True) |
| 668 |
a[0] = libfoo |
| 669 |
|
| 670 |
def test_windll_as_integer(self): |
| 671 |
if not self.iswin32: |
| 672 |
skip("windows specific") |
| 673 |
import _rawffi |
| 674 |
from _rawffi.alt import WinDLL |
| 675 |
libm = WinDLL(self.libm_name) |
| 676 |
A = _rawffi.Array('i') |
| 677 |
a = A(1, autofree=True) |
| 678 |
a[0] = libm |