def test_array_addressof(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) alloc = lib.ptr('allocate_array', [], 'P') A = _rawffi.Array('i') res = alloc() a = A.fromaddress(res[0], 1) assert a[0] == 3 assert A.fromaddress(a.buffer, 1)[0] == 3
def test_byordinal(self): if not self.iswin32: skip("win32 specific") import _rawffi lib = _rawffi.CDLL(self.lib_name) # This will call the ordinal function numbered 1 # my compiler seems to order them alphabetically: # AAA_first_ordinal_function assert lib.ptr(1, [], 'i')()[0] == 42
def test_time(self): import _rawffi libc = _rawffi.CDLL('libc.so.6') time = libc.ptr('time', ['z'], 'l') # 'z' instead of 'P' just for test arg = _rawffi.Array('P')(1) arg[0] = 0 res = time(arg) assert res[0] != 0 res.free() arg.free()
def test_libload_fail(self): import _rawffi try: _rawffi.CDLL("xxxxx_this_name_does_not_exist_xxxxx") except OSError as e: print e assert str(e).startswith( "Cannot load library xxxxx_this_name_does_not_exist_xxxxx: ") else: raise AssertionError("did not fail??")
def test_struct_byvalue(self): import _rawffi, sys X_Y = _rawffi.Structure([('x', 'l'), ('y', 'l')]) x_y = X_Y() lib = _rawffi.CDLL(self.lib_name) sum_x_y = lib.ptr('sum_x_y', [(X_Y, 1)], 'l') x_y.x = 200 x_y.y = 220 res = sum_x_y(x_y) assert res[0] == 420 x_y.free()
def test_union(self): import _rawffi longsize = _rawffi.sizeof('l') S = _rawffi.Structure([('x', 'h'), ('y', 'l')], union=True) s = S(autofree=False) s.x = 12345 lib = _rawffi.CDLL(self.lib_name) f = lib.ptr('ret_un_func', [(S, 1)], (S, 1)) ret = f(s) assert ret.y == 1234500, "ret.y == %d" % (ret.y, ) s.free()
def test_short_addition(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) short_add = lib.ptr('add_shorts', ['h', 'h'], 'H') A = _rawffi.Array('h') arg1 = A(1, autofree=True) arg2 = A(1, autofree=True) arg1[0] = 1 arg2[0] = 2 res = short_add(arg1, arg2) assert res[0] == 3
def test_libload_None(self): import _rawffi # this should return *all* loaded libs, dlopen(NULL) try: dll = _rawffi.CDLL(None) except TypeError: if not self.iswin32: raise else: func = dll.ptr('rand', [], 'i') res = func() assert res[0] != 0
def test_bad_parameters(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) nothing = lib.ptr('nothing', [], None) assert nothing() is None raises(AttributeError, "lib.ptr('get_charx', [], None)") raises(ValueError, "lib.ptr('get_char', ['xx'], None)") raises(ValueError, "lib.ptr('get_char', ['x'], None)") raises(ValueError, "lib.ptr('get_char', [], 'x')") raises(ValueError, "_rawffi.Structure(['x1', 'xx'])") raises(ValueError, _rawffi.Structure, [('x1', 'xx')]) raises(ValueError, "_rawffi.Array('xx')")
def test_ret_struct_containing_array(self): import _rawffi AoI = _rawffi.Array('i') S2A = _rawffi.Structure([('bah', (AoI, 2))]) lib = _rawffi.CDLL(self.lib_name) get_s2a = lib.ptr('get_s2a', [], (S2A, 1)) check_s2a = lib.ptr('check_s2a', [(S2A, 1)], 'i') res = get_s2a() assert isinstance(res, _rawffi.StructureInstanceAutoFree) assert res.shape is S2A ok = check_s2a(res) assert ok[0] == 1
def test_another_callback(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) runcallback = lib.ptr('runcallback', ['P'], 'q') def callback(): return 1<<42 cb = _rawffi.CallbackPtr(callback, [], 'q') a1 = cb.byptr() res = runcallback(a1) assert res[0] == 1<<42 a1.free() cb.free()
def test_errno(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) A = _rawffi.Array('i') f = lib.ptr('check_errno', ['i'], 'i') _rawffi.set_errno(42) arg = A(1) arg[0] = 43 res = f(arg) assert res[0] == 42 z = _rawffi.get_errno() assert z == 43 arg.free()
def test_stackcheck(self): if self.platform != "msvc": skip("win32 msvc specific") # Even if the call corresponds to the specified signature, # the STDCALL calling convention may detect some errors import _rawffi lib = _rawffi.CDLL('kernel32') f = lib.ptr('SetLastError', [], 'i') try: f() except ValueError, e: assert "Procedure called with not enough arguments" in e.message
def test_getaddressindll(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) def getprimitive(typecode, name): addr = lib.getaddressindll(name) return _rawffi.Array(typecode).fromaddress(addr, 1) a = getprimitive("l", "static_int") assert a[0] == 42 a = getprimitive("d", "static_double") assert a[0] == 42.42 raises(ValueError, getprimitive, 'z', 'ddddddd') raises(ValueError, getprimitive, 'zzz', 'static_int')
def test_pow(self): import _rawffi libm = _rawffi.CDLL(self.libm_name) pow = libm.ptr('pow', ['d', 'd'], 'd') A = _rawffi.Array('d') arg1 = A(1) arg2 = A(1) raises(TypeError, "arg1[0] = 'x'") arg1[0] = 3 arg2[0] = 2.0 res = pow(arg1, arg2) assert res[0] == 9.0 arg1.free() arg2.free()
def test_struct_byvalue(self): import _rawffi, sys X_Y = _rawffi.Structure([('x', 'l'), ('y', 'l')]) x_y = X_Y() lib = _rawffi.CDLL(self.lib_name) print >> sys.stderr, "getting..." sum_x_y = lib.ptr('sum_x_y', [(X_Y, 1)], 'l') x_y.x = 200 x_y.y = 220 print >> sys.stderr, "calling..." res = sum_x_y(x_y) print >> sys.stderr, "done" assert res[0] == 420 x_y.free()
def test_void_returning_callback(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) runcallback = lib.ptr('runcallback', ['P'], None) called = [] def callback(): called.append(True) cb = _rawffi.CallbackPtr(callback, [], None) a1 = cb.byptr() res = runcallback(a1) assert res is None assert called == [True] a1.free() cb.free()
def test_passing_raw_pointers(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) A = _rawffi.Array('i') get_array_elem = lib.ptr('get_array_elem', ['P', 'i'], 'i') a = A(1) a[0] = 3 arg1 = _rawffi.Array('P')(1) arg1[0] = a.buffer arg2 = _rawffi.Array('i')(1) res = get_array_elem(arg1, arg2) assert res[0] == 3 arg1.free() arg2.free() a.free()
def test_longs_ulongs(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) some_huge_value = lib.ptr('some_huge_value', [], 'q') res = some_huge_value() assert res[0] == 1 << 42 some_huge_uvalue = lib.ptr('some_huge_uvalue', [], 'Q') res = some_huge_uvalue() assert res[0] == 1 << 42 pass_ll = lib.ptr('pass_ll', ['q'], 'q') arg1 = _rawffi.Array('q')(1) arg1[0] = 1 << 42 res = pass_ll(arg1) assert res[0] == 1 << 42 arg1.free()
def test_struct_byvalue(self): if self.isx86_64: skip("Segfaults on x86_64 because small structures " "may be passed in registers and " "c_elements must not be null") import _rawffi X_Y = _rawffi.Structure([('x', 'l'), ('y', 'l')]) x_y = X_Y() lib = _rawffi.CDLL(self.lib_name) sum_x_y = lib.ptr('sum_x_y', [(X_Y, 1)], 'l') x_y.x = 200 x_y.y = 220 res = sum_x_y(x_y) assert res[0] == 420 x_y.free()
def test_getchar(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) get_char = lib.ptr('get_char', ['P', 'H'], 'c') A = _rawffi.Array('c') B = _rawffi.Array('H') dupa = A(5, 'dupa') dupaptr = dupa.byptr() for i in range(4): intptr = B(1) intptr[0] = i res = get_char(dupaptr, intptr) assert res[0] == 'dupa'[i] intptr.free() dupaptr.free() dupa.free()
def test_last_error(self): import sys if sys.platform != 'win32': skip("Windows test") import _rawffi lib = _rawffi.CDLL(self.lib_name) A = _rawffi.Array('i') f = lib.ptr('check_last_error', ['i'], 'i') _rawffi.set_last_error(42) arg = A(1) arg[0] = 43 res = f(arg) assert res[0] == 42 z = _rawffi.get_last_error() assert z == 43 arg.free()
def test_variadic_sum(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) A = _rawffi.Array('l') n = A(1) arg1 = A(1) arg2 = A(1) n[0] = 2 arg1[0] = 3 arg2[0] = 15 variadic_sum = lib.ptr('variadic_sum', ['l', 'l', 'l'], 'l', variadic_args=2) res = variadic_sum(n, arg1, arg2) assert res[0] == 3 + 15 arg1.free() arg2.free() n.free()
def test_array(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) A = _rawffi.Array('i') get_array_elem = lib.ptr('get_array_elem', ['P', 'i'], 'i') a = A(10) a[8] = 3 a[7] = 1 a[6] = 2 arg1 = a.byptr() arg2 = A(1) for i, expected in enumerate([0, 0, 0, 0, 0, 0, 2, 1, 3, 0]): arg2[0] = i res = get_array_elem(arg1, arg2) assert res[0] == expected arg1.free() arg2.free() assert a[3] == 0 a.free()
def test_raw_callable(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) get_raw_pointer = lib.ptr('get_raw_pointer', [], 'P') ptr = get_raw_pointer() rawcall = _rawffi.FuncPtr(ptr[0], ['h', 'h'], 'H') A = _rawffi.Array('h') arg1 = A(1) arg2 = A(1) arg1[0] = 1 arg2[0] = 2 res = rawcall(arg1, arg2) assert res[0] == 3 arg1.free() arg2.free() assert rawcall.buffer == ptr[0] ptr = rawcall.byptr() assert ptr[0] == rawcall.buffer ptr.free()
def test_callback_struct_byvalue(self): import _rawffi, sys X_Y = _rawffi.Structure([('x', 'l'), ('y', 'l')]) lib = _rawffi.CDLL(self.lib_name) op_x_y = lib.ptr('op_x_y', [(X_Y, 1), 'P'], 'l') def callback(x_y): return x_y.x + x_y.y cb = _rawffi.CallbackPtr(callback, [(X_Y, 1)], 'l') x_y = X_Y() x_y.x = 200 x_y.y = 220 a1 = cb.byptr() res = op_x_y(x_y, a1) a1.free() x_y.free() cb.free() assert res[0] == 420
def test_nested_structures(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) inner = lib.ptr("inner_struct_elem", ['P'], 'c') X = _rawffi.Structure([('x1', 'i'), ('x2', 'h'), ('x3', 'c'), ('next', 'P')]) next = X() next.next = 0 next.x3 = 'x' x = X() x.next = next x.x1 = 1 x.x2 = 2 x.x3 = 'x' assert X.fromaddress(x.next).x3 == 'x' x.free() next.free() create_double_struct = lib.ptr("create_double_struct", [], 'P') res = create_double_struct() x = X.fromaddress(res[0]) assert X.fromaddress(x.next).x2 == 3 free_double_struct = lib.ptr("free_double_struct", ['P'], None) free_double_struct(res)
def test_array_of_structure(self): import _rawffi lib = _rawffi.CDLL(self.lib_name) A = _rawffi.Array('P') X = _rawffi.Structure([('x1', 'i'), ('x2', 'h'), ('x3', 'c'), ('next', 'P')]) x = X() x.x2 = 3 a = A(3) a[1] = x get_array_elem_s = lib.ptr('get_array_elem_s', ['P', 'i'], 'P') arg1 = a.byptr() arg2 = _rawffi.Array('i')(1) res = get_array_elem_s(arg1, arg2) assert res[0] == 0 arg2[0] = 1 res = get_array_elem_s(arg1, arg2) assert X.fromaddress(res[0]).x2 == 3 assert res[0] == x.buffer arg1.free() arg2.free() x.free() a.free()
def test_structreturn(self): import _rawffi X = _rawffi.Structure([('x', 'l')]) x = X() x.x = 121 Tm = _rawffi.Structure([('tm_sec', 'i'), ('tm_min', 'i'), ('tm_hour', 'i'), ("tm_mday", 'i'), ("tm_mon", 'i'), ("tm_year", 'i'), ("tm_wday", 'i'), ("tm_yday", 'i'), ("tm_isdst", 'i')]) libc = _rawffi.CDLL('libc.so.6') gmtime = libc.ptr('gmtime', ['P'], 'P') arg = x.byptr() res = gmtime(arg) t = Tm.fromaddress(res[0]) res.free() arg.free() assert t.tm_year == 70 assert t.tm_sec == 1 assert t.tm_min == 2 x.free()
def test_raising_callback(self): import _rawffi, sys import StringIO lib = _rawffi.CDLL(self.lib_name) err = StringIO.StringIO() orig = sys.stderr sys.stderr = err try: runcallback = lib.ptr('runcallback', ['P'], 'q') def callback(): 1 / 0 cb = _rawffi.CallbackPtr(callback, [], 'q') a1 = cb.byptr() res = runcallback(a1) a1.free() del cb val = err.getvalue() assert 'ZeroDivisionError' in val assert 'callback' in val finally: sys.stderr = orig