本文整理汇总了Python中ffi.FFI.new方法的典型用法代码示例。如果您正苦于以下问题:Python FFI.new方法的具体用法?Python FFI.new怎么用?Python FFI.new使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ffi.FFI
的用法示例。
在下文中一共展示了FFI.new方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_new_array_of_pointer_2
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [as 别名]
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
示例2: test_pointer_init
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [as 别名]
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
示例3: test_sizeof_cdata
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [as 别名]
def test_sizeof_cdata(self):
ffi = FFI(backend=self.Backend())
assert ffi.sizeof(ffi.new("short")) == SIZE_OF_PTR
assert ffi.sizeof(ffi.cast("short", 123)) == SIZE_OF_SHORT
#
a = ffi.new("int[]", [10, 11, 12, 13, 14])
assert len(a) == 5
assert ffi.sizeof(a) == 5 * SIZE_OF_INT
示例4: test_new_single_integer
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [as 别名]
def test_new_single_integer(self):
ffi = FFI(backend=self.Backend())
p = ffi.new("int") # similar to ffi.new("int[1]")
assert p[0] == 0
p[0] = -123
assert p[0] == -123
p = ffi.new("int", -42)
assert p[0] == -42
assert repr(p) == "<cdata 'int *' owning %d bytes>" % SIZE_OF_INT
示例5: test_anonymous_struct
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [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"
示例6: test_fetch_const_char_p_field
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [as 别名]
def test_fetch_const_char_p_field(self):
# 'const' is ignored so far
ffi = FFI(backend=self.Backend())
ffi.cdef("struct foo { const char *name; };")
t = ffi.new("const char[]", "testing")
s = ffi.new("struct foo", [t])
assert type(s.name) is not str
assert str(s.name) == "testing"
s.name = None
assert s.name is None
示例7: test_none_as_null
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [as 别名]
def test_none_as_null(self):
ffi = FFI(backend=self.Backend())
p = ffi.new("int*[1]")
assert p[0] is None
#
n = ffi.new("int", 99)
p = ffi.new("int*[]", [n])
assert p[0][0] == 99
p[0] = None
assert p[0] is None
示例8: test_new_pointer_to_array
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [as 别名]
def test_new_pointer_to_array(self):
ffi = FFI(backend=self.Backend())
a = ffi.new("int[4]", [100, 102, 104, 106])
p = ffi.new("int *", a)
assert p[0] == ffi.cast("int *", a)
assert p[0][2] == 104
p = ffi.cast("int *", a)
assert p[0] == 100
assert p[1] == 102
assert p[2] == 104
assert p[3] == 106
示例9: test_union_simple
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [as 别名]
def test_union_simple(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("union foo { int a; short b, c; };")
u = ffi.new("union foo")
assert u.a == u.b == u.c == 0
u.b = -23
assert u.b == -23
assert u.a != 0
py.test.raises(OverflowError, "u.b = 32768")
#
u = ffi.new("union foo", -2)
assert u.a == -2
py.test.raises((AttributeError, TypeError), "del u.a")
assert repr(u) == "<cdata 'union foo *' owning %d bytes>" % SIZE_OF_INT
示例10: test_string_from_char_array
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [as 别名]
def test_string_from_char_array(self):
ffi = FFI(backend=self.Backend())
assert str(ffi.cast("char", "x")) == "x"
p = ffi.new("char[]", "hello.")
p[5] = '!'
assert str(p) == "hello!"
p[6] = '?'
assert str(p) == "hello!?"
p[3] = '\x00'
assert str(p) == "hel"
py.test.raises(IndexError, "p[7] = 'X'")
#
a = ffi.new("char[]", "hello\x00world")
assert len(a) == 12
p = ffi.cast("char *", a)
assert str(p) == 'hello'
示例11: test_constructor_struct_from_dict
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [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})
示例12: test_passing_array
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [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
示例13: test_strchr
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [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!"
示例14: test_new_array_of_array
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [as 别名]
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]")
示例15: test_char
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import new [as 别名]
def test_char(self):
ffi = FFI(backend=self.Backend())
assert ffi.new("char", "\xff")[0] == '\xff'
assert ffi.new("char")[0] == '\x00'
assert int(ffi.cast("char", 300)) == 300 - 256
assert bool(ffi.new("char"))
py.test.raises(TypeError, ffi.new, "char", 32)
py.test.raises(TypeError, ffi.new, "char", "foo")
#
p = ffi.new("char[]", ['a', 'b', '\x9c'])
assert len(p) == 3
assert p[0] == 'a'
assert p[1] == 'b'
assert p[2] == '\x9c'
p[0] = '\xff'
assert p[0] == '\xff'
p = ffi.new("char[]", "abcd")
assert len(p) == 5
assert p[4] == '\x00' # like in C, with: char[] p = "abcd";
#
p = ffi.new("char[4]", "ab")
assert len(p) == 4
assert [p[i] for i in range(4)] == ['a', 'b', '\x00', '\x00']
p = ffi.new("char[2]", "ab")
assert len(p) == 2
assert [p[i] for i in range(2)] == ['a', 'b']
py.test.raises(IndexError, ffi.new, "char[2]", "abc")