def main(): import os try: import _cffi_backend except ImportError: sys.stderr.write('SKIP: cannot import _cffi_backend\n') return 0 libc = _cffi_backend.load_library(None) BInt = _cffi_backend.new_primitive_type("int") BSizeT = _cffi_backend.new_primitive_type("size_t") BChar = _cffi_backend.new_primitive_type("char") BCharP = _cffi_backend.new_pointer_type(BChar) BWrite = _cffi_backend.new_function_type( [BInt, BCharP, BSizeT], BSizeT) # not signed here! _write = libc.load_function(BWrite, 'write') i = 0 fd0, fd1 = os.pipe() buffer = _cffi_backend.newp(BCharP, 'A') while i < 300: tmp = _write(fd1, buffer, 1) # ID: cfficall assert tmp == 1 assert os.read(fd0, 2) == 'A' i += 1 os.close(fd0) os.close(fd1) return 42
def main(): import os try: import _cffi_backend except ImportError: sys.stderr.write('SKIP: cannot import _cffi_backend\n') return 0 libc = _cffi_backend.load_library(None) BInt = _cffi_backend.new_primitive_type("int") BSizeT = _cffi_backend.new_primitive_type("size_t") BChar = _cffi_backend.new_primitive_type("char") BCharP = _cffi_backend.new_pointer_type(BChar) BWrite = _cffi_backend.new_function_type([BInt, BCharP, BSizeT], BSizeT) # not signed here! _write = libc.load_function(BWrite, 'write') i = 0 fd0, fd1 = os.pipe() buffer = _cffi_backend.newp(BCharP, 'A') while i < 300: tmp = _write(fd1, buffer, 1) # ID: cfficall assert tmp == 1 assert os.read(fd0, 2) == 'A' i += 1 os.close(fd0) os.close(fd1) return 42
def test_bug(self): import _cffi_backend LONG = _cffi_backend.new_primitive_type('long') five = _cffi_backend.cast(LONG, 5) raises(TypeError, list, five) DOUBLE = _cffi_backend.new_primitive_type('double') five_and_a_half = _cffi_backend.cast(DOUBLE, 5.5) raises(TypeError, list, five_and_a_half)
def test_fast_init_clongdouble_from_list(self): import _cffi_backend LONGDOUBLE = _cffi_backend.new_primitive_type('long double') P_LONGDOUBLE = _cffi_backend.new_pointer_type(LONGDOUBLE) LONGDOUBLE_ARRAY = _cffi_backend.new_array_type(P_LONGDOUBLE, None) buf = _cffi_backend.newp(LONGDOUBLE_ARRAY, [1.25, -3.5]) assert float(buf[0]) == 1.25 assert float(buf[1]) == -3.5
def test_bug_not_list_or_tuple(self): import _cffi_backend LONG = _cffi_backend.new_primitive_type('long') P_LONG = _cffi_backend.new_pointer_type(LONG) LONG_ARRAY_2 = _cffi_backend.new_array_type(P_LONG, 2) P_LONG_ARRAY_2 = _cffi_backend.new_pointer_type(LONG_ARRAY_2) LONG_ARRAY_ARRAY = _cffi_backend.new_array_type(P_LONG_ARRAY_2, None) raises(TypeError, _cffi_backend.newp, LONG_ARRAY_ARRAY, [set([4, 5])])
def test_TypeError_if_no_length(self): import _cffi_backend LONG = _cffi_backend.new_primitive_type('long') P_LONG = _cffi_backend.new_pointer_type(LONG) LONG_ARRAY = _cffi_backend.new_array_type(P_LONG, 3) buf = _cffi_backend.newp(LONG_ARRAY) pbuf = _cffi_backend.cast(P_LONG, buf) raises(TypeError, "list(pbuf)")
def test_fast_init_cfloat_from_list(self): import _cffi_backend FLOAT = _cffi_backend.new_primitive_type('float') P_FLOAT = _cffi_backend.new_pointer_type(FLOAT) FLOAT_ARRAY = _cffi_backend.new_array_type(P_FLOAT, None) buf = _cffi_backend.newp(FLOAT_ARRAY, [1.25, -3.5]) assert buf[0] == 1.25 assert buf[1] == -3.5
def test_fast_init_from_list_float(self): import _cffi_backend DOUBLE = _cffi_backend.new_primitive_type('double') P_DOUBLE = _cffi_backend.new_pointer_type(DOUBLE) DOUBLE_ARRAY = _cffi_backend.new_array_type(P_DOUBLE, None) buf = _cffi_backend.newp(DOUBLE_ARRAY, [1.1, 2.2, 3.3]) assert buf[0] == 1.1 assert buf[1] == 2.2 assert buf[2] == 3.3
def test_fast_init_from_list(self): import _cffi_backend LONG = _cffi_backend.new_primitive_type('long') P_LONG = _cffi_backend.new_pointer_type(LONG) LONG_ARRAY = _cffi_backend.new_array_type(P_LONG, None) buf = _cffi_backend.newp(LONG_ARRAY, [1, 2, 3]) assert buf[0] == 1 assert buf[1] == 2 assert buf[2] == 3
def test_fast_init_bool_from_list(self): import _cffi_backend BOOL = _cffi_backend.new_primitive_type('_Bool') P_BOOL = _cffi_backend.new_pointer_type(BOOL) BOOL_ARRAY = _cffi_backend.new_array_type(P_BOOL, None) buf = _cffi_backend.newp(BOOL_ARRAY, [1, 0]) assert buf[0] is True assert buf[1] is False raises(OverflowError, _cffi_backend.newp, BOOL_ARRAY, [2]) raises(OverflowError, _cffi_backend.newp, BOOL_ARRAY, [-1])
def test_fast_init_short_from_list(self): import _cffi_backend SHORT = _cffi_backend.new_primitive_type('short') P_SHORT = _cffi_backend.new_pointer_type(SHORT) SHORT_ARRAY = _cffi_backend.new_array_type(P_SHORT, None) buf = _cffi_backend.newp(SHORT_ARRAY, [1, -2, 3]) assert buf[0] == 1 assert buf[1] == -2 assert buf[2] == 3 raises(OverflowError, _cffi_backend.newp, SHORT_ARRAY, [40000]) raises(OverflowError, _cffi_backend.newp, SHORT_ARRAY, [-40000])
def test_fast_init_ushort_from_list(self): import _cffi_backend USHORT = _cffi_backend.new_primitive_type('unsigned short') P_USHORT = _cffi_backend.new_pointer_type(USHORT) USHORT_ARRAY = _cffi_backend.new_array_type(P_USHORT, None) buf = _cffi_backend.newp(USHORT_ARRAY, [1, 2, 40000]) assert buf[0] == 1 assert buf[1] == 2 assert buf[2] == 40000 raises(OverflowError, _cffi_backend.newp, USHORT_ARRAY, [70000]) raises(OverflowError, _cffi_backend.newp, USHORT_ARRAY, [-1])
def test_fast_init_bool_from_list(self): import _cffi_backend BOOL = _cffi_backend.new_primitive_type('_Bool') P_BOOL = _cffi_backend.new_pointer_type(BOOL) BOOL_ARRAY = _cffi_backend.new_array_type(P_BOOL, None) buf = _cffi_backend.newp(BOOL_ARRAY, [1, 0]) assert buf[0] == 1 assert buf[1] == 0 assert type(buf[1]) is int raises(OverflowError, _cffi_backend.newp, BOOL_ARRAY, [2]) raises(OverflowError, _cffi_backend.newp, BOOL_ARRAY, [-1])
def test_alignment_of_longlong(self): import _cffi_backend BULongLong = _cffi_backend.new_primitive_type('unsigned long long') x1 = _cffi_backend.alignof(BULongLong) assert x1 in [4, 8] # ffi, lib = self.prepare("struct foo_s { unsigned long long x; };", 'test_alignment_of_longlong', "struct foo_s { unsigned long long x; };") assert ffi.alignof('unsigned long long') == x1 assert ffi.alignof('struct foo_s') == x1
def main(libm_name): try: import _cffi_backend except ImportError: sys.stderr.write('SKIP: cannot import _cffi_backend\n') return 0 libm = _cffi_backend.load_library(libm_name) BDouble = _cffi_backend.new_primitive_type("double") BInt = _cffi_backend.new_primitive_type("int") BPow = _cffi_backend.new_function_type([BDouble, BInt], BDouble) ldexp = libm.load_function(BPow, 'ldexp') i = 0 res = 0 while i < 300: tmp = ldexp(1, 3) # ID: cfficall res += tmp i += 1 BLong = _cffi_backend.new_primitive_type("long") ldexp_addr = int(_cffi_backend.cast(BLong, ldexp)) return ldexp_addr, res
def test_alignment_of_longlong(self): import _cffi_backend BULongLong = _cffi_backend.new_primitive_type('unsigned long long') x1 = _cffi_backend.alignof(BULongLong) assert x1 in [4, 8] # ffi, lib = self.prepare( "struct foo_s { unsigned long long x; };", 'test_alignment_of_longlong', "struct foo_s { unsigned long long x; };") assert ffi.alignof('unsigned long long') == x1 assert ffi.alignof('struct foo_s') == x1
def test_fast_init_longlong_from_list(self): import _cffi_backend import sys large_int = 2 ** (50 if sys.maxsize > 2**31 - 1 else 30) LONGLONG = _cffi_backend.new_primitive_type('long long') P_LONGLONG = _cffi_backend.new_pointer_type(LONGLONG) LONGLONG_ARRAY = _cffi_backend.new_array_type(P_LONGLONG, None) buf = _cffi_backend.newp(LONGLONG_ARRAY, [1, -2, 3, large_int]) assert buf[0] == 1 assert buf[1] == -2 assert buf[2] == 3 assert buf[3] == large_int
def test_fast_init_ulong_from_list(self): import sys import _cffi_backend ULONG = _cffi_backend.new_primitive_type('unsigned long') P_ULONG = _cffi_backend.new_pointer_type(ULONG) ULONG_ARRAY = _cffi_backend.new_array_type(P_ULONG, None) buf = _cffi_backend.newp(ULONG_ARRAY, [1, 2, sys.maxsize]) assert buf[0] == 1 assert buf[1] == 2 assert buf[2] == sys.maxsize raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-1]) raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-sys.maxsize])
def test_fast_init_ulong_from_list(self): import sys import _cffi_backend ULONG = _cffi_backend.new_primitive_type('unsigned long') P_ULONG = _cffi_backend.new_pointer_type(ULONG) ULONG_ARRAY = _cffi_backend.new_array_type(P_ULONG, None) buf = _cffi_backend.newp(ULONG_ARRAY, [1, 2, sys.maxint]) assert buf[0] == 1 assert buf[1] == 2 assert buf[2] == sys.maxint raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-1]) raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-sys.maxint])
def test_fast_init_longlong_from_list(self): import _cffi_backend import sys large_int = 2**(50 if sys.maxsize > 2**31 - 1 else 30) LONGLONG = _cffi_backend.new_primitive_type('long long') P_LONGLONG = _cffi_backend.new_pointer_type(LONGLONG) LONGLONG_ARRAY = _cffi_backend.new_array_type(P_LONGLONG, None) buf = _cffi_backend.newp(LONGLONG_ARRAY, [1, -2, 3, large_int]) assert buf[0] == 1 assert buf[1] == -2 assert buf[2] == 3 assert buf[3] == large_int
def test_list_int(self): import _cffi_backend LONG = _cffi_backend.new_primitive_type('long') P_LONG = _cffi_backend.new_pointer_type(LONG) LONG_ARRAY = _cffi_backend.new_array_type(P_LONG, 3) buf = _cffi_backend.newp(LONG_ARRAY) buf[0] = 1 buf[1] = 2 buf[2] = 3 lst = list(buf) assert lst == [1, 2, 3] if not self.runappdirect: assert self.get_count() == 1
def main(): import os try: import _cffi_backend except ImportError: sys.stderr.write('SKIP: cannot import _cffi_backend\n') return 0 libc = _cffi_backend.load_library(None) BInt = _cffi_backend.new_primitive_type("int") BClose = _cffi_backend.new_function_type([BInt], BInt) _dup = libc.load_function(BClose, 'dup') i = 0 fd0, fd1 = os.pipe() while i < 300: tmp = _dup(fd0) # ID: cfficall os.close(tmp) i += 1 os.close(fd0) os.close(fd1) BLong = _cffi_backend.new_primitive_type("long") return 42
def test_list_cfloat(self): import _cffi_backend FLOAT = _cffi_backend.new_primitive_type('float') P_FLOAT = _cffi_backend.new_pointer_type(FLOAT) FLOAT_ARRAY = _cffi_backend.new_array_type(P_FLOAT, 3) buf = _cffi_backend.newp(FLOAT_ARRAY) buf[0] = 1.25 buf[1] = -2.5 buf[2] = 3.75 lst = list(buf) assert lst == [1.25, -2.5, 3.75] if not self.runappdirect: assert self.get_count() == 1
def test_list_ushort(self): import _cffi_backend USHORT = _cffi_backend.new_primitive_type('unsigned short') P_USHORT = _cffi_backend.new_pointer_type(USHORT) USHORT_ARRAY = _cffi_backend.new_array_type(P_USHORT, 3) buf = _cffi_backend.newp(USHORT_ARRAY) buf[0] = 1 buf[1] = 2 buf[2] = 50505 lst = list(buf) assert lst == [1, 2, 50505] if not self.runappdirect: assert self.get_count() == 1
def test_list_short(self): import _cffi_backend SHORT = _cffi_backend.new_primitive_type('short') P_SHORT = _cffi_backend.new_pointer_type(SHORT) SHORT_ARRAY = _cffi_backend.new_array_type(P_SHORT, 3) buf = _cffi_backend.newp(SHORT_ARRAY) buf[0] = 1 buf[1] = 2 buf[2] = 3 lst = list(buf) assert lst == [1, 2, 3] if not self.runappdirect: assert self.get_count() == 1
def test_list_float(self): import _cffi_backend DOUBLE = _cffi_backend.new_primitive_type('double') P_DOUBLE = _cffi_backend.new_pointer_type(DOUBLE) DOUBLE_ARRAY = _cffi_backend.new_array_type(P_DOUBLE, 3) buf = _cffi_backend.newp(DOUBLE_ARRAY) buf[0] = 1.1 buf[1] = 2.2 buf[2] = 3.3 lst = list(buf) assert lst == [1.1, 2.2, 3.3] if not self.runappdirect: assert self.get_count() == 1
def test_fast_init_ulong_from_list(self): import sys import _cffi_backend maxlong = sys.maxint if sys.platform == 'win32': # maxlong == 2**31-1 < sys.maxint == 2**63-1 on win64! maxlong = int(2**31 - 1) ULONG = _cffi_backend.new_primitive_type('unsigned long') P_ULONG = _cffi_backend.new_pointer_type(ULONG) ULONG_ARRAY = _cffi_backend.new_array_type(P_ULONG, None) buf = _cffi_backend.newp(ULONG_ARRAY, [1, 2, maxlong]) assert buf[0] == 1 assert buf[1] == 2 assert buf[2] == maxlong raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-1]) raises(OverflowError, _cffi_backend.newp, ULONG_ARRAY, [-maxlong])
def main(libm_name): try: import _cffi_backend except ImportError: sys.stderr.write('SKIP: cannot import _cffi_backend\n') return 0 libm = _cffi_backend.load_library(libm_name) BDouble = _cffi_backend.new_primitive_type("double") BSin = _cffi_backend.new_function_type([BDouble], BDouble) sin = libm.load_function(BSin, 'sin') def f(*args): for i in range(300): sin(*args) f(1.0) f(1)
def test_AsWriteBuffer(self): import array module = self.import_extension('buffer', [ ('write_buffer_len', 'METH_O', """ void* buf; Py_ssize_t buf_len; if (PyObject_AsWriteBuffer(args, &buf, &buf_len) < 0) { //PyErr_SetString(PyExc_ValueError, "bad value"); return NULL; } return PyLong_FromLong(buf_len); """)]) assert module.write_buffer_len(bytearray(b'123')) == 3 assert module.write_buffer_len(array.array('i', [1, 2, 3])) == 12 # import _cffi_backend BChar = _cffi_backend.new_primitive_type("char") BCharPtr = _cffi_backend.new_pointer_type(BChar) BCharArray = _cffi_backend.new_array_type(BCharPtr, None) p = _cffi_backend.newp(BCharArray, b"abcde") bb = _cffi_backend.buffer(p) assert module.write_buffer_len(bb) == 6