| 1 |
from rpython.translator.platform import platform |
| 2 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 3 |
from rpython.translator import cdir |
| 4 |
from pypy.module._rawffi.interp_rawffi import TYPEMAP, TYPEMAP_FLOAT_LETTERS |
| 5 |
from pypy.module._rawffi.tracker import Tracker |
| 6 |
|
| 7 |
import os, sys, py |
| 8 |
|
| 9 |
class AppTestFfi(object): |
| 10 |
spaceconfig = dict(usemodules=['_rawffi', 'struct']) |
| 11 |
|
| 12 |
def prepare_c_example(): |
| 13 |
from rpython.tool.udir import udir |
| 14 |
c_file = udir.ensure("test__rawffi", dir=1).join("xlib.c") |
| 15 |
c_file.write(py.code.Source(''' |
| 16 |
#include "src/precommondefs.h" |
| 17 |
#include <stdlib.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <stdarg.h> |
| 20 |
#include <errno.h> |
| 21 |
|
| 22 |
struct x |
| 23 |
{ |
| 24 |
int x1; |
| 25 |
short x2; |
| 26 |
char x3; |
| 27 |
struct x* next; |
| 28 |
}; |
| 29 |
|
| 30 |
RPY_EXPORTED |
| 31 |
void nothing() |
| 32 |
{ |
| 33 |
} |
| 34 |
|
| 35 |
RPY_EXPORTED |
| 36 |
char inner_struct_elem(struct x *x1) |
| 37 |
{ |
| 38 |
return x1->next->x3; |
| 39 |
} |
| 40 |
|
| 41 |
RPY_EXPORTED |
| 42 |
struct x* create_double_struct() |
| 43 |
{ |
| 44 |
struct x* x1, *x2; |
| 45 |
|
| 46 |
x1 = (struct x*)malloc(sizeof(struct x)); |
| 47 |
x2 = (struct x*)malloc(sizeof(struct x)); |
| 48 |
x1->next = x2; |
| 49 |
x2->x2 = 3; |
| 50 |
return x1; |
| 51 |
} |
| 52 |
|
| 53 |
RPY_EXPORTED |
| 54 |
void free_double_struct(struct x* x1) |
| 55 |
{ |
| 56 |
free(x1->next); |
| 57 |
free(x1); |
| 58 |
} |
| 59 |
|
| 60 |
const char *static_str = "xxxxxx"; |
| 61 |
RPY_EXPORTED long static_int; |
| 62 |
long static_int = 42; |
| 63 |
RPY_EXPORTED double static_double; |
| 64 |
double static_double = 42.42; |
| 65 |
RPY_EXPORTED long double static_longdouble; |
| 66 |
long double static_longdouble = 42.42; |
| 67 |
|
| 68 |
RPY_EXPORTED |
| 69 |
unsigned short add_shorts(short one, short two) |
| 70 |
{ |
| 71 |
return one + two; |
| 72 |
} |
| 73 |
|
| 74 |
RPY_EXPORTED |
| 75 |
void* get_raw_pointer() |
| 76 |
{ |
| 77 |
return (void*)add_shorts; |
| 78 |
} |
| 79 |
|
| 80 |
RPY_EXPORTED |
| 81 |
char get_char(char* s, unsigned short num) |
| 82 |
{ |
| 83 |
return s[num]; |
| 84 |
} |
| 85 |
|
| 86 |
RPY_EXPORTED |
| 87 |
const char *char_check(char x, char y) |
| 88 |
{ |
| 89 |
if (y == static_str[0]) |
| 90 |
return static_str; |
| 91 |
return NULL; |
| 92 |
} |
| 93 |
|
| 94 |
RPY_EXPORTED |
| 95 |
int get_array_elem(int* stuff, int num) |
| 96 |
{ |
| 97 |
return stuff[num]; |
| 98 |
} |
| 99 |
|
| 100 |
RPY_EXPORTED |
| 101 |
struct x* get_array_elem_s(struct x** array, int num) |
| 102 |
{ |
| 103 |
return array[num]; |
| 104 |
} |
| 105 |
|
| 106 |
RPY_EXPORTED |
| 107 |
long long some_huge_value() |
| 108 |
{ |
| 109 |
return 1LL<<42; |
| 110 |
} |
| 111 |
|
| 112 |
RPY_EXPORTED |
| 113 |
unsigned long long some_huge_uvalue() |
| 114 |
{ |
| 115 |
return 1LL<<42; |
| 116 |
} |
| 117 |
|
| 118 |
RPY_EXPORTED |
| 119 |
long long pass_ll(long long x) |
| 120 |
{ |
| 121 |
return x; |
| 122 |
} |
| 123 |
|
| 124 |
static int prebuilt_array1[] = {3}; |
| 125 |
|
| 126 |
RPY_EXPORTED |
| 127 |
int* allocate_array() |
| 128 |
{ |
| 129 |
return prebuilt_array1; |
| 130 |
} |
| 131 |
|
| 132 |
RPY_EXPORTED |
| 133 |
long long runcallback(long long(*callback)()) |
| 134 |
{ |
| 135 |
return callback(); |
| 136 |
} |
| 137 |
|
| 138 |
struct x_y { |
| 139 |
long x; |
| 140 |
long y; |
| 141 |
}; |
| 142 |
|
| 143 |
RPY_EXPORTED |
| 144 |
long sum_x_y(struct x_y s) { |
| 145 |
return s.x + s.y; |
| 146 |
} |
| 147 |
|
| 148 |
RPY_EXPORTED |
| 149 |
long op_x_y(struct x_y s, long(*callback)(struct x_y)) |
| 150 |
{ |
| 151 |
return callback(s); |
| 152 |
} |
| 153 |
|
| 154 |
struct s2h { |
| 155 |
short x; |
| 156 |
short y; |
| 157 |
}; |
| 158 |
|
| 159 |
RPY_EXPORTED |
| 160 |
struct s2h give(short x, short y) { |
| 161 |
struct s2h out; |
| 162 |
out.x = x; |
| 163 |
out.y = y; |
| 164 |
return out; |
| 165 |
} |
| 166 |
|
| 167 |
RPY_EXPORTED |
| 168 |
struct s2h perturb(struct s2h inp) { |
| 169 |
inp.x *= 2; |
| 170 |
inp.y *= 3; |
| 171 |
return inp; |
| 172 |
} |
| 173 |
|
| 174 |
struct s2a { |
| 175 |
int bah[2]; |
| 176 |
}; |
| 177 |
|
| 178 |
RPY_EXPORTED |
| 179 |
struct s2a get_s2a(void) { |
| 180 |
struct s2a outp; |
| 181 |
outp.bah[0] = 4; |
| 182 |
outp.bah[1] = 5; |
| 183 |
return outp; |
| 184 |
} |
| 185 |
|
| 186 |
RPY_EXPORTED |
| 187 |
int check_s2a(struct s2a inp) { |
| 188 |
return (inp.bah[0] == 4 && inp.bah[1] == 5); |
| 189 |
} |
| 190 |
|
| 191 |
RPY_EXPORTED |
| 192 |
int AAA_first_ordinal_function() |
| 193 |
{ |
| 194 |
return 42; |
| 195 |
} |
| 196 |
|
| 197 |
typedef union { |
| 198 |
short x; |
| 199 |
long y; |
| 200 |
} UN; |
| 201 |
|
| 202 |
RPY_EXPORTED |
| 203 |
UN ret_un_func(UN inp) |
| 204 |
{ |
| 205 |
inp.y = inp.x * 100; |
| 206 |
return inp; |
| 207 |
} |
| 208 |
|
| 209 |
RPY_EXPORTED |
| 210 |
int check_errno(int incoming) |
| 211 |
{ |
| 212 |
int old_errno = errno; |
| 213 |
errno = incoming; |
| 214 |
return old_errno; |
| 215 |
} |
| 216 |
|
| 217 |
#ifdef _WIN32 |
| 218 |
#include <Windows.h> |
| 219 |
RPY_EXPORTED |
| 220 |
int check_last_error(int incoming) |
| 221 |
{ |
| 222 |
int old_errno = GetLastError(); |
| 223 |
SetLastError(incoming); |
| 224 |
return old_errno; |
| 225 |
} |
| 226 |
#endif |
| 227 |
|
| 228 |
RPY_EXPORTED |
| 229 |
long variadic_sum(long n, ...) { |
| 230 |
va_list ptr; |
| 231 |
int sum = 0; |
| 232 |
printf("n: %ld\\n", n); |
| 233 |
|
| 234 |
va_start(ptr, n); |
| 235 |
for (int i = 0; i < n; i++) { |
| 236 |
long foo = va_arg(ptr, long); |
| 237 |
sum += foo; |
| 238 |
printf("Arg %d: %ld\\n", i, foo); |
| 239 |
} |
| 240 |
va_end(ptr); |
| 241 |
return sum; |
| 242 |
} |
| 243 |
''')) |
| 244 |
eci = ExternalCompilationInfo(include_dirs=[cdir]) |
| 245 |
return str(platform.compile([c_file], eci, 'x', standalone=False)) |
| 246 |
prepare_c_example = staticmethod(prepare_c_example) |
| 247 |
|
| 248 |
def setup_class(cls): |
| 249 |
space = cls.space |
| 250 |
from rpython.rlib.clibffi import get_libc_name |
| 251 |
cls.w_lib_name = space.wrap(cls.prepare_c_example()) |
| 252 |
cls.w_libc_name = space.wrap(get_libc_name()) |
| 253 |
if sys.platform == 'win32': |
| 254 |
cls.w_iswin32 = space.wrap(True) |
| 255 |
cls.w_libm_name = space.wrap('msvcrt') |
| 256 |
else: |
| 257 |
cls.w_iswin32 = space.wrap(False) |
| 258 |
cls.w_libm_name = space.wrap('libm.so') |
| 259 |
if sys.platform == "darwin": |
| 260 |
cls.w_libm_name = space.wrap('libm.dylib') |
| 261 |
cls.w_platform = space.wrap(platform.name) |
| 262 |
cls.w_sizes_and_alignments = space.wrap(dict( |
| 263 |
[(k, (v.c_size, v.c_alignment)) for k,v in TYPEMAP.iteritems()])) |
| 264 |
cls.w_float_typemap = space.wrap(TYPEMAP_FLOAT_LETTERS) |
| 265 |
cls.w_is64bit = space.wrap(sys.maxint > 2147483647) |
| 266 |
cls.w_runappdirect = space.wrap(cls.runappdirect) |
| 267 |
|
| 268 |
def test_libload(self): |
| 269 |
import _rawffi |
| 270 |
_rawffi.CDLL(self.libc_name) |
| 271 |
|
| 272 |
def test_libload_fail(self): |
| 273 |
import _rawffi |
| 274 |
try: |
| 275 |
_rawffi.CDLL("xxxxx_this_name_does_not_exist_xxxxx") |
| 276 |
except OSError as e: |
| 277 |
print(e) |
| 278 |
assert str(e).startswith( |
| 279 |
"Cannot load library xxxxx_this_name_does_not_exist_xxxxx: ") |
| 280 |
else: |
| 281 |
raise AssertionError("did not fail??") |
| 282 |
|
| 283 |
def test_libload_None(self): |
| 284 |
import _rawffi |
| 285 |
|
| 286 |
try: |
| 287 |
dll = _rawffi.CDLL(None) |
| 288 |
except TypeError: |
| 289 |
if not self.iswin32: |
| 290 |
raise |
| 291 |
else: |
| 292 |
func = dll.ptr('rand', [], 'i') |
| 293 |
res = func() |
| 294 |
assert res[0] != 0 |
| 295 |
|
| 296 |
def test_libc_load(self): |
| 297 |
import _rawffi |
| 298 |
_rawffi.get_libc() |
| 299 |
|
| 300 |
def test_getattr(self): |
| 301 |
import _rawffi |
| 302 |
libc = _rawffi.get_libc() |
| 303 |
func = libc.ptr('rand', [], 'i') |
| 304 |
assert libc.ptr('rand', [], 'i') is func |
| 305 |
assert libc.ptr('rand', [], 'l') is not func |
| 306 |
assert isinstance(func, _rawffi.FuncPtr) |
| 307 |
raises(AttributeError, "libc.xxxxxxxxxxxxxx") |
| 308 |
|
| 309 |
def test_byordinal(self): |
| 310 |
if not self.iswin32: |
| 311 |
skip("win32 specific") |
| 312 |
import _rawffi |
| 313 |
lib = _rawffi.CDLL(self.lib_name) |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
assert lib.ptr(1, [], 'i')()[0] == 42 |
| 318 |
|
| 319 |
def test_getchar(self): |
| 320 |
import _rawffi |
| 321 |
lib = _rawffi.CDLL(self.lib_name) |
| 322 |
get_char = lib.ptr('get_char', ['P', 'H'], 'c') |
| 323 |
A = _rawffi.Array('c') |
| 324 |
B = _rawffi.Array('H') |
| 325 |
dupa = A(5, b'dupa') |
| 326 |
dupaptr = dupa.byptr() |
| 327 |
for i in range(4): |
| 328 |
intptr = B(1) |
| 329 |
intptr[0] = i |
| 330 |
res = get_char(dupaptr, intptr) |
| 331 |
char = b'dupa'[i:i+1] |
| 332 |
assert res[0] == char |
| 333 |
intptr.free() |
| 334 |
dupaptr.free() |
| 335 |
dupa.free() |
| 336 |
|
| 337 |
def test_chararray_as_bytebuffer(self): |
| 338 |
|
| 339 |
import _rawffi |
| 340 |
A = _rawffi.Array('c') |
| 341 |
buf = A(10, autofree=True) |
| 342 |
buf[0] = b'*' |
| 343 |
assert buf[1:5] == b'\x00' * 4 |
| 344 |
buf[7:] = b'abc' |
| 345 |
assert buf[9] == b'c' |
| 346 |
assert buf[:8] == b'*' + b'\x00'*6 + b'a' |
| 347 |
|
| 348 |
def test_returning_str(self): |
| 349 |
import _rawffi |
| 350 |
lib = _rawffi.CDLL(self.lib_name) |
| 351 |
char_check = lib.ptr('char_check', ['c', 'c'], 's') |
| 352 |
A = _rawffi.Array('c') |
| 353 |
arg1 = A(1) |
| 354 |
arg2 = A(1) |
| 355 |
arg1[0] = b'y' |
| 356 |
arg2[0] = b'x' |
| 357 |
res = char_check(arg1, arg2) |
| 358 |
assert _rawffi.charp2string(res[0]) == b'xxxxxx' |
| 359 |
assert _rawffi.charp2rawstring(res[0]) == b'xxxxxx' |
| 360 |
assert _rawffi.charp2rawstring(res[0], 3) == b'xxx' |
| 361 |
a = A(6, b'xx\x00\x00xx') |
| 362 |
assert _rawffi.charp2string(a.buffer) == b'xx' |
| 363 |
assert _rawffi.charp2rawstring(a.buffer, 4) == b'xx\x00\x00' |
| 364 |
arg1[0] = b'x' |
| 365 |
arg2[0] = b'y' |
| 366 |
res = char_check(arg1, arg2) |
| 367 |
assert res[0] == 0 |
| 368 |
assert _rawffi.charp2string(res[0]) is None |
| 369 |
arg1.free() |
| 370 |
arg2.free() |
| 371 |
a.free() |
| 372 |
|
| 373 |
def test_unicode_array(self): |
| 374 |
import _rawffi |
| 375 |
A = _rawffi.Array('u') |
| 376 |
a = A(6, u'\u1234') |
| 377 |
assert a[0] == u'\u1234' |
| 378 |
a[0] = u'\ud800' |
| 379 |
assert a[0] == u'\ud800' |
| 380 |
if _rawffi.sizeof('u') == 4: |
| 381 |
a[0] = u'\U00012345' |
| 382 |
assert a[0] == u'\U00012345' |
| 383 |
B = _rawffi.Array('i') |
| 384 |
b = B.fromaddress(a.itemaddress(0), 1) |
| 385 |
b[0] = 0xffffffff |
| 386 |
if self.runappdirect: |
| 387 |
|
| 388 |
|
| 389 |
raises(ValueError, "a[0]") |
| 390 |
a.free() |
| 391 |
|
| 392 |
def test_returning_unicode(self): |
| 393 |
import _rawffi |
| 394 |
A = _rawffi.Array('u') |
| 395 |
a = A(6, u'xx\x00\x00xx') |
| 396 |
for i in (-1, 6): |
| 397 |
res = _rawffi.wcharp2unicode(a.buffer, i) |
| 398 |
assert isinstance(res, str) |
| 399 |
assert res == u'xx' |
| 400 |
a.free() |
| 401 |
|
| 402 |
def test_rawstring2charp(self): |
| 403 |
import _rawffi |
| 404 |
A = _rawffi.Array('c') |
| 405 |
a = A(10, b'x'*10) |
| 406 |
_rawffi.rawstring2charp(a.buffer, b"foobar") |
| 407 |
assert b''.join([a[i] for i in range(10)]) == b"foobarxxxx" |
| 408 |
_rawffi.rawstring2charp(a.buffer, memoryview(b"baz")) |
| 409 |
assert b''.join([a[i] for i in range(10)]) == b"bazbarxxxx" |
| 410 |
_rawffi.rawstring2charp(a.buffer, memoryview(b"ABCDEF"), 2) |
| 411 |
assert b''.join([a[i] for i in range(10)]) == b"CDEFarxxxx" |
| 412 |
_rawffi.rawstring2charp(a.buffer, memoryview(b"ZYXWVU"), 2, 3) |
| 413 |
assert b''.join([a[i] for i in range(10)]) == b"XWVFarxxxx" |
| 414 |
a.free() |
| 415 |
|
| 416 |
def test_raw_callable(self): |
| 417 |
import _rawffi |
| 418 |
lib = _rawffi.CDLL(self.lib_name) |
| 419 |
get_raw_pointer = lib.ptr('get_raw_pointer', [], 'P') |
| 420 |
ptr = get_raw_pointer() |
| 421 |
rawcall = _rawffi.FuncPtr(ptr[0], ['h', 'h'], 'H') |
| 422 |
A = _rawffi.Array('h') |
| 423 |
arg1 = A(1) |
| 424 |
arg2 = A(1) |
| 425 |
arg1[0] = 1 |
| 426 |
arg2[0] = 2 |
| 427 |
res = rawcall(arg1, arg2) |
| 428 |
assert res[0] == 3 |
| 429 |
arg1.free() |
| 430 |
arg2.free() |
| 431 |
assert rawcall.buffer == ptr[0] |
| 432 |
ptr = rawcall.byptr() |
| 433 |
assert ptr[0] == rawcall.buffer |
| 434 |
ptr.free() |
| 435 |
|
| 436 |
def test_raw_callable_returning_void(self): |
| 437 |
import _rawffi |
| 438 |
_rawffi.FuncPtr(0, [], None) |
| 439 |
|
| 440 |
|
| 441 |
def test_short_addition(self): |
| 442 |
import _rawffi |
| 443 |
lib = _rawffi.CDLL(self.lib_name) |
| 444 |
short_add = lib.ptr('add_shorts', ['h', 'h'], 'H') |
| 445 |
A = _rawffi.Array('h') |
| 446 |
arg1 = A(1) |
| 447 |
arg2 = A(1) |
| 448 |
arg1[0] = 1 |
| 449 |
arg2[0] = 2 |
| 450 |
res = short_add(arg1, arg2) |
| 451 |
assert res[0] == 3 |
| 452 |
arg1.free() |
| 453 |
arg2.free() |
| 454 |
|
| 455 |
def test_pow(self): |
| 456 |
import _rawffi |
| 457 |
libm = _rawffi.CDLL(self.libm_name) |
| 458 |
pow = libm.ptr('pow', ['d', 'd'], 'd') |
| 459 |
A = _rawffi.Array('d') |
| 460 |
arg1 = A(1) |
| 461 |
arg2 = A(1) |
| 462 |
raises(TypeError, "arg1[0] = 'x'") |
| 463 |
arg1[0] = 3 |
| 464 |
arg2[0] = 2.0 |
| 465 |
res = pow(arg1, arg2) |
| 466 |
assert res[0] == 9.0 |
| 467 |
arg1.free() |
| 468 |
arg2.free() |
| 469 |
|
| 470 |
def test_time(self): |
| 471 |
import _rawffi |
| 472 |
libc = _rawffi.get_libc() |
| 473 |
try: |
| 474 |
time = libc.ptr('time', ['z'], 'l') |
| 475 |
except AttributeError: |
| 476 |
|
| 477 |
time = libc.ptr('_time32', ['z'], 'l') |
| 478 |
arg = _rawffi.Array('P')(1) |
| 479 |
arg[0] = 0 |
| 480 |
res = time(arg) |
| 481 |
assert res[0] != 0 |
| 482 |
arg.free() |
| 483 |
|
| 484 |
def test_gettimeofday(self): |
| 485 |
if self.iswin32: |
| 486 |
skip("No gettimeofday on win32") |
| 487 |
import _rawffi |
| 488 |
struct_type = _rawffi.Structure([('tv_sec', 'l'), ('tv_usec', 'l')]) |
| 489 |
structure = struct_type() |
| 490 |
libc = _rawffi.get_libc() |
| 491 |
gettimeofday = libc.ptr('gettimeofday', ['P', 'P'], 'i') |
| 492 |
|
| 493 |
arg1 = structure.byptr() |
| 494 |
arg2 = _rawffi.Array('P')(1) |
| 495 |
res = gettimeofday(arg1, arg2) |
| 496 |
assert res[0] == 0 |
| 497 |
|
| 498 |
struct2 = struct_type() |
| 499 |
arg1[0] = struct2 |
| 500 |
res = gettimeofday(arg1, arg2) |
| 501 |
assert res[0] == 0 |
| 502 |
|
| 503 |
assert structure.tv_usec != struct2.tv_usec |
| 504 |
assert (structure.tv_sec == struct2.tv_sec) or (structure.tv_sec == struct2.tv_sec - 1) |
| 505 |
raises(AttributeError, "structure.xxx") |
| 506 |
structure.free() |
| 507 |
struct2.free() |
| 508 |
arg1.free() |
| 509 |
arg2.free() |
| 510 |
|
| 511 |
def test_structreturn(self): |
| 512 |
import _rawffi |
| 513 |
X = _rawffi.Structure([('x', 'l')]) |
| 514 |
x = X() |
| 515 |
x.x = 121 |
| 516 |
Tm = _rawffi.Structure([('tm_sec', 'i'), |
| 517 |
('tm_min', 'i'), |
| 518 |
('tm_hour', 'i'), |
| 519 |
("tm_mday", 'i'), |
| 520 |
("tm_mon", 'i'), |
| 521 |
("tm_year", 'i'), |
| 522 |
("tm_wday", 'i'), |
| 523 |
("tm_yday", 'i'), |
| 524 |
("tm_isdst", 'i')]) |
| 525 |
libc = _rawffi.get_libc() |
| 526 |
try: |
| 527 |
gmtime = libc.ptr('gmtime', ['P'], 'P') |
| 528 |
except AttributeError: |
| 529 |
|
| 530 |
gmtime = libc.ptr('_gmtime32', ['P'], 'P') |
| 531 |
|
| 532 |
arg = x.byptr() |
| 533 |
res = gmtime(arg) |
| 534 |
t = Tm.fromaddress(res[0]) |
| 535 |
arg.free() |
| 536 |
assert t.tm_year == 70 |
| 537 |
assert t.tm_sec == 1 |
| 538 |
assert t.tm_min == 2 |
| 539 |
x.free() |
| 540 |
|
| 541 |
def test_nested_structures(self): |
| 542 |
import _rawffi |
| 543 |
lib = _rawffi.CDLL(self.lib_name) |
| 544 |
inner = lib.ptr("inner_struct_elem", ['P'], 'c') |
| 545 |
X = _rawffi.Structure([('x1', 'i'), ('x2', 'h'), ('x3', 'c'), ('next', 'P')]) |
| 546 |
next = X() |
| 547 |
next.next = 0 |
| 548 |
next.x3 = b'x' |
| 549 |
x = X() |
| 550 |
x.next = next |
| 551 |
x.x1 = 1 |
| 552 |
x.x2 = 2 |
| 553 |
x.x3 = b'x' |
| 554 |
assert X.fromaddress(x.next).x3 == b'x' |
| 555 |
x.free() |
| 556 |
next.free() |
| 557 |
create_double_struct = lib.ptr("create_double_struct", [], 'P') |
| 558 |
res = create_double_struct() |
| 559 |
x = X.fromaddress(res[0]) |
| 560 |
assert X.fromaddress(x.next).x2 == 3 |
| 561 |
free_double_struct = lib.ptr("free_double_struct", ['P'], None) |
| 562 |
free_double_struct(res) |
| 563 |
|
| 564 |
def test_structure_bitfields_char(self): |
| 565 |
import _rawffi |
| 566 |
X = _rawffi.Structure([('A', 'B', 1), |
| 567 |
('B', 'B', 6), |
| 568 |
('C', 'B', 1)]) |
| 569 |
x = X() |
| 570 |
x.A = 0xf |
| 571 |
x.B = 0xff |
| 572 |
x.C = 0xf |
| 573 |
assert x.A == 1 |
| 574 |
assert x.B == 63 |
| 575 |
assert x.C == 1 |
| 576 |
x.free() |
| 577 |
|
| 578 |
def test_structure_bitfields_varied(self): |
| 579 |
import _rawffi |
| 580 |
X = _rawffi.Structure([('A', 'I', 1), |
| 581 |
('B', 'I', 2), |
| 582 |
('C', 'i', 2)]) |
| 583 |
x = X() |
| 584 |
x.A = 0xf |
| 585 |
x.B = 0xf |
| 586 |
x.C = 0xf |
| 587 |
assert x.A == 1 |
| 588 |
assert x.B == 3 |
| 589 |
assert x.C == -1 |
| 590 |
x.free() |
| 591 |
|
| 592 |
def test_structure_bitfields_int(self): |
| 593 |
import _rawffi |
| 594 |
Y = _rawffi.Structure([('a', 'i', 1), |
| 595 |
('b', 'i', 30), |
| 596 |
('c', 'i', 1)]) |
| 597 |
y = Y() |
| 598 |
y.a, y.b, y.c = -1, -7, 1 |
| 599 |
assert (y.a, y.b, y.c) == (-1, -7, -1) |
| 600 |
y.free() |
| 601 |
|
| 602 |
def test_structure_bitfields_uint(self): |
| 603 |
import _rawffi |
| 604 |
Y = _rawffi.Structure([('a', 'I', 1), |
| 605 |
('b', 'I', 30), |
| 606 |
('c', 'I', 1)]) |
| 607 |
y = Y() |
| 608 |
y.a, y.b, y.c = 7, (1 << 29) | 1, 7 |
| 609 |
assert (y.a, y.b, y.c) == (1, (1 << 29) | 1, 1) |
| 610 |
y.free() |
| 611 |
|
| 612 |
def test_structure_bitfields_longlong(self): |
| 613 |
import _rawffi |
| 614 |
Y = _rawffi.Structure([('a', 'q', 1), |
| 615 |
('b', 'q', 62), |
| 616 |
('c', 'q', 1)]) |
| 617 |
y = Y() |
| 618 |
y.a, y.b, y.c = -1, -7, 1 |
| 619 |
assert (y.a, y.b, y.c) == (-1, -7, -1) |
| 620 |
y.free() |
| 621 |
|
| 622 |
def test_structure_bitfields_ulonglong(self): |
| 623 |
import _rawffi |
| 624 |
Y = _rawffi.Structure([('a', 'Q', 1), |
| 625 |
('b', 'Q', 62), |
| 626 |
('c', 'Q', 1)]) |
| 627 |
y = Y() |
| 628 |
y.a, y.b, y.c = 7, (1 << 61) | 1, 7 |
| 629 |
assert (y.a, y.b, y.c) == (1, (1 << 61) | 1, 1) |
| 630 |
y.free() |
| 631 |
|
| 632 |
def test_structure_bitfields_single_signed(self): |
| 633 |
import _rawffi |
| 634 |
for s in [('i', 32), ('q', 64)]: |
| 635 |
Y = _rawffi.Structure([('a',) + s]) |
| 636 |
y = Y() |
| 637 |
y.a = 10 |
| 638 |
assert y.a == 10 |
| 639 |
val = (1 << (s[1] - 1)) | 1 |
| 640 |
y.a = val |
| 641 |
assert y.a == val - (1 << s[1]) |
| 642 |
y.free() |
| 643 |
|
| 644 |
def test_structure_bitfields_single_unsigned(self): |
| 645 |
import _rawffi |
| 646 |
for s in [('I', 32), ('Q', 64)]: |
| 647 |
Y = _rawffi.Structure([('a',) + s]) |
| 648 |
y = Y() |
| 649 |
y.a = 10 |
| 650 |
assert y.a == 10 |
| 651 |
val = (1 << (s[1] - 1)) | 1 |
| 652 |
y.a = val |
| 653 |
assert y.a == val |
| 654 |
y.free() |
| 655 |
|
| 656 |
def test_invalid_bitfields(self): |
| 657 |
import _rawffi |
| 658 |
raises(TypeError, _rawffi.Structure, [('A', 'c', 1)]) |
| 659 |
raises(ValueError, _rawffi.Structure, [('A', 'I', 129)]) |
| 660 |
raises(ValueError, _rawffi.Structure, [('A', 'I', -1)]) |
| 661 |
raises(ValueError, _rawffi.Structure, [('A', 'I', 0)]) |
| 662 |
|
| 663 |
def test_packed_structure(self): |
| 664 |
import _rawffi |
| 665 |
Y = _rawffi.Structure([('a', 'c'), |
| 666 |
('b', 'i')], pack=1) |
| 667 |
assert Y.size == 5 |
| 668 |
|
| 669 |
def test_array(self): |
| 670 |
import _rawffi |
| 671 |
lib = _rawffi.CDLL(self.lib_name) |
| 672 |
A = _rawffi.Array('i') |
| 673 |
get_array_elem = lib.ptr('get_array_elem', ['P', 'i'], 'i') |
| 674 |
a = A(10) |
| 675 |
a[8] = 3 |
| 676 |
a[7] = 1 |
| 677 |
a[6] = 2 |
| 678 |
arg1 = a.byptr() |
| 679 |
arg2 = A(1) |
| 680 |
for i, expected in enumerate([0, 0, 0, 0, 0, 0, 2, 1, 3, 0]): |
| 681 |
arg2[0] = i |
| 682 |
res = get_array_elem(arg1, arg2) |
| 683 |
assert res[0] == expected |
| 684 |
arg1.free() |
| 685 |
arg2.free() |
| 686 |
assert a[3] == 0 |
| 687 |
a.free() |
| 688 |
|
| 689 |
def test_array_of_structure(self): |
| 690 |
import _rawffi |
| 691 |
lib = _rawffi.CDLL(self.lib_name) |
| 692 |
A = _rawffi.Array('P') |
| 693 |
X = _rawffi.Structure([('x1', 'i'), ('x2', 'h'), ('x3', 'c'), ('next', 'P')]) |
| 694 |
x = X() |
| 695 |
x.x2 = 3 |
| 696 |
a = A(3) |
| 697 |
a[1] = x |
| 698 |
get_array_elem_s = lib.ptr('get_array_elem_s', ['P', 'i'], 'P') |
| 699 |
arg1 = a.byptr() |
| 700 |
arg2 = _rawffi.Array('i')(1) |
| 701 |
res = get_array_elem_s(arg1, arg2) |
| 702 |
assert res[0] == 0 |
| 703 |
arg2[0] = 1 |
| 704 |
res = get_array_elem_s(arg1, arg2) |
| 705 |
assert X.fromaddress(res[0]).x2 == 3 |
| 706 |
assert res[0] == x.buffer |
| 707 |
arg1.free() |
| 708 |
arg2.free() |
| 709 |
x.free() |
| 710 |
a.free() |
| 711 |
|
| 712 |
def test_bad_parameters(self): |
| 713 |
import _rawffi |
| 714 |
lib = _rawffi.CDLL(self.lib_name) |
| 715 |
nothing = lib.ptr('nothing', [], None) |
| 716 |
assert nothing() is None |
| 717 |
raises(AttributeError, "lib.ptr('get_charx', [], None)") |
| 718 |
raises(ValueError, "lib.ptr('get_char', ['xx'], None)") |
| 719 |
raises(ValueError, "lib.ptr('get_char', ['x'], None)") |
| 720 |
raises(ValueError, "lib.ptr('get_char', [], 'x')") |
| 721 |
raises(ValueError, "_rawffi.Structure(['x1', 'xx'])") |
| 722 |
raises(ValueError, _rawffi.Structure, [('x1', 'xx')]) |
| 723 |
raises(ValueError, "_rawffi.Array('xx')") |
| 724 |
|
| 725 |
def test_longs_ulongs(self): |
| 726 |
import _rawffi |
| 727 |
lib = _rawffi.CDLL(self.lib_name) |
| 728 |
some_huge_value = lib.ptr('some_huge_value', [], 'q') |
| 729 |
res = some_huge_value() |
| 730 |
assert res[0] == 1<<42 |
| 731 |
some_huge_uvalue = lib.ptr('some_huge_uvalue', [], 'Q') |
| 732 |
res = some_huge_uvalue() |
| 733 |
assert res[0] == 1<<42 |
| 734 |
pass_ll = lib.ptr('pass_ll', ['q'], 'q') |
| 735 |
arg1 = _rawffi.Array('q')(1) |
| 736 |
arg1[0] = 1<<42 |
| 737 |
res = pass_ll(arg1) |
| 738 |
assert res[0] == 1<<42 |
| 739 |
arg1.free() |
| 740 |
|
| 741 |
def test_callback(self): |
| 742 |
import _rawffi |
| 743 |
import struct |
| 744 |
libc = _rawffi.get_libc() |
| 745 |
ll_to_sort = _rawffi.Array('i')(4) |
| 746 |
for i in range(4): |
| 747 |
ll_to_sort[i] = 4-i |
| 748 |
qsort = libc.ptr('qsort', ['P', 'Z', 'Z', 'P'], None) |
| 749 |
bogus_args = [] |
| 750 |
def compare(a, b): |
| 751 |
a1 = _rawffi.Array('i').fromaddress(_rawffi.Array('P').fromaddress(a, 1)[0], 1) |
| 752 |
a2 = _rawffi.Array('i').fromaddress(_rawffi.Array('P').fromaddress(b, 1)[0], 1) |
| 753 |
if a1[0] not in [1,2,3,4] or a2[0] not in [1,2,3,4]: |
| 754 |
bogus_args.append((a1[0], a2[0])) |
| 755 |
if a1[0] > a2[0]: |
| 756 |
return 1 |
| 757 |
return -1 |
| 758 |
a1 = ll_to_sort.byptr() |
| 759 |
a2 = _rawffi.Array('Z')(1) |
| 760 |
a2[0] = len(ll_to_sort) |
| 761 |
a3 = _rawffi.Array('Z')(1) |
| 762 |
a3[0] = struct.calcsize('i') |
| 763 |
cb = _rawffi.CallbackPtr(compare, ['P', 'P'], 'i') |
| 764 |
a4 = cb.byptr() |
| 765 |
qsort(a1, a2, a3, a4) |
| 766 |
res = [ll_to_sort[i] for i in range(len(ll_to_sort))] |
| 767 |
assert res == [1,2,3,4] |
| 768 |
assert not bogus_args |
| 769 |
a1.free() |
| 770 |
a2.free() |
| 771 |
a3.free() |
| 772 |
a4.free() |
| 773 |
ll_to_sort.free() |
| 774 |
cb.free() |
| 775 |
|
| 776 |
def test_another_callback(self): |
| 777 |
import _rawffi |
| 778 |
lib = _rawffi.CDLL(self.lib_name) |
| 779 |
runcallback = lib.ptr('runcallback', ['P'], 'q') |
| 780 |
def callback(): |
| 781 |
return 1<<42 |
| 782 |
|
| 783 |
cb = _rawffi.CallbackPtr(callback, [], 'q') |
| 784 |
a1 = cb.byptr() |
| 785 |
res = runcallback(a1) |
| 786 |
assert res[0] == 1<<42 |
| 787 |
a1.free() |
| 788 |
cb.free() |
| 789 |
|
| 790 |
def test_void_returning_callback(self): |
| 791 |
import _rawffi |
| 792 |
lib = _rawffi.CDLL(self.lib_name) |
| 793 |
runcallback = lib.ptr('runcallback', ['P'], None) |
| 794 |
called = [] |
| 795 |
def callback(): |
| 796 |
called.append(True) |
| 797 |
|
| 798 |
cb = _rawffi.CallbackPtr(callback, [], None) |
| 799 |
a1 = cb.byptr() |
| 800 |
res = runcallback(a1) |
| 801 |
assert res is None |
| 802 |
assert called == [True] |
| 803 |
a1.free() |
| 804 |
cb.free() |
| 805 |
|
| 806 |
def test_raising_callback(self): |
| 807 |
import _rawffi, sys, dis |
| 808 |
from io import StringIO |
| 809 |
lib = _rawffi.CDLL(self.lib_name) |
| 810 |
err = StringIO() |
| 811 |
orig = sys.stderr |
| 812 |
sys.stderr = err |
| 813 |
try: |
| 814 |
runcallback = lib.ptr('runcallback', ['P'], 'q') |
| 815 |
def callback(): |
| 816 |
1/0 |
| 817 |
|
| 818 |
cb = _rawffi.CallbackPtr(callback, [], 'q') |
| 819 |
a1 = cb.byptr() |
| 820 |
res = runcallback(a1) |
| 821 |
a1.free() |
| 822 |
cb.free() |
| 823 |
val = err.getvalue() |
| 824 |
assert 'ZeroDivisionError' in val |
| 825 |
assert 'callback' in val |
| 826 |
assert res[0] == 0 |
| 827 |
finally: |
| 828 |
sys.stderr = orig |
| 829 |
|
| 830 |
def test_setattr_struct(self): |
| 831 |
import _rawffi |
| 832 |
X = _rawffi.Structure([('value1', 'i'), ('value2', 'i')]) |
| 833 |
x = X() |
| 834 |
x.value1 = 1 |
| 835 |
x.value2 = 2 |
| 836 |
assert x.value1 == 1 |
| 837 |
assert x.value2 == 2 |
| 838 |
x.value1 = 3 |
| 839 |
assert x.value1 == 3 |
| 840 |
raises(AttributeError, "x.foo") |
| 841 |
raises(AttributeError, "x.foo = 1") |
| 842 |
x.free() |
| 843 |
|
| 844 |
def test_sizes_and_alignments(self): |
| 845 |
import _rawffi |
| 846 |
for k, (s, a) in self.sizes_and_alignments.items(): |
| 847 |
assert _rawffi.sizeof(k) == s |
| 848 |
assert _rawffi.alignment(k) == a |
| 849 |
|
| 850 |
def test_unaligned(self): |
| 851 |
import _rawffi |
| 852 |
for k in self.float_typemap: |
| 853 |
S = _rawffi.Structure([('pad', 'c'), ('value', k)], pack=1) |
| 854 |
s = S() |
| 855 |
s.value = 4 |
| 856 |
assert s.value == 4 |
| 857 |
s.free() |
| 858 |
|
| 859 |
def test_array_addressof(self): |
| 860 |
import _rawffi |
| 861 |
lib = _rawffi.CDLL(self.lib_name) |
| 862 |
alloc = lib.ptr('allocate_array', [], 'P') |
| 863 |
A = _rawffi.Array('i') |
| 864 |
res = alloc() |
| 865 |
a = A.fromaddress(res[0], 1) |
| 866 |
assert a[0] == 3 |
| 867 |
assert A.fromaddress(a.buffer, 1)[0] == 3 |
| 868 |
|
| 869 |
def test_shape(self): |
| 870 |
import _rawffi |
| 871 |
A = _rawffi.Array('i') |
| 872 |
a = A(1) |
| 873 |
assert a.shape is A |
| 874 |
a.free() |
| 875 |
S = _rawffi.Structure([('v1', 'i')]) |
| 876 |
s = S() |
| 877 |
s.v1 = 3 |
| 878 |
assert s.shape is S |
| 879 |
s.free() |
| 880 |
|
| 881 |
def test_negative_pointers(self): |
| 882 |
import _rawffi |
| 883 |
A = _rawffi.Array('P') |
| 884 |
a = A(1) |
| 885 |
a[0] = -1234 |
| 886 |
a.free() |
| 887 |
|
| 888 |
def test_long_with_fromaddress(self): |
| 889 |
import _rawffi |
| 890 |
addr = -1 |
| 891 |
raises(ValueError, _rawffi.Array('u').fromaddress, addr, 100) |
| 892 |
|
| 893 |
def test_passing_raw_pointers(self): |
| 894 |
import _rawffi |
| 895 |
lib = _rawffi.CDLL(self.lib_name) |
| 896 |
A = _rawffi.Array('i') |
| 897 |
get_array_elem = lib.ptr('get_array_elem', ['P', 'i'], 'i') |
| 898 |
a = A(1) |
| 899 |
a[0] = 3 |
| 900 |
arg1 = _rawffi.Array('P')(1) |
| 901 |
arg1[0] = a.buffer |
| 902 |
arg2 = _rawffi.Array('i')(1) |
| 903 |
res = get_array_elem(arg1, arg2) |
| 904 |
assert res[0] == 3 |
| 905 |
arg1.free() |
| 906 |
arg2.free() |
| 907 |
a.free() |
| 908 |
|
| 909 |
def test_repr(self): |
| 910 |
import _rawffi, struct |
| 911 |
isize = struct.calcsize("i") |
| 912 |
lsize = struct.calcsize("l") |
| 913 |
assert (repr(_rawffi.Array('i')) == |
| 914 |
"<_rawffi.Array 'i' (%d, %d)>" % (isize, isize)) |
| 915 |
|
| 916 |
|
| 917 |
S = _rawffi.Structure([('x', 'c'), ('y', 'l')]) |
| 918 |
assert (repr(_rawffi.Array((S, 2))) == |
| 919 |
"<_rawffi.Array '\0' (%d, %d)>" % (4*lsize, lsize)) |
| 920 |
|
| 921 |
assert (repr(_rawffi.Structure([('x', 'i'), ('yz', 'i')])) == |
| 922 |
"<_rawffi.Structure 'x' 'yz' (%d, %d)>" % (2*isize, isize)) |
| 923 |
|
| 924 |
s = _rawffi.Structure([('x', 'i'), ('yz', 'i')])() |
| 925 |
assert repr(s) == "<_rawffi struct %x>" % (s.buffer,) |
| 926 |
s.free() |
| 927 |
a = _rawffi.Array('i')(5) |
| 928 |
assert repr(a) == "<_rawffi array %x of length %d>" % (a.buffer, |
| 929 |
len(a)) |
| 930 |
a.free() |
| 931 |
|
| 932 |
def test_wide_char(self): |
| 933 |
import _rawffi, sys |
| 934 |
A = _rawffi.Array('u') |
| 935 |
a = A(3) |
| 936 |
a[0] = u'x' |
| 937 |
a[1] = u'y' |
| 938 |
a[2] = u'z' |
| 939 |
assert a[0] == u'x' |
| 940 |
b = _rawffi.Array('c').fromaddress(a.buffer, 38) |
| 941 |
if _rawffi.sizeof('u') == 4: |
| 942 |
|
| 943 |
if sys.byteorder == 'big': |
| 944 |
assert b[0:8] == b'\x00\x00\x00x\x00\x00\x00y' |
| 945 |
else: |
| 946 |
assert b[0:5] == b'x\x00\x00\x00y' |
| 947 |
else: |
| 948 |
|
| 949 |
print(b[0:4]) |
| 950 |
assert b[0:4] == b'x\x00y\x00' |
| 951 |
a.free() |
| 952 |
|
| 953 |
def test_truncate(self): |
| 954 |
import _rawffi, struct |
| 955 |
a = _rawffi.Array('b')(1) |
| 956 |
a[0] = -5 |
| 957 |
assert a[0] == -5 |
| 958 |
a[0] = 123 |
| 959 |
assert a[0] == 123 |
| 960 |
a[0] = 0x97817182ab128111111111111171817d042 |
| 961 |
assert a[0] == 0x42 |
| 962 |
a[0] = 255 |
| 963 |
assert a[0] == -1 |
| 964 |
a[0] = -2 |
| 965 |
assert a[0] == -2 |
| 966 |
a[0] = -255 |
| 967 |
assert a[0] == 1 |
| 968 |
a.free() |
| 969 |
|
| 970 |
a = _rawffi.Array('B')(1) |
| 971 |
a[0] = 123 |
| 972 |
assert a[0] == 123 |
| 973 |
a[0] = 0x18329b1718b97d89b7198db817d042 |
| 974 |
assert a[0] == 0x42 |
| 975 |
a[0] = 255 |
| 976 |
assert a[0] == 255 |
| 977 |
a[0] = -2 |
| 978 |
assert a[0] == 254 |
| 979 |
a[0] = -255 |
| 980 |
assert a[0] == 1 |
| 981 |
a.free() |
| 982 |
|
| 983 |
a = _rawffi.Array('h')(1) |
| 984 |
a[0] = 123 |
| 985 |
assert a[0] == 123 |
| 986 |
a[0] = 0x9112cbc91bd91db19aaaaaaaaaaaaaa8170d42 |
| 987 |
assert a[0] == 0x0d42 |
| 988 |
a[0] = 65535 |
| 989 |
assert a[0] == -1 |
| 990 |
a[0] = -2 |
| 991 |
assert a[0] == -2 |
| 992 |
a[0] = -65535 |
| 993 |
assert a[0] == 1 |
| 994 |
a.free() |
| 995 |
|
| 996 |
a = _rawffi.Array('H')(1) |
| 997 |
a[0] = 123 |
| 998 |
assert a[0] == 123 |
| 999 |
a[0] = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeee817d042 |
| 1000 |
assert a[0] == 0xd042 |
| 1001 |
a[0] = -2 |
| 1002 |
assert a[0] == 65534 |
| 1003 |
a.free() |
| 1004 |
|
| 1005 |
maxptr = (256 ** struct.calcsize("P")) - 1 |
| 1006 |
a = _rawffi.Array('P')(1) |
| 1007 |
a[0] = 123 |
| 1008 |
assert a[0] == 123 |
| 1009 |
a[0] = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeee817d042 |
| 1010 |
assert a[0] == 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeee817d042 & maxptr |
| 1011 |
a[0] = -2 |
| 1012 |
assert a[0] == maxptr - 1 |
| 1013 |
a.free() |
| 1014 |
|
| 1015 |
def test_getaddressindll(self): |
| 1016 |
import _rawffi |
| 1017 |
lib = _rawffi.CDLL(self.lib_name) |
| 1018 |
def getprimitive(typecode, name): |
| 1019 |
addr = lib.getaddressindll(name) |
| 1020 |
return _rawffi.Array(typecode).fromaddress(addr, 1) |
| 1021 |
a = getprimitive("l", "static_int") |
| 1022 |
assert a[0] == 42 |
| 1023 |
a[0] = 43 |
| 1024 |
assert a[0] == 43 |
| 1025 |
a = getprimitive("d", "static_double") |
| 1026 |
assert a[0] == 42.42 |
| 1027 |
a[0] = 43.43 |
| 1028 |
assert a[0] == 43.43 |
| 1029 |
a = getprimitive("g", "static_longdouble") |
| 1030 |
assert a[0] == 42.42 |
| 1031 |
a[0] = 43.43 |
| 1032 |
assert a[0] == 43.43 |
| 1033 |
raises(ValueError, getprimitive, 'z', 'ddddddd') |
| 1034 |
raises(ValueError, getprimitive, 'zzz', 'static_int') |
| 1035 |
|
| 1036 |
def test_segfault_exception(self): |
| 1037 |
import _rawffi |
| 1038 |
S = _rawffi.Structure([('x', 'i')]) |
| 1039 |
s = S() |
| 1040 |
s.x = 3 |
| 1041 |
s.free() |
| 1042 |
raises(_rawffi.SegfaultException, s.__getattr__, 'x') |
| 1043 |
raises(_rawffi.SegfaultException, s.__setattr__, 'x', 3) |
| 1044 |
A = _rawffi.Array('c') |
| 1045 |
a = A(13) |
| 1046 |
a.free() |
| 1047 |
raises(_rawffi.SegfaultException, a.__getitem__, 3) |
| 1048 |
raises(_rawffi.SegfaultException, a.__setitem__, 3, 3) |
| 1049 |
|
| 1050 |
def test_stackcheck(self): |
| 1051 |
if self.platform != "msvc" or self.is64bit: |
| 1052 |
skip("32-bit win32 msvc specific") |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
import _rawffi |
| 1057 |
lib = _rawffi.CDLL('kernel32') |
| 1058 |
|
| 1059 |
f = lib.ptr('SetLastError', [], 'i') |
| 1060 |
try: |
| 1061 |
f() |
| 1062 |
except ValueError as e: |
| 1063 |
assert "Procedure called with not enough arguments" in str(e) |
| 1064 |
else: |
| 1065 |
assert 0, "Did not raise" |
| 1066 |
|
| 1067 |
f = lib.ptr('GetLastError', ['i'], None, |
| 1068 |
flags=_rawffi.FUNCFLAG_STDCALL) |
| 1069 |
arg = _rawffi.Array('i')(1) |
| 1070 |
arg[0] = 1 |
| 1071 |
try: |
| 1072 |
f(arg) |
| 1073 |
except ValueError as e: |
| 1074 |
assert "Procedure called with too many arguments" in str(e) |
| 1075 |
else: |
| 1076 |
assert 0, "Did not raise" |
| 1077 |
arg.free() |
| 1078 |
|
| 1079 |
def test_struct_byvalue(self): |
| 1080 |
import _rawffi, sys |
| 1081 |
X_Y = _rawffi.Structure([('x', 'l'), ('y', 'l')]) |
| 1082 |
x_y = X_Y() |
| 1083 |
lib = _rawffi.CDLL(self.lib_name) |
| 1084 |
sum_x_y = lib.ptr('sum_x_y', [(X_Y, 1)], 'l') |
| 1085 |
x_y.x = 200 |
| 1086 |
x_y.y = 220 |
| 1087 |
res = sum_x_y(x_y) |
| 1088 |
assert res[0] == 420 |
| 1089 |
x_y.free() |
| 1090 |
|
| 1091 |
def test_callback_struct_byvalue(self): |
| 1092 |
import _rawffi, sys |
| 1093 |
X_Y = _rawffi.Structure([('x', 'l'), ('y', 'l')]) |
| 1094 |
lib = _rawffi.CDLL(self.lib_name) |
| 1095 |
op_x_y = lib.ptr('op_x_y', [(X_Y, 1), 'P'], 'l') |
| 1096 |
|
| 1097 |
def callback(x_y): |
| 1098 |
return x_y.x + x_y.y |
| 1099 |
cb = _rawffi.CallbackPtr(callback, [(X_Y, 1)], 'l') |
| 1100 |
|
| 1101 |
x_y = X_Y() |
| 1102 |
x_y.x = 200 |
| 1103 |
x_y.y = 220 |
| 1104 |
|
| 1105 |
a1 = cb.byptr() |
| 1106 |
res = op_x_y(x_y, a1) |
| 1107 |
a1.free() |
| 1108 |
x_y.free() |
| 1109 |
cb.free() |
| 1110 |
|
| 1111 |
assert res[0] == 420 |
| 1112 |
|
| 1113 |
def test_ret_struct(self): |
| 1114 |
import _rawffi |
| 1115 |
S2H = _rawffi.Structure([('x', 'h'), ('y', 'h')]) |
| 1116 |
s2h = S2H() |
| 1117 |
lib = _rawffi.CDLL(self.lib_name) |
| 1118 |
give = lib.ptr('give', ['h', 'h'], (S2H, 1)) |
| 1119 |
a1 = _rawffi.Array('h')(1) |
| 1120 |
a2 = _rawffi.Array('h')(1) |
| 1121 |
a1[0] = 13 |
| 1122 |
a2[0] = 17 |
| 1123 |
res = give(a1, a2) |
| 1124 |
assert isinstance(res, _rawffi.StructureInstanceAutoFree) |
| 1125 |
assert res.shape is S2H |
| 1126 |
assert res.x == 13 |
| 1127 |
assert res.y == 17 |
| 1128 |
a1.free() |
| 1129 |
a2.free() |
| 1130 |
|
| 1131 |
s2h.x = 7 |
| 1132 |
s2h.y = 11 |
| 1133 |
perturb = lib.ptr('perturb', [(S2H, 1)], (S2H, 1)) |
| 1134 |
res = perturb(s2h) |
| 1135 |
assert isinstance(res, _rawffi.StructureInstanceAutoFree) |
| 1136 |
assert res.shape is S2H |
| 1137 |
assert res.x == 14 |
| 1138 |
assert res.y == 33 |
| 1139 |
assert s2h.x == 7 |
| 1140 |
assert s2h.y == 11 |
| 1141 |
|
| 1142 |
s2h.free() |
| 1143 |
|
| 1144 |
def test_ret_struct_containing_array(self): |
| 1145 |
import _rawffi |
| 1146 |
AoI = _rawffi.Array('i') |
| 1147 |
S2A = _rawffi.Structure([('bah', (AoI, 2))]) |
| 1148 |
lib = _rawffi.CDLL(self.lib_name) |
| 1149 |
get_s2a = lib.ptr('get_s2a', [], (S2A, 1)) |
| 1150 |
check_s2a = lib.ptr('check_s2a', [(S2A, 1)], 'i') |
| 1151 |
|
| 1152 |
res = get_s2a() |
| 1153 |
assert isinstance(res, _rawffi.StructureInstanceAutoFree) |
| 1154 |
assert res.shape is S2A |
| 1155 |
ok = check_s2a(res) |
| 1156 |
assert ok[0] == 1 |
| 1157 |
|
| 1158 |
def test_buffer(self): |
| 1159 |
import _rawffi |
| 1160 |
S = _rawffi.Structure((40, 1)) |
| 1161 |
s = S(autofree=True) |
| 1162 |
b = memoryview(s) |
| 1163 |
assert len(b) == 40 |
| 1164 |
b[4] = ord(b'X') |
| 1165 |
b[:3] = b'ABC' |
| 1166 |
assert b[:6] == b'ABC\x00X\x00' |
| 1167 |
|
| 1168 |
A = _rawffi.Array('c') |
| 1169 |
a = A(10, autofree=True) |
| 1170 |
a[3] = b'x' |
| 1171 |
b = memoryview(a) |
| 1172 |
assert len(b) == 10 |
| 1173 |
assert b[3] == b'x' |
| 1174 |
b[6] = b'y' |
| 1175 |
assert a[6] == b'y' |
| 1176 |
b[3:5] = b'zt' |
| 1177 |
assert a[3] == b'z' |
| 1178 |
assert a[4] == b't' |
| 1179 |
|
| 1180 |
b = memoryview(a) |
| 1181 |
assert len(b) == 10 |
| 1182 |
assert b[3] == b'z' |
| 1183 |
b[3] = b'x' |
| 1184 |
assert b[3] == b'x' |
| 1185 |
|
| 1186 |
def test_pypy_raw_address(self): |
| 1187 |
import _rawffi |
| 1188 |
S = _rawffi.Structure((40, 1)) |
| 1189 |
s = S(autofree=True) |
| 1190 |
addr = memoryview(s)._pypy_raw_address() |
| 1191 |
assert type(addr) is int |
| 1192 |
assert memoryview(s)._pypy_raw_address() == addr |
| 1193 |
assert memoryview(s)[10:]._pypy_raw_address() == addr + 10 |
| 1194 |
|
| 1195 |
def test_union(self): |
| 1196 |
import _rawffi |
| 1197 |
longsize = _rawffi.sizeof('l') |
| 1198 |
S = _rawffi.Structure([('x', 'h'), ('y', 'l')], union=True) |
| 1199 |
s = S(autofree=False) |
| 1200 |
s.x = 12345 |
| 1201 |
lib = _rawffi.CDLL(self.lib_name) |
| 1202 |
f = lib.ptr('ret_un_func', [(S, 1)], (S, 1)) |
| 1203 |
ret = f(s) |
| 1204 |
assert ret.y == 1234500, "ret.y == %d" % (ret.y,) |
| 1205 |
s.free() |
| 1206 |
|
| 1207 |
def test_ffi_type(self): |
| 1208 |
import _rawffi |
| 1209 |
EMPTY = _rawffi.Structure([]) |
| 1210 |
S2E = _rawffi.Structure([('bah', (EMPTY, 1))]) |
| 1211 |
S2E.get_ffi_type() |
| 1212 |
|
| 1213 |
def test_overflow_error(self): |
| 1214 |
import _rawffi |
| 1215 |
A = _rawffi.Array('d') |
| 1216 |
arg1 = A(1) |
| 1217 |
raises(OverflowError, "arg1[0] = 10**900") |
| 1218 |
arg1.free() |
| 1219 |
|
| 1220 |
def test_errno(self): |
| 1221 |
import _rawffi |
| 1222 |
lib = _rawffi.CDLL(self.lib_name) |
| 1223 |
A = _rawffi.Array('i') |
| 1224 |
f = lib.ptr('check_errno', ['i'], 'i') |
| 1225 |
_rawffi.set_errno(42) |
| 1226 |
arg = A(1) |
| 1227 |
arg[0] = 43 |
| 1228 |
res = f(arg) |
| 1229 |
assert res[0] == 42 |
| 1230 |
z = _rawffi.get_errno() |
| 1231 |
assert z == 43 |
| 1232 |
arg.free() |
| 1233 |
|
| 1234 |
def test_last_error(self): |
| 1235 |
import sys |
| 1236 |
if sys.platform != 'win32': |
| 1237 |
skip("Windows test") |
| 1238 |
import _rawffi |
| 1239 |
lib = _rawffi.CDLL(self.lib_name) |
| 1240 |
A = _rawffi.Array('i') |
| 1241 |
f = lib.ptr('check_last_error', ['i'], 'i') |
| 1242 |
_rawffi.set_last_error(42) |
| 1243 |
arg = A(1) |
| 1244 |
arg[0] = 43 |
| 1245 |
res = f(arg) |
| 1246 |
assert res[0] == 42 |
| 1247 |
z = _rawffi.get_last_error() |
| 1248 |
assert z == 43 |
| 1249 |
arg.free() |
| 1250 |
|
| 1251 |
def test_char_array_int(self): |
| 1252 |
import _rawffi |
| 1253 |
A = _rawffi.Array('c') |
| 1254 |
a = A(1) |
| 1255 |
a[0] = b'a' |
| 1256 |
assert a[0] == b'a' |
| 1257 |
|
| 1258 |
a[0] = 100 |
| 1259 |
assert a[0] == b'd' |
| 1260 |
a.free() |
| 1261 |
|
| 1262 |
def test_cdll_name(self): |
| 1263 |
import _rawffi |
| 1264 |
lib = _rawffi.CDLL(self.lib_name) |
| 1265 |
assert lib.name == self.lib_name |
| 1266 |
|
| 1267 |
def test_wcharp2rawunicode(self): |
| 1268 |
import _rawffi |
| 1269 |
A = _rawffi.Array('i') |
| 1270 |
arg = A(1) |
| 1271 |
arg[0] = 0x1234 |
| 1272 |
u = _rawffi.wcharp2rawunicode(arg.itemaddress(0)) |
| 1273 |
assert u == u'\u1234' |
| 1274 |
u = _rawffi.wcharp2rawunicode(arg.itemaddress(0), 1) |
| 1275 |
assert u == u'\u1234' |
| 1276 |
arg[0] = -1 |
| 1277 |
if _rawffi.sizeof('u') == 4 and self.runappdirect: |
| 1278 |
|
| 1279 |
|
| 1280 |
raises(ValueError, _rawffi.wcharp2rawunicode, arg.itemaddress(0)) |
| 1281 |
raises(ValueError, _rawffi.wcharp2rawunicode, arg.itemaddress(0), 1) |
| 1282 |
arg[0] = 0x110000 |
| 1283 |
raises(ValueError, _rawffi.wcharp2rawunicode, arg.itemaddress(0)) |
| 1284 |
raises(ValueError, _rawffi.wcharp2rawunicode, arg.itemaddress(0), 1) |
| 1285 |
arg.free() |
| 1286 |
|
| 1287 |
def test_variadic_sum(self): |
| 1288 |
import _rawffi |
| 1289 |
lib = _rawffi.CDLL(self.lib_name) |
| 1290 |
|
| 1291 |
A = _rawffi.Array('l') |
| 1292 |
n = A(1) |
| 1293 |
arg1 = A(1) |
| 1294 |
arg2 = A(1) |
| 1295 |
n[0] = 2 |
| 1296 |
arg1[0] = 3 |
| 1297 |
arg2[0] = 15 |
| 1298 |
variadic_sum = lib.ptr('variadic_sum', ['l', 'l', 'l'], 'l', variadic_args=2) |
| 1299 |
res = variadic_sum(n, arg1, arg2) |
| 1300 |
assert res[0] == 3 + 15 |
| 1301 |
arg1.free() |
| 1302 |
arg2.free() |
| 1303 |
n.free() |
| 1304 |
|
| 1305 |
class AppTestAutoFree: |
| 1306 |
spaceconfig = dict(usemodules=['_rawffi', 'struct']) |
| 1307 |
|
| 1308 |
def setup_class(cls): |
| 1309 |
cls.w_sizes_and_alignments = cls.space.wrap(dict( |
| 1310 |
[(k, (v.c_size, v.c_alignment)) for k,v in TYPEMAP.iteritems()])) |
| 1311 |
Tracker.DO_TRACING = True |
| 1312 |
|
| 1313 |
def test_structure_autofree(self): |
| 1314 |
import gc, _rawffi |
| 1315 |
gc.collect() |
| 1316 |
gc.collect() |
| 1317 |
S = _rawffi.Structure([('x', 'i')]) |
| 1318 |
try: |
| 1319 |
oldnum = _rawffi._num_of_allocated_objects() |
| 1320 |
except RuntimeError: |
| 1321 |
oldnum = '?' |
| 1322 |
s = S(autofree=True) |
| 1323 |
s.x = 3 |
| 1324 |
s = None |
| 1325 |
gc.collect() |
| 1326 |
if oldnum != '?': |
| 1327 |
assert oldnum == _rawffi._num_of_allocated_objects() |
| 1328 |
|
| 1329 |
def test_array_autofree(self): |
| 1330 |
import gc, _rawffi |
| 1331 |
gc.collect() |
| 1332 |
try: |
| 1333 |
oldnum = _rawffi._num_of_allocated_objects() |
| 1334 |
except RuntimeError: |
| 1335 |
oldnum = '?' |
| 1336 |
|
| 1337 |
A = _rawffi.Array('c') |
| 1338 |
a = A(6, b'xxyxx\x00', autofree=True) |
| 1339 |
assert _rawffi.charp2string(a.buffer) == b'xxyxx' |
| 1340 |
a = None |
| 1341 |
gc.collect() |
| 1342 |
if oldnum != '?': |
| 1343 |
assert oldnum == _rawffi._num_of_allocated_objects() |
| 1344 |
|
| 1345 |
def teardown_class(cls): |
| 1346 |
Tracker.DO_TRACING = False |