本文整理汇总了Python中ffi.FFI.cast方法的典型用法代码示例。如果您正苦于以下问题:Python FFI.cast方法的具体用法?Python FFI.cast怎么用?Python FFI.cast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ffi.FFI
的用法示例。
在下文中一共展示了FFI.cast方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cast_pointer_and_int
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [as 别名]
def test_cast_pointer_and_int(self):
ffi = FFI(backend=self.Backend())
a = ffi.new("short int[]", [0x1234, 0x5678])
l1 = ffi.cast("intptr_t", a)
p = ffi.cast("short*", a)
l2 = ffi.cast("intptr_t", p)
assert int(l1) == int(l2) != 0
q = ffi.cast("short*", l1)
assert q == ffi.cast("short*", int(l1))
assert q[0] == 0x1234
示例2: test_new_pointer_to_array
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [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
示例3: test_cast_functionptr_and_int
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [as 别名]
def test_cast_functionptr_and_int(self):
ffi = FFI(backend=self.Backend())
def cb(n):
return n + 1
a = ffi.callback("int(*)(int)", cb)
p = ffi.cast("void *", a)
assert p
b = ffi.cast("int(*)(int)", p)
assert b(41) == 42
assert a == b
assert hash(a) == hash(b)
示例4: test_cast_between_pointers
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [as 别名]
def test_cast_between_pointers(self):
ffi = FFI(backend=self.Backend())
a = ffi.new("short int[]", [0x1234, 0x5678])
p = ffi.cast("short*", a)
p2 = ffi.cast("int*", p)
q = ffi.cast("char*", p2)
data = ''.join([q[i] for i in range(4)])
if sys.byteorder == 'little':
assert data == '\x34\x12\x78\x56'
else:
assert data == '\x12\x34\x56\x78'
示例5: test_enum_non_contiguous
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [as 别名]
def test_enum_non_contiguous(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("enum foo { A, B=42, C };")
assert int(ffi.cast("enum foo", "A")) == 0
assert int(ffi.cast("enum foo", "B")) == 42
assert int(ffi.cast("enum foo", "C")) == 43
assert str(ffi.cast("enum foo", 0)) == "A"
assert str(ffi.cast("enum foo", 42)) == "B"
assert str(ffi.cast("enum foo", 43)) == "C"
invalid_value = ffi.cast("enum foo", 2)
assert int(invalid_value) == 2
assert str(invalid_value) == "#2"
示例6: test_pointer_direct
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [as 别名]
def test_pointer_direct(self):
ffi = FFI(backend=self.Backend())
p = ffi.cast("int*", 0)
assert bool(p) is False
assert p == ffi.cast("int*", 0)
a = ffi.new("int[]", [123, 456])
p = ffi.cast("int*", a)
assert bool(p) is True
assert p == ffi.cast("int*", a)
assert p != ffi.cast("int*", 0)
assert p[0] == 123
assert p[1] == 456
示例7: test_string_from_char_array
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [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'
示例8: test_char
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [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")
示例9: test_sizeof_cdata
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [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
示例10: test_cast_array_to_charp
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [as 别名]
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'
示例11: test_ffi_string
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [as 别名]
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'
示例12: test_pointer_arithmetic
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [as 别名]
def test_pointer_arithmetic(self):
ffi = FFI(backend=self.Backend())
s = ffi.new("short[]", range(100, 110))
p = ffi.cast("short *", s)
assert p[2] == 102
assert p+1 == p+1
assert p+1 != p+0
assert p == p+0 == p-0
assert (p+1)[0] == 101
assert (p+19)[-10] == 109
assert (p+5) - (p+1) == 4
assert p == s+0
assert p+1 == s+1
示例13: test_vararg
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [as 别名]
def test_vararg(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("""
int printf(const char *format, ...);
int fflush(void *);
""")
with FdWriteCapture() as fd:
ffi.C.printf("hello with no arguments\n")
ffi.C.printf("hello, %s!\n", ffi.new("char[]", "world"))
ffi.C.printf(ffi.new("char[]", "hello, %s!\n"),
ffi.new("char[]", "world2"))
ffi.C.printf("hello int %d long %ld long long %lld\n",
ffi.cast("int", 42),
ffi.cast("long", 84),
ffi.cast("long long", 168))
ffi.C.printf("hello %p\n", None)
ffi.C.fflush(None)
res = fd.getvalue()
assert res == ("hello with no arguments\n"
"hello, world!\n"
"hello, world2!\n"
"hello int 42 long 84 long long 168\n"
"hello (nil)\n")
示例14: test_functionptr_simple
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [as 别名]
def test_functionptr_simple(self):
ffi = FFI(backend=self.Backend())
py.test.raises(TypeError, ffi.callback, "int(*)(int)")
py.test.raises(TypeError, ffi.callback, "int(*)(int)", 0)
def cb(n):
return n + 1
p = ffi.callback("int(*)(int)", cb)
res = p(41) # calling an 'int(*)(int)', i.e. a function pointer
assert res == 42 and type(res) is int
res = p(ffi.cast("int", -41))
assert res == -40 and type(res) is int
assert repr(p).startswith(
"<cdata 'int(*)(int)' calling <function cb at 0x")
assert ffi.typeof(p) is ffi.typeof("int(*)(int)")
q = ffi.new("int(*)(int)", p)
assert repr(q) == "<cdata 'int(* *)(int)' owning %d bytes>" % (
SIZE_OF_PTR)
py.test.raises(TypeError, "q(43)")
res = q[0](43)
assert res == 44
q = ffi.cast("int(*)(int)", p)
assert repr(q) == "<cdata 'int(*)(int)'>"
res = q(45)
assert res == 46
示例15: test_char_cast
# 需要导入模块: from ffi import FFI [as 别名]
# 或者: from ffi.FFI import cast [as 别名]
def test_char_cast(self):
ffi = FFI(backend=self.Backend())
p = ffi.cast("int", '\x01')
assert ffi.typeof(p) is ffi.typeof("int")
assert int(p) == 1
p = ffi.cast("int", ffi.cast("char", "a"))
assert int(p) == ord("a")
p = ffi.cast("int", ffi.cast("char", "\x80"))
assert int(p) == 0x80 # "char" is considered unsigned in this case
p = ffi.cast("int", "\x81")
assert int(p) == 0x81