本文整理汇总了Python中ffi.FFI.cdef方法的典型用法代码示例。如果您正苦于以下问题:Python FFI.cdef方法的具体用法?Python FFI.cdef怎么用?Python FFI.cdef使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ffi.FFI
的用法示例。
在下文中一共展示了FFI.cdef方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_simple
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
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
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
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_strchr
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
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!"
示例4: test_must_specify_type_of_vararg
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
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)")
示例5: test_sin
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
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)
示例6: test_passing_array
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
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
示例7: test_array_of_struct
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
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]')
示例8: test_function_with_struct_argument
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
def test_function_with_struct_argument(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("""
struct in_addr { unsigned int s_addr; };
char *inet_ntoa(struct in_addr in);
""")
ina = ffi.new("struct in_addr", [0x04040404])
a = ffi.C.inet_ntoa(ina[0])
assert str(a) == '4.4.4.4'
示例9: test_typedef
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
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>'
示例10: test_struct_pointer
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
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_anonymous_struct
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
def test_anonymous_struct(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("typedef struct { int a; } foo_t;")
ffi.cdef("typedef struct { char b, c; } bar_t;")
f = ffi.new("foo_t", [12345])
b = ffi.new("bar_t", ["B", "C"])
assert f.a == 12345
assert f.b == "B"
assert f.c == "C"
示例12: test_getting_errno
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
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
示例13: test_simple_verify
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
def test_simple_verify():
ffi = FFI()
ffi.cdef("void some_completely_unknown_function();")
py.test.raises(CompilationError, ffi.verify)
ffi = FFI()
ffi.cdef("double sin(double x);")
# omission of math.h
py.test.raises(CompilationError, ffi.verify)
assert ffi.verify('#include <math.h>') is None
示例14: test_function_has_a_c_type
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
def test_function_has_a_c_type(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("""
int puts(const char *);
""")
fptr = ffi.C.puts
assert ffi.typeof(fptr) == ffi.typeof("int(*)(const char*)")
if self.Backend is CTypesBackend:
assert repr(fptr) == "<cdata 'int puts(char *)'>"
示例15: test_typedef_more_complex
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cdef [as 别名]
def test_typedef_more_complex():
ffi = FFI(backend=FakeBackend())
ffi.cdef("""
typedef struct { int a, b; } foo_t, *foo_p;
int foo(foo_p[]);
""")
assert str(ffi.typeof("foo_t")) == '<int>a, <int>b'
assert ffi.typeof("foo_p") == '<pointer to <int>a, <int>b>'
assert ffi.C.foo.BType == ('<func (<pointer to <pointer to '
'<int>a, <int>b>>), <int>, False>')