当前位置: 首页>>代码示例>>Python>>正文


Python ffi.FFI类代码示例

本文整理汇总了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>'
开发者ID:AN2016,项目名称:ffi,代码行数:7,代码来源:test_parsing.py

示例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})
开发者ID:AN2016,项目名称:ffi,代码行数:8,代码来源:backend_tests.py

示例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)")
开发者ID:AN2016,项目名称:ffi,代码行数:8,代码来源:test_function.py

示例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
开发者ID:AN2016,项目名称:ffi,代码行数:8,代码来源:test_function.py

示例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
开发者ID:AN2016,项目名称:ffi,代码行数:8,代码来源:backend_tests.py

示例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]")
开发者ID:AN2016,项目名称:ffi,代码行数:8,代码来源:backend_tests.py

示例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!"
开发者ID:AN2016,项目名称:ffi,代码行数:8,代码来源:test_function.py

示例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
开发者ID:AN2016,项目名称:ffi,代码行数:8,代码来源:backend_tests.py

示例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)
开发者ID:AN2016,项目名称:ffi,代码行数:8,代码来源:test_function.py

示例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]")
开发者ID:AN2016,项目名称:ffi,代码行数:9,代码来源:backend_tests.py

示例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>'
开发者ID:AN2016,项目名称:ffi,代码行数:9,代码来源:test_parsing.py

示例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'
开发者ID:AN2016,项目名称:ffi,代码行数:9,代码来源:backend_tests.py

示例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
开发者ID:AN2016,项目名称:ffi,代码行数:9,代码来源:test_ownlib.py

示例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'
开发者ID:AN2016,项目名称:ffi,代码行数:9,代码来源:backend_tests.py

示例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]')
开发者ID:AN2016,项目名称:ffi,代码行数:9,代码来源:backend_tests.py


注:本文中的ffi.FFI类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。