本文整理汇总了Python中ffi.FFI类的典型用法代码示例。如果您正苦于以下问题:Python FFI类的具体用法?Python FFI怎么用?Python FFI使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FFI类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_simple
def test_simple():
ffi = FFI(backend=FakeBackend())
ffi.cdef("double sin(double x);")
m = ffi.load("m")
func = m.sin # should be a callable on real backends
assert func.name == 'sin'
assert func.BType == '<func (<double>), <double>, False>'
示例2: test_constructor_struct_from_dict
def test_constructor_struct_from_dict(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("struct foo { int a; short b, c; };")
s = ffi.new("struct foo", {'b': 123, 'c': 456})
assert s.a == 0
assert s.b == 123
assert s.c == 456
py.test.raises(KeyError, ffi.new, "struct foo", {'d': 456})
示例3: test_must_specify_type_of_vararg
def test_must_specify_type_of_vararg(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("""
int printf(const char *format, ...);
""")
e = py.test.raises(TypeError, ffi.C.printf, "hello %d\n", 42)
assert str(e.value) == ("argument 2 passed in the variadic part "
"needs to be a cdata object (got int)")
示例4: test_passing_array
def test_passing_array(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("""
int strlen(char[]);
""")
p = ffi.new("char[]", "hello")
res = ffi.C.strlen(p)
assert res == 5
示例5: test_new_array_of_pointer_2
def test_new_array_of_pointer_2(self):
ffi = FFI(backend=self.Backend())
n = ffi.new("int[1]", [99])
p = ffi.new("int*[4]")
p[3] = n
a = p[3]
assert repr(a) == "<cdata 'int *'>"
assert a[0] == 99
示例6: test_new_array_of_array
def test_new_array_of_array(self):
ffi = FFI(backend=self.Backend())
p = ffi.new("int[3][4]")
p[0][0] = 10
p[2][3] = 33
assert p[0][0] == 10
assert p[2][3] == 33
py.test.raises(IndexError, "p[1][-1]")
示例7: test_strchr
def test_strchr(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("""
char *strchr(const char *s, int c);
""")
p = ffi.new("char[]", "hello world!")
q = ffi.C.strchr(p, ord('w'))
assert str(q) == "world!"
示例8: test_pointer_init
def test_pointer_init(self):
ffi = FFI(backend=self.Backend())
n = ffi.new("int", 24)
a = ffi.new("int *[10]", [None, None, n, n, None])
for i in range(10):
if i not in (2, 3):
assert a[i] is None
assert a[2] == a[3] == n
示例9: test_sin
def test_sin(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("""
double sin(double x);
""")
m = ffi.load("m")
x = m.sin(1.23)
assert x == math.sin(1.23)
示例10: test_struct_pointer
def test_struct_pointer(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("struct foo { int a; short b, c; };")
s = ffi.new("struct foo")
assert s[0].a == s[0].b == s[0].c == 0
s[0].b = -23
assert s[0].b == s.b == -23
py.test.raises(OverflowError, "s[0].b = -32769")
py.test.raises(IndexError, "s[1]")
示例11: test_typedef
def test_typedef():
ffi = FFI(backend=FakeBackend())
ffi.cdef("""
typedef unsigned int UInt;
typedef UInt UIntReally;
UInt foo(void);
""")
assert ffi.typeof("UIntReally") == '<unsigned int>'
assert ffi.C.foo.BType == '<func (), <unsigned int>, False>'
示例12: test_cast_array_to_charp
def test_cast_array_to_charp(self):
ffi = FFI(backend=self.Backend())
a = ffi.new("short int[]", [0x1234, 0x5678])
p = ffi.cast("char*", a)
data = ''.join([p[i] for i in range(4)])
if sys.byteorder == 'little':
assert data == '\x34\x12\x78\x56'
else:
assert data == '\x12\x34\x56\x78'
示例13: test_getting_errno
def test_getting_errno(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("""
int test_getting_errno(void);
""")
ownlib = ffi.load(self.module)
res = ownlib.test_getting_errno()
assert res == -1
assert ffi.C.errno == 123
示例14: test_ffi_string
def test_ffi_string(self):
ffi = FFI(backend=self.Backend())
a = ffi.new("int[]", range(100, 110))
s = ffi.string(ffi.cast("void *", a), 8)
assert type(s) is str
if sys.byteorder == 'little':
assert s == '\x64\x00\x00\x00\x65\x00\x00\x00'
else:
assert s == '\x00\x00\x00\x64\x00\x00\x00\x65'
示例15: test_array_of_struct
def test_array_of_struct(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("struct foo { int a, b; };")
s = ffi.new("struct foo[1]")
py.test.raises(AttributeError, 's.b')
py.test.raises(AttributeError, 's.b = 412')
s[0].b = 412
assert s[0].b == 412
py.test.raises(IndexError, 's[1]')